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

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error_handling(char *);
int open(char *, int);
int read(int, char *, int);
int write(int, char *, int);
int main() {
  int fdin;
  int fdout;
  char buf[100];
  fdin = open("data.txt", O_RDONLY);
  if (fdin == -1) {
    error_handling("open1() error");
  }
  fdout = open("dataCopy.txt", O_CREAT | O_APPEND | O_WRONLY);
  if (fdout == -1) {
    error_handling("open2() error");
  }
  if (read(fdin, buf, sizeof(buf)) == -1) {
    error_handling("read() error");
  }
  if (write(fdout, buf, sizeof(buf)) == -1) {
    error_handling("write() error");
  }
  close(fdout);
  close(fdin);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    118d <main+0x3d>
lea    0xe81(%rip),%rdi
call   1060 <error_handling@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
or     (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
lea    0xe68(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11cd <main+0x7d>
lea    0xe5c(%rip),%rdi
call   1060 <error_handling@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11f3 <main+0xa3>
lea    0xe44(%rip),%rdi
call   1060 <error_handling@plt>
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1219 <main+0xc9>
lea    0xe2b(%rip),%rdi
call   1060 <error_handling@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    117e <main+0x2e>
lea    0xe90(%rip),%rdi
call   1060 <error_handling@plt>
mov    0x2e73(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
lea    0xe77(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11b8 <main+0x68>
lea    0xe71(%rip),%rdi
call   1060 <error_handling@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11d8 <main+0x88>
lea    0xe5f(%rip),%rdi
call   1060 <error_handling@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x64,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11f8 <main+0xa8>
lea    0xe4c(%rip),%rdi
call   1060 <error_handling@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"data.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"open1() error\00"
@.str.2 = [13 x i8] c"dataCopy.txt\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [14 x i8] c"open2() error\00"
@.str.4 = [13 x i8] c"read() error\00"
@.str.5 = [14 x i8] c"write() error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @error_handling(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_APPEND
%14 = or i32 %12, %13
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %14, %15
%17 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @error_handling(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %11
%23 = load i32, i32* %2
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 100)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @error_handling(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = load i32, i32* %3
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%32 = call i32 @write(i32 %30, i8* %31, i32 100)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @error_handling(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"data.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"open1() error\00"
@.str.2 = [13 x i8] c"dataCopy.txt\00"
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [14 x i8] c"open2() error\00"
@.str.4 = [13 x i8] c"read() error\00"
@.str.5 = [14 x i8] c"write() error\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @error_handling(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_APPEND
%11 = or i32 %10, %9
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @error_handling(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %8
%19 = call i32 @read(i32 %4, i8* %2, i32 100)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @error_handling(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = call i32 @write(i32 %14, i8* %2, i32 100)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 @error_handling(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = tail call i32 @close(i32 %14)
%30 = tail call i32 @close(i32 %4)
ret i32 0
}