slovo/benchmarks/branch-loop/c/branch_loop.c
2026-05-22 08:38:43 +02:00

34 lines
750 B
C

#include <stdint.h>
#include <stdio.h>
#define LOOP_COUNT 1000000
#define EXPECTED_CHECKSUM 1185071
static int32_t configured_loop_count(void) {
int32_t value = LOOP_COUNT;
if (scanf("%d", &value) != 1 || value <= 0) {
return LOOP_COUNT;
}
return value;
}
static int32_t branch_loop(int32_t limit) {
int32_t i = 0;
int32_t acc = 7;
while (i < limit) {
acc = acc > 1000000000 ? acc - 1000000000 : acc;
acc = i < 500000 ? acc + 3 : acc + 7;
acc = acc > 1234567 ? acc - 1234567 : acc + 11;
i = i + 1;
}
return acc;
}
int main(void) {
int32_t result = branch_loop(configured_loop_count());
printf("%d\n", result);
return result == EXPECTED_CHECKSUM ? 0 : 1;
}