#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*/ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
int atoi(char *);
scalar_t__ count_and_add(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ perror(char const *);
uint64_t count(const char *filename) {
  char line[BUFSIZ];
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    perror(filename);
    exit(1);
  }
  uint64_t c = 0;
  while (fgets(line, sizeof line, fp)) {
    int x = atoi(line);
    c += count_and_add(x);
  }
  fclose(fp);
  return c;
}


x86 assembly (clang, -O0):
<count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11de <count+0x6e>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1228 <count+0xb8>
mov    -0x30(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <count_and_add@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    11e5 <count+0x75>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    0x2e5b(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r12
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe60(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     120b <count+0x9b>
mov    %rax,%r15
mov    %r12,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11f4 <count+0x84>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1090 <count_and_add@plt>
add    %eax,%ebx
mov    %r12,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    11d0 <count+0x60>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @count(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i32, i32* @BUFSIZ
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %3
%11 = alloca i8, i64 %9
store i64 %9, i64* %4
%12 = load i8*, i8** %2
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp ne i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = call i32 @perror(i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
store i32 0, i32* %6
br label %21
21: ; preds = %26, %20
%22 = trunc i64 %9 to i32
%23 = load i32*, i32** %5
%24 = call i64 @fgets(i8* %11, i32 %22, i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %21
%27 = call i32 @atoi(i8* %11)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = call i64 @count_and_add(i32 %28)
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = add nsw i64 %31, %29
%33 = trunc i64 %32 to i32
store i32 %33, i32* %6
br label %21
34: ; preds = %21
%35 = load i32*, i32** %5
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %6
%38 = load i8*, i8** %3
ret i32 %37
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @count(i8* nocapture %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = call i64 @fgets(i8* %4, i32 %2, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %21, label %13
10: ; preds = %1
%11 = tail call i32 @perror(i8* %0)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %7, %13
%14 = phi i32 [ %18, %13 ], [ 0, %7 ]
%15 = call i32 @atoi(i8* %4)
%16 = call i64 @count_and_add(i32 %15)
%17 = trunc i64 %16 to i32
%18 = add i32 %14, %17
%19 = call i64 @fgets(i8* %4, i32 %2, i32* %5)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %13
21: ; preds = %13, %7
%22 = phi i32 [ 0, %7 ], [ %18, %13 ]
%23 = call i32 @fclose(i32* %5)
ret i32 %22
}