#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_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int,
                           char *);
int open64(char const *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int open_file(const char *filename) {
  int fd = -1, ret = 0;
  int open_rw_flags = O_RDWR | O_CREAT;
  fd = open64(filename, open_rw_flags,
              (S_IRUSR | S_IWUSR | S_IXUSR | S_IROTH | S_IWOTH | S_IXOTH |
               S_IRGRP | S_IWGRP | S_IXGRP));
  if (fd < 0) {
    ret = errno;
    fprintf(stderr, "create file %s failed:%d:%s\n", filename, ret,
            strerror(ret));
  }
  ret = fd;
  return ret;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    0x2e9e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    0x2e4a(%rip),%rax
or     (%rax),%edx
mov    0x2e51(%rip),%rax
or     (%rax),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
mov    0x2de7(%rip),%rax
or     (%rax),%edx
mov    0x2e3e(%rip),%rax
or     (%rax),%edx
mov    0x2df5(%rip),%rax
or     (%rax),%edx
mov    0x2e1c(%rip),%rax
or     (%rax),%edx
mov    0x2e03(%rip),%rax
or     (%rax),%edx
call   1040 <open64@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1210 <open_file+0xe0>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%ecx
mov    %rax,%r8
lea    0xdf5(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ea2(%rip),%rax
mov    0x2eb3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e38(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6e(%rip),%rax
or     (%rax),%edx
mov    0x2e25(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    0x2e5b(%rip),%rax
or     (%rax),%edx
mov    0x2e12(%rip),%rax
or     (%rax),%edx
mov    0x2e39(%rip),%rax
or     (%rax),%edx
mov    %rdi,%rbx
mov    0x2e1d(%rip),%rax
or     (%rax),%edx
call   1040 <open64@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11dc <open_file+0xac>
mov    0x2df1(%rip),%rax
mov    (%rax),%r14d
mov    0x2def(%rip),%rax
mov    (%rax),%r15d
mov    %r14d,%edi
call   1050 <strerror@plt>
lea    0xe35(%rip),%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
mov    %r14d,%ecx
mov    %rax,%r8
call   1030 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IXOTH = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IXGRP = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"create file %s failed:%d:%s\0A\00"
define i32 @open_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
store i32 0, i32* %4
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
store i32 %8, i32* %5
%9 = load i8*, i8** %2
%10 = load i32, i32* %5
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IXUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWOTH
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IXOTH
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IWGRP
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IXGRP
%27 = or i32 %25, %26
%28 = call i32 @open64(i8* %9, i32 %10, i32 %27)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %1
%32 = load i32, i32* @errno
store i32 %32, i32* %4
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %2
%35 = load i32, i32* %4
%36 = load i32, i32* %4
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %34, i32 %35, i8* %37)
br label %39
39: ; preds = %31, %1
%40 = load i32, i32* %3
store i32 %40, i32* %4
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IXOTH = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IXGRP = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"create file %s failed:%d:%s\0A\00"
define i32 @open_file(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IXUSR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IWOTH
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IXOTH
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWGRP
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IXGRP
%21 = or i32 %19, %20
%22 = tail call i32 @open64(i8* %0, i32 %4, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %1
%25 = load i32, i32* @errno
%26 = load i32, i32* @stderr
%27 = tail call i8* @strerror(i32 %25)
%28 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %25, i8* %27)
br label %29
29: ; preds = %24, %1
ret i32 %22
}