#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int xopen(char *file, int flag, int chmod) {
  int fd;
  fd = 0;
  if ((chmod != -1) && file) {
    if ((fd = open(file, flag, chmod)) == -1)
      fprintf(stderr, "42sh: %s: %s\n", file, strerror(errno));
  } else if (file)
    if ((fd = open(file, flag)) == -1)
      fprintf(stderr, "42sh: error: no path to open\n");
  return (fd);
}


x86 assembly (clang, -O0):
<xopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     11ba <xopen+0x8a>
cmpq   $0x0,-0x8(%rbp)
je     11ba <xopen+0x8a>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11b5 <xopen+0x85>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1200 <xopen+0xd0>
cmpq   $0x0,-0x8(%rbp)
je     11fb <xopen+0xcb>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11f6 <xopen+0xc6>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    11fb <xopen+0xcb>
jmp    1200 <xopen+0xd0>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xopen>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
je     117e <xopen+0x4e>
cmp    $0xffffffff,%edx
je     117e <xopen+0x4e>
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    11b2 <xopen+0x82>
mov    0x2e8a(%rip),%rax
mov    (%rax),%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe93(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11a9 <xopen+0x79>
test   %rbx,%rbx
je     11b0 <xopen+0x80>
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    11b2 <xopen+0x82>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11b2 <xopen+0x82>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"42sh: %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [30 x i8] c"42sh: error: no path to open\0A\00"
define i32 @xopen(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* %6
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %26
10: ; preds = %3
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = call i32 (i8*, i32, ...) @open(i8* %14, i32 %15, i32 %16)
store i32 %17, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %4
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %23)
br label %25
25: ; preds = %19, %13
br label %39
26: ; preds = %10, %3
%27 = load i8*, i8** %4
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = call i32 (i8*, i32, ...) @open(i8* %30, i32 %31)
store i32 %32, i32* %7
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %34, %29
br label %38
38: ; preds = %37, %26
br label %39
39: ; preds = %38, %25
%40 = load i32, i32* %7
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"42sh: %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [30 x i8] c"42sh: error: no path to open\0A\00"
define i32 @xopen(i8* %0, i32 %1, i32 %2) {
%4 = icmp ne i32 %2, -1
%5 = icmp ne i8* %0, null
%6 = and i1 %5, %4
br i1 %6, label %7, label %15
7: ; preds = %3
%8 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %1, i32 %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %22
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %13)
br label %22
15: ; preds = %3
br i1 %5, label %16, label %22
16: ; preds = %15
%17 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %15, %19, %16, %7, %10
%23 = phi i32 [ -1, %10 ], [ %8, %7 ], [ -1, %19 ], [ %17, %16 ], [ 0, %15 ]
ret i32 %23
}