#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_RDWR;
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, int *, int);
int write(int, char *, int);
int main() {
  int fd = open("a.txt", O_RDWR | O_APPEND);
  if (-1 == fd)
    perror("read ");
  int buf[20];
  int len = write(fd, "my name is jsh ", 17);
  if (-1 == len || len != 17)
    perror("write ");
  int le = read(fd, buf, 15);
  if (le != 15)
    perror("read");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    118a <main+0x4a>
lea    0xe81(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
lea    0xe78(%rip),%rsi
mov    $0x11,%edx
call   1030 <write@plt>
mov    %eax,-0x64(%rbp)
mov    $0xffffffff,%eax
cmp    -0x64(%rbp),%eax
je     11b9 <main+0x79>
cmpl   $0x11,-0x64(%rbp)
je     11c5 <main+0x85>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0xf,%edx
call   1040 <read@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0xf,-0x68(%rbp)
je     11ef <main+0xaf>
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x50,%rsp
mov    0x2e9c(%rip),%rax
mov    0x2e7d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea2(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     119e <main+0x5e>
lea    0xe9b(%rip),%rsi
mov    %ebx,%edi
mov    $0x11,%edx
call   1030 <write@plt>
cmp    $0x11,%eax
jne    11ac <main+0x6c>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xf,%edx
call   1040 <read@plt>
cmp    $0xf,%eax
jne    11ba <main+0x7a>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
jmp    116a <main+0x2a>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
jmp    1182 <main+0x42>
lea    0xe62(%rip),%rdi
call   1060 <perror@plt>
jmp    1196 <main+0x56>

LLVM IR (-O0):
@.str = [6 x i8] c"a.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [6 x i8] c"read \00"
@.str.2 = [16 x i8] c"my name is jsh \00"
@.str.3 = [7 x i8] c"write \00"
@.str.4 = [5 x i8] c"read\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [20 x i32]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_APPEND
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 -1, %10
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = load i32, i32* %2
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 17)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 -1, %17
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 17
br i1 %21, label %22, label %24
22: ; preds = %19, %14
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = load i32, i32* %2
%26 = getelementptr inbounds [20 x i32], [20 x i32]* %3, i64 0, i64 0
%27 = call i32 @read(i32 %25, i32* %26, i32 15)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 15
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"a.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [6 x i8] c"read \00"
@.str.2 = [16 x i8] c"my name is jsh \00"
@.str.3 = [7 x i8] c"write \00"
@.str.4 = [5 x i8] c"read\00"
define i32 @main() {
%1 = alloca [20 x i32]
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_APPEND
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
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 %9
9: ; preds = %7, %0
%10 = bitcast [20 x i32]* %1 to i8*
%11 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 17)
%12 = icmp eq i32 %11, 17
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %9, %13
%16 = getelementptr inbounds [20 x i32], [20 x i32]* %1, i64 0, i64 0
%17 = call i32 @read(i32 %5, i32* %16, i32 15)
%18 = icmp eq i32 %17, 15
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
ret i32 0
}