#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_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int errno;
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int write(int, char *, int);
int main(void) {
  int fd, n;
  if ((fd = open("hello.txt", O_TRUNC | O_CREAT | O_WRONLY,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
    printf("open error with errno = %d\n", errno);
    perror("hello.txt");
    exit(1);
  }
  printf("%d\n", fd);
  if ((n = write(fd, "hello world\n", 12)) == -1) {
    printf("write error with errno = %d\n", errno);
    perror("hello.txt");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    0x2e4e(%rip),%rax
or     (%rax),%edx
mov    0x2e2d(%rip),%rax
or     (%rax),%edx
mov    0x2e1c(%rip),%rax
or     (%rax),%edx
lea    0xe5b(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11e1 <main+0x91>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xe2c(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    123b <main+0xeb>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
lea    0xdd4(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e98(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    0x2e3d(%rip),%rax
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3b(%rip),%rax
or     (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
lea    0xe69(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11cd <main+0x7d>
mov    %eax,%ebx
lea    0xea2(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xe6e(%rip),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11df <main+0x8f>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
jmp    11ef <main+0x9f>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [10 x i8] c"hello.txt\00"
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [13 x i8] c"hello world\0A\00"
@.str.4 = [29 x i8] c"write error with errno = %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_TRUNC
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%22 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
ret i32 0
}