#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 EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char *);
char *fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putc(int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
void test_fopen(const char *fn) {
  FILE *fp;
  int c;
  char line[1024];
  char *rv;
  printf("test_fopen: entered\n");
  fp = fopen(fn, "rb");
  if (NULL == fp) {
    printf("could not open file %s!\n", fn);
    exit(-1);
  } else {
    printf("reading file\n");
    while (EOF != (c = fgetc(fp))) {
      if ((int)c != 13)
        putc(c, stdout);
    }
    fclose(fp);
  }
  while (0 != (rv = (fputs("input: ", stdout), fflush(stdout),
                     fgets(line, sizeof line, stdin)))) {
    printf("Got line: <<%s>>, %d\n", rv, strlen(rv));
  }
  printf("test_fopen: done\n");
}


x86 assembly (clang, -O0):
<test_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11f9 <test_fopen+0x59>
mov    -0x8(%rbp),%rsi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,%ecx
mov    -0x42c(%rbp),%eax
mov    %ecx,-0x14(%rbp)
cmp    %ecx,%eax
je     1252 <test_fopen+0xb2>
cmpl   $0xd,-0x14(%rbp)
je     124d <test_fopen+0xad>
mov    -0x14(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1090 <putc@plt>
jmp    1207 <test_fopen+0x67>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1260 <test_fopen+0xc0>
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    0xdcf(%rip),%rdi
call   1060 <fputs@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
lea    -0x420(%rbp),%rdi
mov    0x2d57(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1080 <fgets@plt>
movslq %eax,%rcx
mov    %rcx,-0x428(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     12e8 <test_fopen+0x148>
mov    -0x428(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1260 <test_fopen+0xc0>
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fopen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe29(%rip),%rsi
mov    %r14,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1304 <test_fopen+0x154>
mov    %rax,%rbx
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de2(%rip),%r15
mov    (%r15),%r14d
mov    %rbx,%rdi
call   1080 <fgetc@plt>
cmp    %eax,%r14d
jne    12d1 <test_fopen+0x121>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2daf(%rip),%r13
mov    0x0(%r13),%esi
lea    0xdf0(%rip),%rdi
call   1070 <fputs@plt>
mov    0x0(%r13),%edi
call   10b0 <fflush@plt>
mov    0x2da7(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %eax,%eax
je     12b3 <test_fopen+0x103>
lea    0xdc8(%rip),%r14
lea    0xdb9(%rip),%r15
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
movslq %eax,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1070 <fputs@plt>
mov    0x0(%r13),%edi
call   10b0 <fflush@plt>
mov    0x0(%rbp),%edx
mov    %r12,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %eax,%eax
jne    1270 <test_fopen+0xc0>
lea    0xda1(%rip),%rdi
call   1030 <puts@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cf8(%rip),%r14
jmp    12f3 <test_fopen+0x143>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%ebp
mov    %rbx,%rdi
call   1080 <fgetc@plt>
cmp    %eax,%ebp
je     1212 <test_fopen+0x62>
cmp    $0xd,%eax
je     12e0 <test_fopen+0x130>
mov    (%r14),%esi
mov    %eax,%edi
call   10a0 <putc@plt>
jmp    12e0 <test_fopen+0x130>
lea    0xcf8(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"test_fopen: entered\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [25 x i8] c"could not open file %s!\0A\00"
@.str.3 = [14 x i8] c"reading file\0A\00"
@EOF = global i32 0
@stdout = global i32 0
@.str.4 = [8 x i8] c"input: \00"
@stdin = global i32 0
@.str.5 = [22 x i8] c"Got line: <<%s>>, %d\0A\00"
@.str.6 = [18 x i8] c"test_fopen: done\0A\00"
define void @test_fopen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i8*
store i8* %0, i8** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = icmp eq i32* null, %10
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %1
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %30, %16
%19 = load i32, i32* @EOF
%20 = load i32*, i32** %3
%21 = call i32 @fgetc(i32* %20)
store i32 %21, i32* %4
%22 = icmp ne i32 %19, %21
br i1 %22, label %23, label %31
23: ; preds = %18
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 13
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* %4
%28 = load i32, i32* @stdout
%29 = call i32 @putc(i32 %27, i32 %28)
br label %30
30: ; preds = %26, %23
br label %18
31: ; preds = %18
%32 = load i32*, i32** %3
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %31
br label %35
35: ; preds = %46, %34
%36 = load i32, i32* @stdout
%37 = call i8* @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @stdout
%39 = call i32 @fflush(i32 %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%41 = load i32, i32* @stdin
%42 = call i32 @fgets(i8* %40, i32 1024, i32 %41)
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %6
%45 = icmp ne i8* null, %44
br i1 %45, label %46, label %51
46: ; preds = %35
%47 = load i8*, i8** %6
%48 = load i8*, i8** %6
%49 = call i32 @strlen(i8* %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %47, i32 %49)
br label %35
51: ; preds = %35
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [25 x i8] c"could not open file %s!\0A\00"
@EOF = global i32 0
@stdout = global i32 0
@.str.4 = [8 x i8] c"input: \00"
@stdin = global i32 0
@.str.5 = [22 x i8] c"Got line: <<%s>>, %d\0A\00"
@str = [20 x i8] c"test_fopen: entered\00"
@str.7 = [13 x i8] c"reading file\00"
@str.8 = [17 x i8] c"test_fopen: done\00"
define void @test_fopen(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0))
%12 = load i32, i32* @EOF
%13 = tail call i32 @fgetc(i32* %5)
%14 = icmp eq i32 %12, %13
br i1 %14, label %25, label %15
15: ; preds = %10, %21
%16 = phi i32 [ %23, %21 ], [ %13, %10 ]
%17 = icmp eq i32 %16, 13
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32, i32* @stdout
%20 = tail call i32 @putc(i32 %16, i32 %19)
br label %21
21: ; preds = %18, %15
%22 = load i32, i32* @EOF
%23 = tail call i32 @fgetc(i32* %5)
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %15
25: ; preds = %21, %10
%26 = tail call i32 @fclose(i32* %5)
%27 = load i32, i32* @stdout
%28 = call i8* @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = load i32, i32* @stdin
%32 = call i32 @fgets(i8* %3, i32 1024, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %47, label %34
34: ; preds = %25, %34
%35 = phi i32 [ %45, %34 ], [ %32, %25 ]
%36 = sext i32 %35 to i64
%37 = inttoptr i64 %36 to i8*
%38 = call i32 @strlen(i8* %37)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %37, i32 %38)
%40 = load i32, i32* @stdout
%41 = call i8* @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
%44 = load i32, i32* @stdin
%45 = call i32 @fgets(i8* %3, i32 1024, i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %34
47: ; preds = %34, %25
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.8, i64 0, i64 0))
ret void
}