#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 /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_APPEND;
int /*<<< orphan*/ close(int);
int creat(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  char *path = "./test.txt";
  int fd = creat(path, O_APPEND);
  if (fd != -1) {
    printf("creat file success!\n");
    char buf[] = "Hello World\n";
    write(fd, buf, (int)strlen(buf));
  } else {
    perror("creat error");
    exit(EXIT_FAILURE);
  }
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1080 <creat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     11fc <main+0x8c>
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
mov    0xe64(%rip),%rax
mov    %rax,-0x21(%rbp)
mov    0xe62(%rip),%eax
mov    %eax,-0x19(%rbp)
mov    0xe5d(%rip),%al
mov    %al,-0x15(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x21(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x21(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1216 <main+0xa6>
lea    0xe2a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1080 <creat@plt>
cmp    $0xffffffff,%eax
je     11ee <main+0x7e>
mov    %eax,%ebx
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
movabs $0xa646c726f5720,%rax
mov    %rax,0xd(%rsp)
movabs $0x6f57206f6c6c6548,%rax
20 57 6f
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"./test.txt\00"
@O_APPEND = global i32 0
@.str.1 = [21 x i8] c"creat file success!\0A\00"
@__const.main.buf = [13 x i8] c"Hello World\0A\00"
@.str.2 = [12 x i8] c"creat error\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [13 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_APPEND
%7 = call i32 @creat(i8* %5, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %19
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%12 = bitcast [13 x i8]* %4 to i8*
%13 = load i32, i32* %3
%14 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%15 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%16 = call i64 @strlen(i8* %15)
%17 = trunc i64 %16 to i32
%18 = call i32 @write(i32 %13, i8* %14, i32 %17)
br label %23
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %10
%24 = load i32, i32* %3
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @EXIT_SUCCESS
ret i32 %26
}

LLVM IR (-O2):
@.str = [11 x i8] c"./test.txt\00"
@O_APPEND = global i32 0
@__const.main.buf = [13 x i8] c"Hello World\0A\00"
@.str.2 = [12 x i8] c"creat error\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [20 x i8] c"creat file success!\00"
define i32 @main() {
%1 = alloca [13 x i8]
%2 = load i32, i32* @O_APPEND
%3 = tail call i32 @creat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %13, label %5
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%7 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%8 = call i64 @strlen(i8* %7)
%9 = trunc i64 %8 to i32
%10 = call i32 @write(i32 %3, i8* %7, i32 %9)
%11 = tail call i32 @close(i32 %3)
%12 = load i32, i32* @EXIT_SUCCESS
ret i32 %12
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
}