#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_APPEND;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
void cmd_open(int argc, char *argv[]) {
  int fd;
  fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, S_IRWXU);
  if (fd == -1)
    fd = open(argv[1], O_RDONLY);
  if (fd == -1)
    perror("open");
  else
    printf("open: %d\n", fd);
}


x86 assembly (clang, -O0):
<cmd_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2ea1(%rip),%rax
or     (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119a <cmd_open+0x6a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b5 <cmd_open+0x85>
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
jmp    11c4 <cmd_open+0x94>
mov    -0x14(%rbp),%esi
lea    0xe46(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_open>:
push   %rbx
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e99(%rip),%rax
mov    0x2eb2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    1181 <cmd_open+0x51>
mov    0x8(%rbx),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1190 <cmd_open+0x60>
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
pop    %rbx
jmp    1030 <printf@plt>
lea    0xe69(%rip),%rdi
pop    %rbx
jmp    1050 <perror@plt>

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

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRWXU = global i32 0
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [10 x i8] c"open: %d\0A\00"
define void @cmd_open(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = tail call i32 (i8*, i32, ...) @open(i8* %4, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 (i8*, i32, ...) @open(i8* %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %23
20: ; preds = %2, %13
%21 = phi i32 [ %16, %13 ], [ %11, %2 ]
%22 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %18
ret void
}