#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 O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int main() {
  char buf[512];
  int fd;
  if ((fd = open("ta", O_CREAT | O_WRONLY, 644)) < 0) {
    printf("cannot open\n");
    return 1;
  }
  strcpy(buf, "Creating the file\n");
  if (write(fd, buf, strlen(buf)) <= 0) {
    printf("write");
    return 1;
  }
  close(fd);
  if ((fd = open("ta", O_RDWR | O_APPEND, 0)) < 0) {
    printf("cannot open file\n");
    return 1;
  }
  strcpy(buf, "Second line of file\n");
  if (write(fd, buf, strlen(buf)) <= 0) {
    printf("write 2");
    return 1;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
lea    0xe75(%rip),%rdi
mov    $0x284,%edx
call   1080 <open@plt>
mov    %eax,-0x214(%rbp)
cmp    $0x0,%eax
jge    11bc <main+0x5c>
lea    0xe58(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f7 <main+0x197>
lea    -0x210(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x214(%rbp),%eax
mov    %eax,-0x224(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x220(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x224(%rbp),%edi
mov    -0x220(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jg     122b <main+0xcb>
lea    0xe09(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f7 <main+0x197>
mov    -0x214(%rbp),%edi
call   1070 <close@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    0x2d7a(%rip),%rax
or     (%rax),%esi
lea    0xdb1(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x214(%rbp)
cmp    $0x0,%eax
jge    127d <main+0x11d>
lea    0xdbd(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f7 <main+0x197>
lea    -0x210(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x214(%rbp),%eax
mov    %eax,-0x234(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x230(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x234(%rbp),%edi
mov    -0x230(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jg     12ec <main+0x18c>
lea    0xd75(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f7 <main+0x197>
mov    -0x214(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    0x2e77(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6d(%rip),%rdi
mov    $0x284,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1235 <main+0xc5>
mov    %eax,%ebx
lea    0xe55(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %rax,%rax
jle    123e <main+0xce>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0x2e00(%rip),%rax
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe0e(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1247 <main+0xd7>
mov    %eax,%ebx
lea    0xe16(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %rax,%rax
jle    1255 <main+0xe5>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    1266 <main+0xf6>
lea    0xe0e(%rip),%rdi
jmp    124e <main+0xde>
lea    0xdd1(%rip),%rdi
jmp    125c <main+0xec>
lea    0xdeb(%rip),%rdi
call   1040 <puts@plt>
jmp    1261 <main+0xf1>
lea    0xdd5(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ta\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [13 x i8] c"cannot open\0A\00"
@.str.2 = [19 x i8] c"Creating the file\0A\00"
@.str.3 = [6 x i8] c"write\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.4 = [18 x i8] c"cannot open file\0A\00"
@.str.5 = [21 x i8] c"Second line of file\0A\00"
@.str.6 = [8 x i8] c"write 2\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [512 x i8]
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %6, i32 644)
store i32 %7, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %46
11: ; preds = %0
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%13 = call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* %3
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = call i64 @write(i32 %14, i8* %15, i32 %17)
%19 = icmp sle i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %46
22: ; preds = %11
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_APPEND
%27 = or i32 %25, %26
%28 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %27, i32 0)
store i32 %28, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %46
32: ; preds = %22
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32, i32* %3
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = call i64 @write(i32 %35, i8* %36, i32 %38)
%40 = icmp sle i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %46
43: ; preds = %32
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %43, %41, %30, %20, %9
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"ta\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [19 x i8] c"Creating the file\0A\00"
@.str.3 = [6 x i8] c"write\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.5 = [21 x i8] c"Second line of file\0A\00"
@.str.6 = [8 x i8] c"write 2\00"
@str = [17 x i8] c"cannot open file\00"
@str.7 = [12 x i8] c"cannot open\00"
define i32 @main() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %5, i32 644)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %35
10: ; preds = %0
%11 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @strlen(i8* %2)
%13 = call i64 @write(i32 %6, i8* %2, i32 %12)
%14 = icmp slt i64 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %35
17: ; preds = %10
%18 = call i32 @close(i32 %6)
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_APPEND
%21 = or i32 %20, %19
%22 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %35
26: ; preds = %17
%27 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @strlen(i8* %2)
%29 = call i64 @write(i32 %22, i8* %2, i32 %28)
%30 = icmp slt i64 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %35
33: ; preds = %26
%34 = call i32 @close(i32 %22)
br label %35
35: ; preds = %33, %31, %24, %15, %8
%36 = phi i32 [ 1, %8 ], [ 1, %15 ], [ 1, %24 ], [ 1, %31 ], [ 0, %33 ]
ret i32 %36
}