#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 /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ umask(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  umask(0777);
  fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0777);
  if (fd < 0) {
    perror("open()");
    return -1;
  }
  printf("fd=%d\n", fd);
  char *p = "hello world";
  write(fd, p, 11);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1ff,%edi
call   1060 <umask@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d2 <main+0x72>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <main+0xac>
mov    -0x14(%rbp),%esi
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
lea    0xe26(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
mov    $0x1ff,%edi
call   1060 <umask@plt>
mov    0x2e53(%rip),%rax
mov    0x2e7c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2e65(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11c9 <main+0x69>
mov    %eax,%ebx
lea    0xe63(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xe5c(%rip),%rsi
mov    %ebx,%edi
mov    $0xb,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe30(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [7 x i8] c"open()\00"
@.str.1 = [7 x i8] c"fd=%d\0A\00"
@.str.2 = [12 x i8] c"hello world\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @umask(i32 511)
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %5, i32 %10, i32 511)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
15: ; preds = %2
%16 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%17 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%18 = tail call i32 @close(i32 %11)
br label %19
19: ; preds = %15, %13
%20 = phi i32 [ -1, %13 ], [ 0, %15 ]
ret i32 %20
}