#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
int tryopen(const char *path, int *size, void **map) {
  int fd;
  struct stat s;
  if (stat(path, &s)) {
    perror("stat()");
    return -1;
  }
  if ((fd = open(path, O_RDWR)) == -1) {
    perror("open()");
    return -2;
  }
  *size = s.st_size;
  *map = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (*map == MAP_FAILED) {
    close(fd);
    perror("mmap()");
    return -3;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tryopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1193 <tryopen+0x43>
lea    0xe7e(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <tryopen+0xfa>
mov    -0x10(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11c9 <tryopen+0x79>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    124a <tryopen+0xfa>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    0x2de0(%rip),%rax
or     (%rax),%edx
mov    0x2df7(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%rax
jne    1244 <tryopen+0xf4>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
lea    0xddb(%rip),%rdi
call   1070 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    124a <tryopen+0xfa>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tryopen>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    11d3 <tryopen+0x83>
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e1 <tryopen+0x91>
mov    %eax,%ebx
mov    0x8(%rsp),%esi
mov    %esi,(%r15)
mov    0x2e61(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3f(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
je     11ef <tryopen+0x9f>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    $0xffffffff,%ebx
lea    0xe21(%rip),%rdi
jmp    1202 <tryopen+0xb2>
mov    $0xfffffffe,%ebx
lea    0xe1a(%rip),%rdi
jmp    1202 <tryopen+0xb2>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xfffffffd,%ebx
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
jmp    11c7 <tryopen+0x77>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"stat()\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [7 x i8] c"mmap()\00"
define i32 @tryopen(i8* %0, i32* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca %struct.stat
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8** %2, i8*** %7
%10 = load i8*, i8** %5
%11 = call i64 @stat(i8* %10, %struct.stat* %9)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %45
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %4
br label %45
22: ; preds = %15
%23 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32*, i32** %6
store i32 %24, i32* %25
%26 = load i32*, i32** %6
%27 = load i32, i32* %26
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @PROT_WRITE
%30 = or i32 %28, %29
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* %8
%33 = call i8* @mmap(i32* null, i32 %27, i32 %30, i32 %31, i32 %32, i32 0)
%34 = load i8**, i8*** %7
store i8* %33, i8** %34
%35 = load i8**, i8*** %7
%36 = load i8*, i8** %35
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %43
39: ; preds = %22
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
%42 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %4
br label %45
43: ; preds = %22
%44 = load i32, i32* %8
store i32 %44, i32* %4
br label %45
45: ; preds = %43, %39, %20, %13
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"stat()\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [7 x i8] c"mmap()\00"
define i32 @tryopen(i8* %0, i32* nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %24
8: ; preds = %3
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %24, label %12
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i32, i32* %13
store i32 %14, i32* %1
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = call i8* @mmap(i32* null, i32 %14, i32 %17, i32 %18, i32 %10, i32 0)
store i8* %19, i8** %2
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %28
22: ; preds = %12
%23 = call i32 @close(i32 %10)
br label %24
24: ; preds = %8, %3, %22
%25 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %22 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %8 ]
%26 = phi i32 [ -3, %22 ], [ -1, %3 ], [ -2, %8 ]
%27 = call i32 @perror(i8* %25)
br label %28
28: ; preds = %24, %12
%29 = phi i32 [ %10, %12 ], [ %26, %24 ]
ret i32 %29
}