#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_CUR;
unsigned long atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int length;
  printf("search 'number.txt' for the largest sum of adjacent digits.\n");
  printf("how long should the string of adjacent digits be?\n");
  scanf("%d", &length);
  int string_bytes = length * sizeof(char);
  FILE *fp;
  fp = fopen("number.txt", "r");
  if (fp == NULL) {
    perror("failed to open file\n");
    exit(EXIT_FAILURE);
  }
  char c;
  unsigned long m = 0, product, max_product = 0, done = 0, distance = 0;
  int extras = 0;
  while (done != 1) {
    extras = 0;
    product = 1;
    printf("starting at %lu .. ", ftell(fp));
    for (int j = 0; j < length; j++) {
      c = fgetc(fp);
      if (c == EOF) {
        done = 1;
        break;
      } else if (c == '\n') {
        extras++;
        j--;
        continue;
      }
      m = atoi(&c);
      if (j == 0)
        printf("%lu", product);
      printf("* %lu", m);
      product = m * product;
    }
    printf(" = %lu\n", product);
    max_product = product > max_product ? product : max_product;
    fseek(fp, -(length - 1) - extras, SEEK_CUR);
  }
  printf("%lu\n", max_product);
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9e(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <scanf@plt>
movslq -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,-0xc(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1222 <main+0x82>
lea    0xe71(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpq   $0x1,-0x40(%rbp)
je     139f <main+0x1ff>
movl   $0x0,-0x4c(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    133e <main+0x19e>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
mov    0x2d39(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    12bf <main+0x11f>
movq   $0x1,-0x40(%rbp)
jmp    133e <main+0x19e>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    12e3 <main+0x143>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x50(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x50(%rbp)
jmp    1330 <main+0x190>
jmp    12e8 <main+0x148>
lea    -0x19(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x50(%rbp)
jne    1311 <main+0x171>
mov    -0x30(%rbp),%rsi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
imul   -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1284 <main+0xe4>
mov    -0x30(%rbp),%rsi
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
jbe    136b <main+0x1cb>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    1373 <main+0x1d3>
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
xor    %esi,%esi
sub    %eax,%esi
sub    -0x4c(%rbp),%esi
mov    0x2c55(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
jmp    1249 <main+0xa9>
mov    -0x38(%rbp),%rsi
lea    0xd08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe23(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1090 <scanf@plt>
lea    0xe15(%rip),%rdi
lea    0xe19(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     134e <main+0x19e>
mov    %rax,%r13
mov    0x2dd3(%rip),%rbx
xor    %ebp,%ebp
jmp    1277 <main+0xc7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%r14d
movl   $0x0,0x8(%rsp)
xor    %r12d,%r12d
lea    0xe07(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%rbp
cmp    %rbp,%r14
cmova  %r14,%rbp
mov    0x8(%rsp),%eax
add    0xc(%rsp),%eax
mov    $0x1,%esi
sub    %eax,%esi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
call   1080 <fseek@plt>
test   %r12b,%r12b
jne    1324 <main+0x174>
mov    %rbp,0x10(%rsp)
mov    %r13,%rdi
call   1070 <ftell@plt>
lea    0xd9a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x0,0xc(%rsp)
jle    1220 <main+0x70>
mov    $0x1,%r14d
xor    %r15d,%r15d
movl   $0x0,0x8(%rsp)
jmp    12c8 <main+0x118>
nop
addl   $0x1,0x8(%rsp)
add    $0xffffffff,%r15d
add    $0x1,%r15d
cmp    0xc(%rsp),%r15d
jge    1231 <main+0x81>
mov    %r13,%rdi
call   1060 <fgetc@plt>
mov    %al,0x7(%rsp)
cmp    (%rbx),%al
sete   %r12b
je     1231 <main+0x81>
cmp    $0xa,%al
je     12b0 <main+0x100>
mov    %r13,%rbp
lea    0x7(%rsp),%rdi
call   10c0 <atoi@plt>
mov    %rax,%r13
test   %r15d,%r15d
jne    130a <main+0x15a>
lea    0xd3b(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd28(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
imul   %r13,%r14
mov    %rbp,%r13
jmp    12b9 <main+0x109>
lea    0xd17(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcbb(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [61 x i8] c"search 'number.txt' for the largest sum of adjacent digits.\0A\00"
@.str.1 = [51 x i8] c"how long should the string of adjacent digits be?\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [11 x i8] c"number.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [21 x i8] c"failed to open file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.6 = [20 x i8] c"starting at %lu .. \00"
@EOF = global i8 0
@.str.7 = [4 x i8] c"%lu\00"
@.str.8 = [6 x i8] c"* %lu\00"
@.str.9 = [8 x i8] c" = %lu\0A\00"
@SEEK_CUR = global i32 0
@.str.10 = [5 x i8] c"%lu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
%19 = trunc i64 %18 to i32
store i32 %19, i32* %3
%20 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %20, i32** %4
%21 = load i32*, i32** %4
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %0
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %0
store i64 0, i64* %6
store i64 0, i64* %8
store i64 0, i64* %9
store i64 0, i64* %10
store i32 0, i32* %11
br label %28
28: ; preds = %84, %27
%29 = load i64, i64* %9
%30 = icmp ne i64 %29, 1
br i1 %30, label %31, label %94
31: ; preds = %28
store i32 0, i32* %11
store i64 1, i64* %7
%32 = load i32*, i32** %4
%33 = call i64 @ftell(i32* %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %33)
store i32 0, i32* %12
br label %35
35: ; preds = %71, %31
%36 = load i32, i32* %12
%37 = load i32, i32* %2
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %74
39: ; preds = %35
%40 = load i32*, i32** %4
%41 = call signext i8 @fgetc(i32* %40)
store i8 %41, i8* %5
%42 = load i8, i8* %5
%43 = sext i8 %42 to i32
%44 = load i8, i8* @EOF
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %43, %45
br i1 %46, label %47, label %48
47: ; preds = %39
store i64 1, i64* %9
br label %74
48: ; preds = %39
%49 = load i8, i8* %5
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 10
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
%55 = load i32, i32* %12
%56 = add nsw i32 %55, -1
store i32 %56, i32* %12
br label %71
57: ; preds = %48
br label %58
58: ; preds = %57
%59 = call i64 @atoi(i8* %5)
store i64 %59, i64* %6
%60 = load i32, i32* %12
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load i64, i64* %7
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i64 %63)
br label %65
65: ; preds = %62, %58
%66 = load i64, i64* %6
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i64 %66)
%68 = load i64, i64* %6
%69 = load i64, i64* %7
%70 = mul i64 %68, %69
store i64 %70, i64* %7
br label %71
71: ; preds = %65, %52
%72 = load i32, i32* %12
%73 = add nsw i32 %72, 1
store i32 %73, i32* %12
br label %35
74: ; preds = %47, %35
%75 = load i64, i64* %7
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i64 %75)
%77 = load i64, i64* %7
%78 = load i64, i64* %8
%79 = icmp ugt i64 %77, %78
br i1 %79, label %80, label %82
80: ; preds = %74
%81 = load i64, i64* %7
br label %84
82: ; preds = %74
%83 = load i64, i64* %8
br label %84
84: ; preds = %82, %80
%85 = phi i64 [ %81, %80 ], [ %83, %82 ]
store i64 %85, i64* %8
%86 = load i32*, i32** %4
%87 = load i32, i32* %2
%88 = sub nsw i32 %87, 1
%89 = sub nsw i32 0, %88
%90 = load i32, i32* %11
%91 = sub nsw i32 %89, %90
%92 = load i32, i32* @SEEK_CUR
%93 = call i32 @fseek(i32* %86, i32 %91, i32 %92)
br label %28
94: ; preds = %28
%95 = load i64, i64* %8
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i64 %95)
%97 = load i32*, i32** %4
%98 = call i32 @fclose(i32* %97)
%99 = load i32, i32* %1
ret i32 %99
}

LLVM IR (-O2):
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [11 x i8] c"number.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [21 x i8] c"failed to open file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.6 = [20 x i8] c"starting at %lu .. \00"
@EOF = global i8 0
@.str.7 = [4 x i8] c"%lu\00"
@.str.8 = [6 x i8] c"* %lu\00"
@.str.9 = [8 x i8] c" = %lu\0A\00"
@SEEK_CUR = global i32 0
@.str.10 = [5 x i8] c"%lu\0A\00"
@str = [60 x i8] c"search 'number.txt' for the largest sum of adjacent digits.\00"
@str.11 = [50 x i8] c"how long should the string of adjacent digits be?\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = bitcast i32* %1 to i8*
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.11, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%7 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
br label %14
14: ; preds = %13, %47
%15 = phi i64 [ 0, %13 ], [ %53, %47 ]
%16 = call i64 @ftell(i32* %7)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %16)
%18 = load i32, i32* %1
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %47
20: ; preds = %14, %40
%21 = phi i32 [ %44, %40 ], [ 0, %14 ]
%22 = phi i32 [ %42, %40 ], [ 0, %14 ]
%23 = phi i64 [ %41, %40 ], [ 1, %14 ]
%24 = call signext i8 @fgetc(i32* %7)
store i8 %24, i8* %2
%25 = load i8, i8* @EOF
%26 = icmp eq i8 %24, %25
br i1 %26, label %47, label %27
27: ; preds = %20
%28 = icmp eq i8 %24, 10
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = add nsw i32 %22, 1
%31 = add nsw i32 %21, -1
br label %40
32: ; preds = %27
%33 = call i64 @atoi(i8* %2)
%34 = icmp eq i32 %21, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i64 %23)
br label %37
37: ; preds = %35, %32
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i64 %33)
%39 = mul i64 %33, %23
br label %40
40: ; preds = %37, %29
%41 = phi i64 [ %23, %29 ], [ %39, %37 ]
%42 = phi i32 [ %30, %29 ], [ %22, %37 ]
%43 = phi i32 [ %31, %29 ], [ %21, %37 ]
%44 = add nsw i32 %43, 1
%45 = load i32, i32* %1
%46 = icmp slt i32 %44, %45
br i1 %46, label %20, label %47
47: ; preds = %40, %20, %14
%48 = phi i64 [ 1, %14 ], [ %23, %20 ], [ %41, %40 ]
%49 = phi i32 [ 0, %14 ], [ %22, %20 ], [ %42, %40 ]
%50 = phi i1 [ false, %14 ], [ %26, %20 ], [ %26, %40 ]
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i64 %48)
%52 = icmp ugt i64 %48, %15
%53 = select i1 %52, i64 %48, i64 %15
%54 = load i32, i32* %1
%55 = add i32 %49, %54
%56 = sub i32 1, %55
%57 = load i32, i32* @SEEK_CUR
%58 = call i32 @fseek(i32* %7, i32 %56, i32 %57)
br i1 %50, label %59, label %14
59: ; preds = %47
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i64 %53)
%61 = call i32 @fclose(i32* %7)
ret i32 0
}