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

35 lines
815 B
C

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define LOOP_COUNT 1000000
#define EXPECTED_CHECKSUM 345000001
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 parse_loop(int32_t limit, const char *text) {
int32_t i = 0;
int32_t acc = 1;
while (i < limit) {
acc = acc + (int32_t)strtol(text, NULL, 10);
acc = acc > 1000000000 ? acc - 1000000000 : acc;
i = i + 1;
}
return acc;
}
int main(int argc, char **argv) {
const char *text = argc > 1 ? argv[1] : "12345";
int32_t result = parse_loop(configured_loop_count(), text);
printf("%d\n", result);
return result == EXPECTED_CHECKSUM ? 0 : 1;
}