#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 PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int open(char *, int);
int main(int argc, char *argv[]) {
  int fd;
  void *a;
  fd = open(argv[1], O_RDWR | O_CREAT);
  if (fd < 0)
    error("Failed to open file", errno);
  a = mmap(NULL, (64 * 1024), PROT_READ | PROT_WRITE, 0x03 | 0x80000, fd, 0);
  if ((unsigned long)a == -1)
    error("Failed to mmap ", errno);
  *(int *)a = 10;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e9a(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1187 <main+0x57>
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0xe7e(%rip),%rdi
call   1040 <error@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10000,%esi
mov    $0x80003,%ecx
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    $0xffffffffffffffff,%rax
jne    11da <main+0xaa>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe3f(%rip),%rdi
call   1040 <error@plt>
mov    -0x20(%rbp),%rax
movl   $0xa,(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x8(%rsi),%rdi
mov    0x2e9c(%rip),%rax
mov    0x2eb5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1167 <main+0x37>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1040 <error@plt>
mov    0x2e82(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %edi,%edi
mov    $0x10000,%esi
mov    $0x80003,%ecx
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    11ae <main+0x7e>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1040 <error@plt>
movl   $0xa,(%rbx)
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [20 x i8] c"Failed to open file\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@.str.1 = [16 x i8] c"Failed to mmap \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %2
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* %6
%25 = call i8* @mmap(i32* null, i32 65536, i32 %23, i32 524291, i32 %24, i32 0)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = ptrtoint i8* %26 to i64
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i32, i32* @errno
%31 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %29, %20
%33 = load i8*, i8** %7
%34 = bitcast i8* %33 to i32*
store i32 10, i32* %34
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [20 x i8] c"Failed to open file\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@.str.1 = [16 x i8] c"Failed to mmap \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %4, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = tail call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %2
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = tail call i8* @mmap(i32* null, i32 65536, i32 %16, i32 524291, i32 %8, i32 0)
%18 = icmp eq i8* %17, inttoptr (i64 -1 to i8*)
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @errno
%21 = tail call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %19, %13
%23 = bitcast i8* %17 to i32*
store i32 10, i32* %23
ret i32 0
}