#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 */

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main() {
  int fd = open("./datafile", O_CREAT | O_APPEND);
  if (-1 == fd) {
    fprintf(stderr, "Error[%d], ", errno);
    perror(NULL);
  }
  char buf[1024] = {'\0'};
  int count = read(fd, buf, 1024);
  if (count) {
    fprintf(stdout, "%s", buf);
  }
  close(fd);
  return fd;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11ca <main+0x6a>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
je     1222 <main+0xc2>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    0x2e87(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11e2 <main+0x82>
mov    %rsp,%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11ce <main+0x6e>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    0xe10(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
jmp    118f <main+0x2f>

LLVM IR (-O0):
@.str = [11 x i8] c"./datafile\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"Error[%d], \00"
@errno = global i32 0
@stdout = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_APPEND
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 @perror(i32* null)
br label %16
16: ; preds = %11, %0
%17 = bitcast [1024 x i8]* %3 to i8*
%18 = load i32, i32* %2
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 1024)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i32, i32* @stdout
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %23, %16
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str = [11 x i8] c"./datafile\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"Error[%d], \00"
@errno = global i32 0
@stdout = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_APPEND
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i32 @perror(i32* null)
br label %12
12: ; preds = %7, %0
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%14 = call i32 @read(i32 %5, i8* %13, i32 1024)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = load i32, i32* @stdout
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %19
19: ; preds = %16, %12
%20 = call i32 @close(i32 %5)
ret i32 %5
}