#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 MAP_ANON;
void *MAP_FAILED;
int MAP_FIXED;
int MAP_NORESERVE;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
void *mmap(void *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
void *map_segment(void *fixed_addr) {
  int flags = MAP_NORESERVE | MAP_ANON | MAP_PRIVATE | MAP_FIXED;
  void *addr = mmap(fixed_addr, 1, PROT_READ | PROT_WRITE, flags, -1, 0);
  if (addr == MAP_FAILED) {
    int error = errno;
    fprintf(stderr, "mmap failed: %s (%d)\n", strerror(error), error);
    return NULL;
  }
  assert(addr == fixed_addr);
  *((char *)addr) = 1;
  return addr;
}


x86 assembly (clang, -O0):
<map_segment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    0x2e5c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e4b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
mov    0x2e39(%rip),%rax
or     (%rax),%edx
mov    -0x14(%rbp),%ecx
mov    $0x1,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    11f9 <map_segment+0xb9>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
mov    -0x24(%rbp),%ecx
lea    0xe19(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    121d <map_segment+0xdd>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_segment>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e95(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %rdi,%r14
mov    0x2e50(%rip),%rax
or     (%rax),%ecx
mov    0x2e3f(%rip),%rax
or     (%rax),%ecx
mov    0x2e86(%rip),%rax
mov    0x2e47(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    $0x1,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e06(%rip),%rax
cmp    (%rax),%rbx
je     11b2 <map_segment+0x72>
xor    %edi,%edi
cmp    %r14,%rbx
sete   %dil
call   1040 <assert@plt>
movb   $0x1,(%rbx)
jmp    11e0 <map_segment+0xa0>
mov    0x2e0f(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0e(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe2e(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
mov    %ebx,%ecx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@MAP_NORESERVE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FIXED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@errno = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"mmap failed: %s (%d)\0A\00"
define i8* @map_segment(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @MAP_NORESERVE
%8 = load i32, i32* @MAP_ANON
%9 = or i32 %7, %8
%10 = load i32, i32* @MAP_PRIVATE
%11 = or i32 %9, %10
%12 = load i32, i32* @MAP_FIXED
%13 = or i32 %11, %12
store i32 %13, i32* %4
%14 = load i8*, i8** %3
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = load i32, i32* %4
%19 = call i8* @mmap(i8* %14, i32 1, i32 %17, i32 %18, i32 -1, i32 0)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %30
23: ; preds = %1
%24 = load i32, i32* @errno
store i32 %24, i32* %6
%25 = load i32, i32* @stderr
%26 = load i32, i32* %6
%27 = call i8* @strerror(i32 %26)
%28 = load i32, i32* %6
%29 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %27, i32 %28)
store i8* null, i8** %2
br label %38
30: ; preds = %1
%31 = load i8*, i8** %5
%32 = load i8*, i8** %3
%33 = icmp eq i8* %31, %32
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i8*, i8** %5
store i8 1, i8* %36
%37 = load i8*, i8** %5
store i8* %37, i8** %2
br label %38
38: ; preds = %30, %23
%39 = load i8*, i8** %2
ret i8* %39
}

LLVM IR (-O2):
@MAP_NORESERVE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FIXED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@errno = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"mmap failed: %s (%d)\0A\00"
define i8* @map_segment(i8* %0) {
%2 = load i32, i32* @MAP_NORESERVE
%3 = load i32, i32* @MAP_ANON
%4 = or i32 %3, %2
%5 = load i32, i32* @MAP_PRIVATE
%6 = or i32 %4, %5
%7 = load i32, i32* @MAP_FIXED
%8 = or i32 %6, %7
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = tail call i8* @mmap(i8* %0, i32 1, i32 %11, i32 %8, i32 -1, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = load i32, i32* @stderr
%18 = tail call i8* @strerror(i32 %16)
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %16)
br label %24
20: ; preds = %1
%21 = icmp eq i8* %12, %0
%22 = zext i1 %21 to i32
%23 = tail call i32 @assert(i32 %22)
store i8 1, i8* %12
br label %24
24: ; preds = %20, %15
%25 = phi i8* [ null, %15 ], [ %12, %20 ]
ret i8* %25
}