#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*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  FILE *fd;
  char str[100];
  fd = fopen("hello.txt", "w+");
  if (fd) {
    fputs("Hello World", fd);
    fclose(fd);
  }
  fd = fopen("hello.txt", "r");
  fgets(str, 100, fd);
  printf("%s\n", str);
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119d <main+0x4d>
mov    -0x10(%rbp),%rsi
lea    0xe7e(%rip),%rdi
call   1050 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe5c(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    -0x80(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0xea2(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1193 <main+0x43>
mov    %rax,%rbx
lea    0xe94(%rip),%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe66(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x64,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
lea    0xe58(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"hello.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [12 x i8] c"Hello World\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [100 x i8]
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32*, i32** %2
%9 = call i32 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%10 = load i32*, i32** %2
%11 = call i32 @fclose(i32* %10)
br label %12
12: ; preds = %7, %0
%13 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %13, i32** %2
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%15 = load i32*, i32** %2
%16 = call i32 @fgets(i8* %14, i32 100, i32* %15)
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %17)
%19 = load i32*, i32** %2
%20 = call i32 @fclose(i32* %19)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"hello.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [12 x i8] c"Hello World\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = tail call i64 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 11, i64 1, i32* %3)
%7 = tail call i32 @fclose(i32* %3)
br label %8
8: ; preds = %5, %0
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%10 = call i32 @fgets(i8* %2, i32 100, i32* %9)
%11 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%12 = call i32 @fclose(i32* %9)
ret i32 0
}