#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
scalar_t__ stat(char *, struct stat *);
scalar_t__ unlink(char *);
int main(void) {
  FILE *fp;
  char file[] = "foobar";
  struct stat st;
  if ((fp = fopen(file, "a")) == NULL) {
    perror("fopen");
    return EXIT_FAILURE;
  }
  fprintf(fp, "Hello World\n");
  fflush(fp);
  fclose(fp);
  printf("Waiting 2 seconds...\n");
  sleep(2);
  if (unlink(file) < 0) {
    perror("unlink");
  }
  if (stat(file, &st) < 0) {
    printf("File no longer exists.\n");
    return EXIT_FAILURE;
  } else {
    printf("File is still here!\n");
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe5b(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xe55(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe4d(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0x17(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11f7 <main+0x67>
lea    0xe28(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129a <main+0x10a>
mov    -0x10(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdfc(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
lea    -0x17(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    124e <main+0xbe>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
lea    -0x17(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    1282 <main+0xf2>
lea    0xdcd(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129a <main+0x10a>
lea    0xdc8(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x726162,0x3(%rsp)
movl   $0x626f6f66,(%rsp)
lea    0xe5c(%rip),%rsi
mov    %rsp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     124d <main+0xbd>
mov    %rax,%rbx
lea    0xe49(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe34(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
js     125b <main+0xcb>
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     1232 <main+0xa2>
lea    0xe14(%rip),%rdi
call   1040 <puts@plt>
mov    0x2da8(%rip),%rax
jmp    1245 <main+0xb5>
lea    0xe13(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
jmp    123e <main+0xae>
lea    0xdba(%rip),%rdi
call   1090 <perror@plt>
jmp    120b <main+0x7b>

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.main.file = [7 x i8] c"foobar\00"
@.str = [2 x i8] c"a\00"
@.str.1 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"Hello World\0A\00"
@.str.3 = [22 x i8] c"Waiting 2 seconds...\0A\00"
@.str.4 = [7 x i8] c"unlink\00"
@.str.5 = [24 x i8] c"File no longer exists.\0A\00"
@.str.6 = [21 x i8] c"File is still here!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [7 x i8]
%4 = alloca %struct.stat
store i32 0, i32* %1
%5 = bitcast [7 x i8]* %3 to i8*
%6 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
store i32 %11, i32* %1
br label %37
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32*, i32** %2
%16 = call i32 @fflush(i32* %15)
%17 = load i32*, i32** %2
%18 = call i32 @fclose(i32* %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @sleep(i32 2)
%21 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%22 = call i64 @unlink(i8* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %12
%27 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%28 = call i64 @stat(i8* %27, %struct.stat* %4)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
store i32 %32, i32* %1
br label %37
33: ; preds = %26
%34 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %33
%36 = load i32, i32* @EXIT_SUCCESS
store i32 %36, i32* %1
br label %37
37: ; preds = %35, %30, %9
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.main.file = [7 x i8] c"foobar\00"
@.str = [2 x i8] c"a\00"
@.str.1 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"Hello World\0A\00"
@.str.4 = [7 x i8] c"unlink\00"
@EXIT_SUCCESS = global i32 0
@str = [21 x i8] c"Waiting 2 seconds...\00"
@str.7 = [20 x i8] c"File is still here!\00"
@str.8 = [23 x i8] c"File no longer exists.\00"
define i32 @main() {
%1 = alloca [7 x i8]
%2 = alloca %struct.stat
%3 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %26
9: ; preds = %0
%10 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 12, i64 1, i32* %5)
%11 = tail call i32 @fflush(i32* %5)
%12 = tail call i32 @fclose(i32* %5)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @sleep(i32 2)
%15 = call i64 @unlink(i8* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %17, %9
%20 = call i64 @stat(i8* %3, %struct.stat* %2)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
br label %26
24: ; preds = %19
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
br label %26
26: ; preds = %24, %22, %7
%27 = phi i32* [ @EXIT_FAILURE, %7 ], [ @EXIT_FAILURE, %22 ], [ @EXIT_SUCCESS, %24 ]
%28 = load i32, i32* %27
ret i32 %28
}