#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 */
scalar_t__ EOF;
int /*<<< orphan*/ do_stuff();
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  FILE *logfile = fopen("log", "a");
  if (logfile == NULL) {
    printf("Could not open file\n");
  }
  fprintf(logfile, "main\n");
  do_stuff();
  if (fclose(logfile) == EOF) {
    printf("Could not close file\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118d <main+0x3d>
lea    0xe7e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1070 <do_stuff@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    11c9 <main+0x79>
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1178 <main+0x28>
lea    0xeae(%rip),%rdi
call   1030 <puts@plt>
lea    0xe87(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
xor    %eax,%eax
call   1070 <do_stuff@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
jne    11b8 <main+0x68>
lea    0xe59(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"log\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [21 x i8] c"Could not open file\0A\00"
@.str.3 = [6 x i8] c"main\0A\00"
@EOF = global i64 0
@.str.4 = [22 x i8] c"Could not close file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
%3 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %2
%4 = load i32*, i32** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32*, i32** %2
%10 = call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%11 = call i32 (...) @do_stuff()
%12 = load i32*, i32** %2
%13 = call i64 @fclose(i32* %12)
%14 = load i64, i64* @EOF
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %16, %8
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"log\00"
@.str.1 = [2 x i8] c"a\00"
@.str.3 = [6 x i8] c"main\0A\00"
@EOF = global i64 0
@str = [21 x i8] c"Could not close file\00"
@str.5 = [20 x i8] c"Could not open file\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = tail call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 5, i64 1, i32* %1)
%7 = tail call i32 (...) @do_stuff()
%8 = tail call i64 @fclose(i32* %1)
%9 = load i64, i64* @EOF
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %11, %5
ret i32 0
}