#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 */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ malloc(int);
int readfile(char *filename) {
  int sum;
  char *line;
  FILE *file = NULL;
  sum = 0;
  file = fopen(filename, "r");
  if (file != NULL) {
    line = (char *)malloc(1000);
    while (1) {
      if (fgets(line, 1000, file))
        sum = sum + atoi(line);
      else
        break;
    }
    fclose(file);
  }
  return (sum);
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e5 <readfile+0x95>
mov    $0x3e8,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3e8,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11d2 <readfile+0x82>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    11d7 <readfile+0x87>
jmp    11dc <readfile+0x8c>
jmp    1198 <readfile+0x48>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r14
push   %rbx
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b9 <readfile+0x69>
mov    %rax,%r14
mov    $0x3e8,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x3e8,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11af <readfile+0x5f>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <atoi@plt>
add    %eax,%ebp
mov    %rbx,%rdi
mov    $0x3e8,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1190 <readfile+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11bb <readfile+0x6b>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @readfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %2
store i32* null, i32** %5
store i32 0, i32* %3
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %28
10: ; preds = %1
%11 = call i64 @malloc(i32 1000)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %4
br label %13
13: ; preds = %10, %24
%14 = load i8*, i8** %4
%15 = load i32*, i32** %5
%16 = call i64 @fgets(i8* %14, i32 1000, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* %3
%20 = load i8*, i8** %4
%21 = call i32 @atoi(i8* %20)
%22 = add nsw i32 %19, %21
store i32 %22, i32* %3
br label %24
23: ; preds = %13
br label %25
24: ; preds = %18
br label %13
25: ; preds = %23
%26 = load i32*, i32** %5
%27 = call i32 @fclose(i32* %26)
br label %28
28: ; preds = %25, %1
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @readfile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %18, label %4
4: ; preds = %1
%5 = tail call i64 @malloc(i32 1000)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i64 @fgets(i8* %6, i32 1000, i32* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %4, %9
%10 = phi i32 [ %12, %9 ], [ 0, %4 ]
%11 = tail call i32 @atoi(i8* %6)
%12 = add nsw i32 %11, %10
%13 = tail call i64 @fgets(i8* %6, i32 1000, i32* %2)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %9
15: ; preds = %9, %4
%16 = phi i32 [ 0, %4 ], [ %12, %9 ]
%17 = tail call i32 @fclose(i32* %2)
br label %18
18: ; preds = %15, %1
%19 = phi i32 [ %16, %15 ], [ 0, %1 ]
ret i32 %19
}