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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, int, char *);
int main(int argc, char *argv[]) {
  char *filename = "nonexistent.txt";
  FILE *file = fopen(filename, "r");
  char buffer[128];
  for (int line = 0; fgets(buffer, sizeof(buffer), file) != NULL; line++) {
    printf("%d: %s", line, buffer);
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea0(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0xa4(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11ce <main+0x8e>
mov    -0xa4(%rbp),%esi
lea    -0xa0(%rbp),%rdx
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    1182 <main+0x42>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
lea    0xeac(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11b5 <main+0x75>
xor    %ebp,%ebp
lea    0xe91(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1190 <main+0x50>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [16 x i8] c"nonexistent.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"%d: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = call i32* @fgets(i8* %5, i32 128, i32* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %10, %2
%9 = call i32 @fclose(i32* %4)
ret i32 0
10: ; preds = %2, %10
%11 = phi i32 [ %13, %10 ], [ 0, %2 ]
%12 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %11, i8* %5)
%13 = add nuw nsw i32 %11, 1
%14 = call i32* @fgets(i8* %5, i32 128, i32* %4)
%15 = icmp eq i32* %14, null
br i1 %15, label %8, label %10
}