#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 */
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ svz_fd_cloexec(int);
int /*<<< orphan*/ svz_file_add(int);
int /*<<< orphan*/ svz_log(int, char *, char const *, int /*<<< orphan*/);
int svz_open(const char *file, int flags, unsigned int mode) {
  int fd;
  if ((fd = open(file, flags, (mode_t)mode)) < 0) {
    svz_log(1, "open (%s): %s\n", file, strerror(errno));
    return -1;
  }
  if (svz_fd_cloexec(fd) < 0) {
    close(fd);
    return -1;
  }
  svz_file_add(fd);
  return fd;
}


x86 assembly (clang, -O0):
<svz_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11c6 <svz_open+0x66>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xe4b(%rip),%rsi
call   1080 <svz_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <svz_open+0x9a>
mov    -0x1c(%rbp),%edi
call   1050 <svz_fd_cloexec@plt>
cmp    $0x0,%rax
jge    11ec <svz_open+0x8c>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <svz_open+0x9a>
mov    -0x1c(%rbp),%edi
call   1040 <svz_file_add@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<svz_open>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1060 <open@plt>
test   %eax,%eax
js     1187 <svz_open+0x27>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <svz_fd_cloexec@plt>
test   %rax,%rax
js     11ad <svz_open+0x4d>
mov    %ebx,%edi
call   1040 <svz_file_add@plt>
jmp    11b9 <svz_open+0x59>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe64(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
mov    %eax,%ecx
call   1080 <svz_log@plt>
jmp    11b4 <svz_open+0x54>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"open (%s): %s\0A\00"
@errno = global i32 0
define i32 @svz_open(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = load i32, i32* %7
%12 = call i32 @open(i8* %9, i32 %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @svz_log(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %17)
store i32 -1, i32* %4
br label %30
19: ; preds = %3
%20 = load i32, i32* %8
%21 = call i64 @svz_fd_cloexec(i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %8
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %4
br label %30
26: ; preds = %19
%27 = load i32, i32* %8
%28 = call i32 @svz_file_add(i32 %27)
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %30
30: ; preds = %26, %23, %14
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@.str = [15 x i8] c"open (%s): %s\0A\00"
@errno = global i32 0
define i32 @svz_open(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @open(i8* %0, i32 %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @svz_log(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %8)
br label %17
10: ; preds = %3
%11 = tail call i64 @svz_fd_cloexec(i32 %4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @close(i32 %4)
br label %17
15: ; preds = %10
%16 = tail call i32 @svz_file_add(i32 %4)
br label %17
17: ; preds = %15, %13, %6
%18 = phi i32 [ -1, %6 ], [ -1, %13 ], [ %4, %15 ]
ret i32 %18
}