#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, char *, char *,
                           int);
int /*<<< orphan*/ printf(char *, int);
void fopen_t() {
  FILE *fp;
  fp = fopen("file.txt", "w+");
  fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
  fclose(fp);
  int c;
  fp = fopen("file.txt", "r");
  while (1) {
    c = fgetc(fp);
    if (feof(fp)) {
      break;
    }
    printf("%c", c);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<fopen_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
lea    0xe87(%rip),%rdx
lea    0xe83(%rip),%rcx
lea    0xe80(%rip),%r8
mov    $0x7dc,%r9d
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe46(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     11ee <fopen_t+0x8e>
jmp    1202 <fopen_t+0xa2>
mov    -0xc(%rbp),%esi
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
jmp    11ca <fopen_t+0x6a>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_t>:
push   %rbp
push   %r14
push   %rbx
lea    0xe95(%rip),%rbx
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbp
lea    0xe88(%rip),%rsi
lea    0xe8d(%rip),%rdx
lea    0xe89(%rip),%rcx
lea    0xe86(%rip),%r8
mov    %rax,%rdi
mov    $0x7dc,%r9d
call   1060 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe6c(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    1201 <fopen_t+0xa1>
lea    0xe45(%rip),%r14
nop
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     11e0 <fopen_t+0x80>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [12 x i8] c"%s %s %s %d\00"
@.str.3 = [3 x i8] c"We\00"
@.str.4 = [4 x i8] c"are\00"
@.str.5 = [3 x i8] c"in\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [3 x i8] c"%c\00"
define void @fopen_t() {
%1 = alloca i32*
%2 = alloca i32
%3 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %1
%4 = load i32*, i32** %1
%5 = call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2012)
%6 = load i32*, i32** %1
%7 = call i32 @fclose(i32* %6)
%8 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %8, i32** %1
br label %9
9: ; preds = %0, %16
%10 = load i32*, i32** %1
%11 = call i32 @fgetc(i32* %10)
store i32 %11, i32* %2
%12 = load i32*, i32** %1
%13 = call i64 @feof(i32* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %9
br label %19
16: ; preds = %9
%17 = load i32, i32* %2
%18 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 %17)
br label %9
19: ; preds = %15
%20 = load i32*, i32** %1
%21 = call i32 @fclose(i32* %20)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [12 x i8] c"%s %s %s %d\00"
@.str.3 = [3 x i8] c"We\00"
@.str.4 = [4 x i8] c"are\00"
@.str.5 = [3 x i8] c"in\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [3 x i8] c"%c\00"
define void @fopen_t() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2012)
%3 = tail call i32 @fclose(i32* %1)
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%5 = tail call i32 @fgetc(i32* %4)
%6 = tail call i64 @feof(i32* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %0, %8
%9 = phi i32 [ %11, %8 ], [ %5, %0 ]
%10 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 %9)
%11 = tail call i32 @fgetc(i32* %4)
%12 = tail call i64 @feof(i32* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %8, label %14
14: ; preds = %8, %0
%15 = tail call i32 @fclose(i32* %4)
ret void
}