#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 /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  char buf[] = "hello, world!\n";
  fd = open("data.txt", O_CREAT | O_WRONLY | O_TRUNC);
  if (fd == -1) {
    perror("open() failed");
    exit(EXIT_FAILURE);
  }
  printf("file descriptor %d\n", fd);
  if (write(fd, buf, sizeof(buf)) == -1)
    perror("write() failed");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe83(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe81(%rip),%eax
mov    %eax,-0x1b(%rbp)
mov    0xe7b(%rip),%ax
mov    %ax,-0x17(%rbp)
mov    0xe73(%rip),%al
mov    %al,-0x15(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ec <main+0x8c>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xe30(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x23(%rbp),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1221 <main+0xc1>
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movabs $0xa21646c726f77,%rax
mov    %rax,0x7(%rsp)
mov    0x2e7d(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movabs $0x77202c6f6c6c6568,%rax
2c 20 77
mov    %rax,(%rsp)
mov    0x2e55(%rip),%rax
or     (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11ef <main+0x8f>
mov    %eax,%ebx
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xf,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11e1 <main+0x81>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe52(%rip),%rdi
call   1070 <perror@plt>
jmp    11d2 <main+0x72>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.buf = [15 x i8] c"hello, world!\0A\00"
@.str = [9 x i8] c"data.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [14 x i8] c"open() failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [20 x i8] c"file descriptor %d\0A\00"
@.str.3 = [15 x i8] c"write() failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [15 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast [15 x i8]* %7 to i8*
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_WRONLY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i32, i32* %6
%23 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %6
%25 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%26 = call i32 @write(i32 %24, i8* %25, i32 15)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %21
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buf = [15 x i8] c"hello, world!\0A\00"
@.str = [9 x i8] c"data.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [14 x i8] c"open() failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [20 x i8] c"file descriptor %d\0A\00"
@.str.3 = [15 x i8] c"write() failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [15 x i8]
%4 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%18 = call i32 @write(i32 %10, i8* %4, i32 15)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = tail call i32 @close(i32 %10)
ret i32 0
}