#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_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int);
int write(int, char *, int);
int main() {
  int fildes = open("temp.file", O_WRONLY | O_APPEND);
  if (fildes == -1) {
    write(2, "An error occurs when open temp.file\n", 100);
  } else if (write(fildes, "1234567890\n", 11) != 11) {
    write(2, "error when write into it\n", 100);
  }
  close(fildes);
  write(1, "done", 4);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
lea    0xea8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1185 <main+0x55>
mov    $0x2,%edi
lea    0xe94(%rip),%rsi
mov    $0x64,%edx
call   1030 <write@plt>
jmp    11bd <main+0x8d>
mov    -0x8(%rbp),%edi
lea    0xea0(%rip),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
cmp    $0xb,%eax
je     11b8 <main+0x88>
mov    $0x2,%edi
lea    0xe8d(%rip),%rsi
mov    $0x64,%edx
call   1030 <write@plt>
jmp    11bd <main+0x8d>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
lea    0xe84(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1177 <main+0x47>
lea    0xed2(%rip),%rsi
mov    %ebx,%edi
mov    $0xb,%edx
call   1030 <write@plt>
cmp    $0xb,%eax
je     118d <main+0x5d>
lea    0xec6(%rip),%rsi
jmp    117e <main+0x4e>
lea    0xe8c(%rip),%rsi
mov    $0x2,%edi
mov    $0x64,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xeba(%rip),%rsi
mov    $0x1,%edi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"temp.file\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [37 x i8] c"An error occurs when open temp.file\0A\00"
@.str.2 = [12 x i8] c"1234567890\0A\00"
@.str.3 = [26 x i8] c"error when write into it\0A\00"
@.str.4 = [5 x i8] c"done\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_APPEND
%5 = or i32 %3, %4
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 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 @write(i32 2, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 100)
br label %18
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 @write(i32 %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%14 = icmp ne i32 %13, 11
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @write(i32 2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 100)
br label %17
17: ; preds = %15, %11
br label %18
18: ; preds = %17, %9
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
%21 = call i32 @write(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"temp.file\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [37 x i8] c"An error occurs when open temp.file\0A\00"
@.str.2 = [12 x i8] c"1234567890\0A\00"
@.str.3 = [26 x i8] c"error when write into it\0A\00"
@.str.4 = [5 x i8] c"done\00"
define i32 @main() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_APPEND
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%8 = icmp eq i32 %7, 11
br i1 %8, label %12, label %9
9: ; preds = %6, %0
%10 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), %6 ]
%11 = tail call i32 @write(i32 2, i8* %10, i32 100)
br label %12
12: ; preds = %9, %6
%13 = tail call i32 @close(i32 %4)
%14 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
ret i32 0
}