#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_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ write(int, char *, int);
void main() {
  int fd;
  fd = open("foo", O_WRONLY | O_CREAT, 0644);
  if (fd < 0) {
    printf("error number %d\n", errno);
    perror("foo");
    exit(1);
  }
  write(fd, "hello world", 11);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11c3 <main+0x63>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
lea    0xe4c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
lea    0xe48(%rip),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e60(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe86(%rip),%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11a5 <main+0x45>
mov    %eax,%ebx
lea    0xe84(%rip),%rsi
mov    %eax,%edi
mov    $0xb,%edx
call   1030 <write@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <close@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [17 x i8] c"error number %d\0A\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"hello world\00"
define void @main() {
%1 = alloca i32
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %2, %3
%5 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %4, i32 420)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %1
%15 = call i32 @write(i32 %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%16 = load i32, i32* %1
%17 = call i32 @close(i32 %16)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [17 x i8] c"error number %d\0A\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"hello world\00"
define void @main() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3, i32 420)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%13 = tail call i32 @close(i32 %4)
ret void
}