#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_ANONYMOUS;
void *MAP_FAILED;
int MAP_FIXED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ ftruncate(int, size_t);
int mkstemp(char *);
void *mmap(void *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int /*<<< orphan*/ unlink(char *);
void *map_buffer(size_t length) {
  void *address = NULL;
  void *full_address = mmap(0, length * 2, PROT_READ | PROT_WRITE,
                            MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  if (MAP_FAILED == full_address) {
    fprintf(stderr, "Failed to create anon memory map: %s[%d]\n",
            strerror(errno), errno);
    return NULL;
  }
  char name[] = "/dev/shm/spsc_rb_XXXXXX";
  int fd = mkstemp(name);
  if (fd < 0) {
    fprintf(stderr, "Failed to create shared memory: %s[%d]\n", strerror(errno),
            errno);
    return NULL;
  }
  ftruncate(fd, length);
  void *first_half_address = mmap(full_address, length, PROT_READ | PROT_WRITE,
                                  MAP_SHARED | MAP_FIXED, fd, 0);
  if (MAP_FAILED == first_half_address) {
    fprintf(stderr, "Failed to map real file: %s[%d]\n", strerror(errno),
            errno);
    goto cleanup;
  }
  void *second_half_address =
      mmap(full_address + length, length, PROT_READ | PROT_WRITE,
           MAP_SHARED | MAP_FIXED, fd, 0);
  if (MAP_FAILED == second_half_address) {
    fprintf(stderr, "Failed to map real file: %s[%d]\n", strerror(errno),
            errno);
    goto cleanup;
  }
  address = first_half_address;
cleanup:
  close(fd);
  unlink(name);
  return address;
}


x86 assembly (clang, -O0):
<map_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
shl    $0x1,%rsi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    0x2e1c(%rip),%rax
or     (%rax),%edx
mov    0x2e43(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2a(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jne    121c <map_buffer+0xac>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
mov    0x2dbf(%rip),%rax
mov    (%rax),%ecx
lea    0xdf6(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13da <map_buffer+0x26a>
mov    0xe5d(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe57(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1295 <map_buffer+0x125>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
mov    0x2d46(%rip),%rax
mov    (%rax),%ecx
lea    0xda7(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13da <map_buffer+0x26a>
mov    -0x44(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <ftruncate@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d48(%rip),%rax
mov    (%rax),%edx
mov    0x2cff(%rip),%rax
or     (%rax),%edx
mov    0x2d26(%rip),%rax
mov    (%rax),%ecx
mov    0x2ce5(%rip),%rax
or     (%rax),%ecx
mov    -0x44(%rbp),%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x50(%rbp)
mov    0x2cc4(%rip),%rax
mov    (%rax),%rax
cmp    -0x50(%rbp),%rax
jne    132b <map_buffer+0x1bb>
mov    0x2cd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
mov    0x2ca8(%rip),%rax
mov    (%rax),%ecx
lea    0xd31(%rip),%rsi
call   1070 <fprintf@plt>
jmp    13c1 <map_buffer+0x251>
mov    -0x20(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2cba(%rip),%rax
mov    (%rax),%edx
mov    0x2c71(%rip),%rax
or     (%rax),%edx
mov    0x2c98(%rip),%rax
mov    (%rax),%ecx
mov    0x2c57(%rip),%rax
or     (%rax),%ecx
mov    -0x44(%rbp),%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x58(%rbp)
mov    0x2c36(%rip),%rax
mov    (%rax),%rax
cmp    -0x58(%rbp),%rax
jne    13b9 <map_buffer+0x249>
mov    0x2c42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2c2e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
mov    0x2c1a(%rip),%rax
mov    (%rax),%ecx
lea    0xca3(%rip),%rsi
call   1070 <fprintf@plt>
jmp    13c1 <map_buffer+0x251>
mov    -0x50(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
lea    -0x40(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
lea    (%rdi,%rdi,1),%rsi
mov    0x2e6c(%rip),%rbx
mov    0x2e25(%rip),%r12
mov    (%r12),%edx
or     (%rbx),%edx
mov    0x2e48(%rip),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%ecx
or     (%rdi),%ecx
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2de6(%rip),%r13
cmp    %rax,0x0(%r13)
je     12ad <map_buffer+0x13d>
mov    %rax,%rbp
movaps 0xeaa(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x5858585858585f,%rax
mov    %rax,0x10(%rsp)
mov    %rsp,%rdi
call   1080 <mkstemp@plt>
test   %eax,%eax
js     12d1 <map_buffer+0x161>
mov    %eax,%r14d
mov    %eax,%edi
mov    %r15,%rsi
call   1050 <ftruncate@plt>
mov    (%r12),%edx
or     (%rbx),%edx
mov    0x2d9d(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcc(%rip),%rax
or     (%rax),%ecx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    %rax,0x0(%r13)
je     126d <map_buffer+0xfd>
mov    %rbx,%rcx
mov    %rax,%rbx
mov    (%r12),%edx
or     (%rcx),%edx
add    %r15,%rbp
mov    0x2d65(%rip),%rax
mov    (%rax),%ecx
mov    0x2d94(%rip),%rax
or     (%rax),%ecx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    %rax,0x0(%r13)
jne    129b <map_buffer+0x12b>
mov    0x2d54(%rip),%rax
mov    (%rax),%ebx
mov    0x2d43(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
mov    0x0(%rbp),%ecx
lea    0xdc3(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %r14d,%edi
call   1060 <close@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
jmp    12ff <map_buffer+0x18f>
mov    0x2d14(%rip),%rax
mov    (%rax),%ebx
mov    0x2d03(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
mov    0x0(%rbp),%ecx
lea    0xd31(%rip),%rsi
jmp    12f3 <map_buffer+0x183>
mov    0x2cf0(%rip),%rax
mov    (%rax),%ebx
mov    0x2cdf(%rip),%rbp
mov    0x0(%rbp),%edi
call   1090 <strerror@plt>
mov    0x0(%rbp),%ecx
lea    0xd37(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str = [42 x i8] c"Failed to create anon memory map: %s[%d]\0A\00"
@errno = global i32 0
@__const.map_buffer.name = [24 x i8] c"/dev/shm/spsc_rb_XXXXXX\00"
@.str.1 = [40 x i8] c"Failed to create shared memory: %s[%d]\0A\00"
@MAP_FIXED = global i32 0
@.str.2 = [33 x i8] c"Failed to map real file: %s[%d]\0A\00"
define i8* @map_buffer(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [24 x i8]
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i64 %0, i64* %3
store i8* null, i8** %4
%10 = load i64, i64* %3
%11 = mul i64 %10, 2
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* @MAP_ANONYMOUS
%17 = or i32 %15, %16
%18 = call i8* @mmap(i8* null, i64 %11, i32 %14, i32 %17, i32 -1, i32 0)
store i8* %18, i8** %5
%19 = load i8*, i8** @MAP_FAILED
%20 = load i8*, i8** %5
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %28
22: ; preds = %1
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = load i32, i32* @errno
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %26)
store i8* null, i8** %2
br label %93
28: ; preds = %1
%29 = bitcast [24 x i8]* %6 to i8*
%30 = getelementptr inbounds [24 x i8], [24 x i8]* %6, i64 0, i64 0
%31 = call i32 @mkstemp(i8* %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = load i32, i32* @errno
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %37, i32 %38)
store i8* null, i8** %2
br label %93
40: ; preds = %28
%41 = load i32, i32* %7
%42 = load i64, i64* %3
%43 = call i32 @ftruncate(i32 %41, i64 %42)
%44 = load i8*, i8** %5
%45 = load i64, i64* %3
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @PROT_WRITE
%48 = or i32 %46, %47
%49 = load i32, i32* @MAP_SHARED
%50 = load i32, i32* @MAP_FIXED
%51 = or i32 %49, %50
%52 = load i32, i32* %7
%53 = call i8* @mmap(i8* %44, i64 %45, i32 %48, i32 %51, i32 %52, i32 0)
store i8* %53, i8** %8
%54 = load i8*, i8** @MAP_FAILED
%55 = load i8*, i8** %8
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %63
57: ; preds = %40
%58 = load i32, i32* @stderr
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = load i32, i32* @errno
%62 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %60, i32 %61)
br label %87
63: ; preds = %40
%64 = load i8*, i8** %5
%65 = load i64, i64* %3
%66 = getelementptr i8, i8* %64, i64 %65
%67 = load i64, i64* %3
%68 = load i32, i32* @PROT_READ
%69 = load i32, i32* @PROT_WRITE
%70 = or i32 %68, %69
%71 = load i32, i32* @MAP_SHARED
%72 = load i32, i32* @MAP_FIXED
%73 = or i32 %71, %72
%74 = load i32, i32* %7
%75 = call i8* @mmap(i8* %66, i64 %67, i32 %70, i32 %73, i32 %74, i32 0)
store i8* %75, i8** %9
%76 = load i8*, i8** @MAP_FAILED
%77 = load i8*, i8** %9
%78 = icmp eq i8* %76, %77
br i1 %78, label %79, label %85
79: ; preds = %63
%80 = load i32, i32* @stderr
%81 = load i32, i32* @errno
%82 = call i8* @strerror(i32 %81)
%83 = load i32, i32* @errno
%84 = call i32 @fprintf(i32 %80, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %82, i32 %83)
br label %87
85: ; preds = %63
%86 = load i8*, i8** %8
store i8* %86, i8** %4
br label %87
87: ; preds = %85, %79, %57
%88 = load i32, i32* %7
%89 = call i32 @close(i32 %88)
%90 = getelementptr inbounds [24 x i8], [24 x i8]* %6, i64 0, i64 0
%91 = call i32 @unlink(i8* %90)
%92 = load i8*, i8** %4
store i8* %92, i8** %2
br label %93
93: ; preds = %87, %34, %22
%94 = load i8*, i8** %2
ret i8* %94
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str = [42 x i8] c"Failed to create anon memory map: %s[%d]\0A\00"
@errno = global i32 0
@__const.map_buffer.name = [24 x i8] c"/dev/shm/spsc_rb_XXXXXX\00"
@.str.1 = [40 x i8] c"Failed to create shared memory: %s[%d]\0A\00"
@MAP_FIXED = global i32 0
@.str.2 = [33 x i8] c"Failed to map real file: %s[%d]\0A\00"
define i8* @map_buffer(i64 %0) {
%2 = alloca [24 x i8]
%3 = shl i64 %0, 1
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_SHARED
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %8, %7
%10 = tail call i8* @mmap(i8* null, i64 %3, i32 %6, i32 %9, i32 -1, i32 0)
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %11, %10
br i1 %12, label %13, label %19
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = load i32, i32* @errno
%18 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17)
br label %63
19: ; preds = %1
%20 = getelementptr inbounds [24 x i8], [24 x i8]* %2, i64 0, i64 0
%21 = call i32 @mkstemp(i8* %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = load i32, i32* @errno
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %27)
br label %61
29: ; preds = %19
%30 = call i32 @ftruncate(i32 %21, i64 %0)
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %32, %31
%34 = load i32, i32* @MAP_SHARED
%35 = load i32, i32* @MAP_FIXED
%36 = or i32 %35, %34
%37 = call i8* @mmap(i8* %10, i64 %0, i32 %33, i32 %36, i32 %21, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %38, %37
br i1 %39, label %51, label %40
40: ; preds = %29
%41 = getelementptr i8, i8* %10, i64 %0
%42 = load i32, i32* @PROT_READ
%43 = load i32, i32* @PROT_WRITE
%44 = or i32 %43, %42
%45 = load i32, i32* @MAP_SHARED
%46 = load i32, i32* @MAP_FIXED
%47 = or i32 %46, %45
%48 = call i8* @mmap(i8* %41, i64 %0, i32 %44, i32 %47, i32 %21, i32 0)
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp eq i8* %49, %48
br i1 %50, label %51, label %57
51: ; preds = %40, %29
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = load i32, i32* @errno
%56 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %54, i32 %55)
br label %57
57: ; preds = %51, %40
%58 = phi i8* [ %37, %40 ], [ null, %51 ]
%59 = call i32 @close(i32 %21)
%60 = call i32 @unlink(i8* %20)
br label %61
61: ; preds = %57, %23
%62 = phi i8* [ null, %23 ], [ %58, %57 ]
br label %63
63: ; preds = %61, %13
%64 = phi i8* [ null, %13 ], [ %62, %61 ]
ret i8* %64
}