#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 {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
int open(char *, int /*<<< orphan*/);
int open_config_file(char *config_file_name, int *out_fd, char **out_text,
                     size_t *out_sz) {
  int fd = -1;
  struct stat stat;
  char *text = NULL;
  size_t sz = 0;
  int saved_errno;
  fd = open(config_file_name, O_RDONLY);
  if (fd < 0)
    goto open_config_file_error;
  if (fstat(fd, &stat))
    goto open_config_file_error;
  sz = stat.st_size;
  text = mmap(0, sz, PROT_READ, MAP_PRIVATE, fd, 0);
  if (text == NULL)
    goto open_config_file_error;
  *out_fd = fd;
  *out_text = text;
  *out_sz = sz;
  return 0;
open_config_file_error:
  saved_errno = errno;
  if (text != NULL)
    munmap(text, sz);
  if (fd > 0)
    close(fd);
  errno = saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<open_config_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11a3 <open_config_file+0x53>
jmp    122a <open_config_file+0xda>
mov    -0x2c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     11be <open_config_file+0x6e>
jmp    122a <open_config_file+0xda>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    0x2dee(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11ff <open_config_file+0xaf>
jmp    122a <open_config_file+0xda>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1273 <open_config_file+0x123>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     124e <open_config_file+0xfe>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1050 <munmap@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    1260 <open_config_file+0x110>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x4c(%rbp),%ecx
mov    0x2d66(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_config_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     118d <open_config_file+0x4d>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %rax,%rax
je     11b5 <open_config_file+0x75>
mov    0x2e52(%rip),%rax
mov    (%rax),%ebx
test   %ebp,%ebp
je     1196 <open_config_file+0x56>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1196 <open_config_file+0x56>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e33(%rip),%rax
mov    %ebx,(%rax)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%r13
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r13,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
test   %rax,%rax
je     1177 <open_config_file+0x37>
mov    %ebp,(%r12)
mov    %rax,(%r15)
mov    %r13,(%r14)
jmp    11a4 <open_config_file+0x64>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@errno = global i32 0
define i32 @open_config_file(i8* %0, i32* %1, i8** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i32
%11 = alloca %struct.stat
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
store i8* %0, i8** %6
store i32* %1, i32** %7
store i8** %2, i8*** %8
store i64* %3, i64** %9
store i32 -1, i32* %10
store i8* null, i8** %12
store i64 0, i64* %13
%15 = load i8*, i8** %6
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
br label %44
21: ; preds = %4
%22 = load i32, i32* %10
%23 = call i64 @fstat(i32 %22, %struct.stat* %11)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
br label %44
26: ; preds = %21
%27 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %13
%29 = load i64, i64* %13
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @MAP_PRIVATE
%32 = load i32, i32* %10
%33 = call i8* @mmap(i32 0, i64 %29, i32 %30, i32 %31, i32 %32, i32 0)
store i8* %33, i8** %12
%34 = load i8*, i8** %12
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %37
36: ; preds = %26
br label %44
37: ; preds = %26
%38 = load i32, i32* %10
%39 = load i32*, i32** %7
store i32 %38, i32* %39
%40 = load i8*, i8** %12
%41 = load i8**, i8*** %8
store i8* %40, i8** %41
%42 = load i64, i64* %13
%43 = load i64*, i64** %9
store i64 %42, i64* %43
store i32 0, i32* %5
br label %60
44: ; preds = %36, %25, %20
%45 = load i32, i32* @errno
store i32 %45, i32* %14
%46 = load i8*, i8** %12
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i8*, i8** %12
%50 = load i64, i64* %13
%51 = call i32 @munmap(i8* %49, i64 %50)
br label %52
52: ; preds = %48, %44
%53 = load i32, i32* %10
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* %10
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55, %52
%59 = load i32, i32* %14
store i32 %59, i32* @errno
store i32 -1, i32* %5
br label %60
60: ; preds = %58, %37
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@errno = global i32 0
define i32 @open_config_file(i8* nocapture %0, i32* nocapture writeonly %1, i8** nocapture writeonly %2, i64* nocapture writeonly %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = load i32, i32* @errno
br label %28
12: ; preds = %4
%13 = call i64 @fstat(i32 %8, %struct.stat* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%17 = load i64, i64* %16
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_PRIVATE
%20 = tail call i8* @mmap(i32 0, i64 %17, i32 %18, i32 %19, i32 %8, i32 0)
%21 = icmp eq i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %15
store i32 %8, i32* %1
store i8* %20, i8** %2
store i64 %17, i64* %3
br label %30
23: ; preds = %15, %12
%24 = load i32, i32* @errno
%25 = icmp eq i32 %8, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @close(i32 %8)
br label %28
28: ; preds = %10, %26, %23
%29 = phi i32 [ %11, %10 ], [ %24, %26 ], [ %24, %23 ]
store i32 %29, i32* @errno
br label %30
30: ; preds = %28, %22
%31 = phi i32 [ -1, %28 ], [ 0, %22 ]
ret i32 %31
}