#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 */
scalar_t__ EACCES;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int chmod(char *, int /*<<< orphan*/);
int dup(int);
scalar_t__ errno;
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int fd = open("b.txt", O_CREAT | O_RDWR);
  if (fd == -1) {
    if (errno == EACCES) {
      if (chmod("b.txt", S_IRWXU) == -1) {
        perror("chmod");
        return errno;
      }
    }
    perror("open");
    return errno;
  }
  int newfd = dup(fd);
  if (newfd == -1) {
    perror("dup");
    return errno;
  }
  char *str = "hello world";
  write(newfd, str, strlen(str));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1213 <main+0xb3>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <main+0x95>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
call   1060 <chmod@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x90>
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    127d <main+0x11d>
jmp    11f5 <main+0x95>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    127d <main+0x11d>
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1246 <main+0xe6>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    127d <main+0x11d>
lea    0xdc8(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe83(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11ba <main+0x5a>
mov    %eax,%edi
call   1050 <dup@plt>
cmp    $0xffffffff,%eax
je     11f3 <main+0x93>
mov    %eax,%ebx
lea    0xe79(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dff(%rip),%rbx
mov    (%rbx),%rax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <main+0xaa>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe20(%rip),%rdi
call   1060 <chmod@plt>
cmp    $0xffffffff,%eax
jne    120a <main+0xaa>
lea    0xe15(%rip),%rdi
jmp    1211 <main+0xb1>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
jmp    11b2 <main+0x52>
lea    0xdfb(%rip),%rdi
call   1080 <perror@plt>
mov    (%rbx),%eax
jmp    11b2 <main+0x52>

LLVM IR (-O0):
@.str = [6 x i8] c"b.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@S_IRWXU = global i32 0
@.str.1 = [6 x i8] c"chmod\00"
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [4 x i8] c"dup\00"
@.str.4 = [12 x i8] c"hello world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %28
11: ; preds = %0
%12 = load i64, i64* @errno
%13 = load i64, i64* @EACCES
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = load i32, i32* @S_IRWXU
%17 = call i32 @chmod(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = load i64, i64* @errno
%22 = trunc i64 %21 to i32
store i32 %22, i32* %1
br label %43
23: ; preds = %15
br label %24
24: ; preds = %23, %11
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = load i64, i64* @errno
%27 = trunc i64 %26 to i32
store i32 %27, i32* %1
br label %43
28: ; preds = %0
%29 = load i32, i32* %2
%30 = call i32 @dup(i32 %29)
store i32 %30, i32* %3
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%35 = load i64, i64* @errno
%36 = trunc i64 %35 to i32
store i32 %36, i32* %1
br label %43
37: ; preds = %28
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8** %4
%38 = load i32, i32* %3
%39 = load i8*, i8** %4
%40 = load i8*, i8** %4
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @write(i32 %38, i8* %39, i32 %41)
store i32 0, i32* %1
br label %43
43: ; preds = %37, %33, %24, %19
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [6 x i8] c"b.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@S_IRWXU = global i32 0
@.str.1 = [6 x i8] c"chmod\00"
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [4 x i8] c"dup\00"
@.str.4 = [12 x i8] c"hello world\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_RDWR
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %22
6: ; preds = %0
%7 = load i64, i64* @errno
%8 = load i64, i64* @EACCES
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %18
10: ; preds = %6
%11 = load i32, i32* @S_IRWXU
%12 = tail call i32 @chmod(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = load i64, i64* @errno
%17 = trunc i64 %16 to i32
br label %32
18: ; preds = %10, %6
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = load i64, i64* @errno
%21 = trunc i64 %20 to i32
br label %32
22: ; preds = %0
%23 = tail call i32 @dup(i32 %4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%27 = load i64, i64* @errno
%28 = trunc i64 %27 to i32
br label %32
29: ; preds = %22
%30 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %25, %29, %18, %14
%33 = phi i32 [ %17, %14 ], [ %21, %18 ], [ %28, %25 ], [ 0, %29 ]
ret i32 %33
}