#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;
};
struct shm_log {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
struct shm_log *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int S_IRUSR;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fstat(int, struct stat *);
struct shm_log *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/,
                     int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int shm_open(char *, int /*<<< orphan*/, int);
struct shm_log *open_log(size_t *mem_size) {
  int fd;
  struct stat info;
  struct shm_log *addr;
  fd = shm_open("/addrwatch-shm-log", O_RDONLY, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    perror("shm_open");
    exit(EXIT_FAILURE);
  }
  if (fstat(fd, &info) == -1) {
    perror("fstat");
    exit(EXIT_FAILURE);
  }
  *mem_size = info.st_size;
  addr = mmap(NULL, *mem_size, PROT_READ, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    exit(EXIT_FAILURE);
  }
  if (close(fd) == -1) {
    perror("close");
    exit(EXIT_FAILURE);
  }
  return addr;
}


x86 assembly (clang, -O0):
<open_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    0x2e4b(%rip),%rax
or     (%rax),%edx
lea    0xe72(%rip),%rdi
call   1070 <shm_open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ba <open_log+0x5a>
lea    0xe6c(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11e9 <open_log+0x89>
lea    0xe46(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
jne    124f <open_log+0xef>
lea    0xde6(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    127a <open_log+0x11a>
lea    0xdc0(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_log>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe77(%rip),%rdi
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     11f0 <open_log+0x90>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11f9 <open_log+0x99>
mov    (%rsp),%rsi
mov    %rsi,(%r14)
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r14
mov    0x2ddc(%rip),%rax
cmp    (%rax),%r14
je     1202 <open_log+0xa2>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     120b <open_log+0xab>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1c(%rip),%rdi
jmp    1212 <open_log+0xb2>
lea    0xe1c(%rip),%rdi
jmp    1212 <open_log+0xb2>
lea    0xe19(%rip),%rdi
jmp    1212 <open_log+0xb2>
lea    0xe15(%rip),%rdi
call   1050 <perror@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
%struct.shm_log = type { i32 }
%struct.stat = type { i64 }
@.str = [19 x i8] c"/addrwatch-shm-log\00"
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [9 x i8] c"shm_open\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global %struct.shm_log* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [6 x i8] c"close\00"
define %struct.shm_log* @open_log(i64* nocapture writeonly %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %6, %5
%8 = tail call i32 @shm_open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
%15 = call i32 @fstat(i32 %8, %struct.stat* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %14
%22 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%23 = load i64, i64* %22
store i64 %23, i64* %0
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @MAP_SHARED
%26 = tail call %struct.shm_log* @mmap(i32* null, i64 %23, i32 %24, i32 %25, i32 %8, i32 0)
%27 = load %struct.shm_log*, %struct.shm_log** @MAP_FAILED
%28 = icmp eq %struct.shm_log* %26, %27
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %21
%34 = tail call i32 @close(i32 %8)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %33
ret %struct.shm_log* %26
}