#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_PRIVATE;
int MAP_SHARED;
int PROT_NONE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int ftruncate(int, int);
int getpagesize();
void *mmap(void *, int, int, int, int, int /*<<< orphan*/);
int my_memfd_create(char *, int /*<<< orphan*/);
void *make_ring_buffer() {
  int page_size = getpagesize();
  if (((128 * 1024) < page_size) || (page_size <= 0) ||
      (((128 * 1024) % page_size) != 0)) {
    return NULL;
  }
  int memfd = my_memfd_create("ring", 0);
  if (memfd == -1) {
    return NULL;
  }
  if (ftruncate(memfd, (128 * 1024)) == -1) {
    return NULL;
  }
  void *base = mmap(NULL, 3 * (128 * 1024), PROT_NONE,
                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (base == MAP_FAILED) {
    return NULL;
  }
  for (int i = 0; i < 3; i++) {
    void *p = mmap(base + (i * (128 * 1024)), (128 * 1024),
                   PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, memfd, 0);
    if (p == MAP_FAILED) {
      return NULL;
    }
  }
  close(memfd);
  return base;
}


x86 assembly (clang, -O0):
<make_ring_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %eax,-0xc(%rbp)
mov    $0x20000,%eax
cmp    -0xc(%rbp),%eax
jl     118c <make_ring_buffer+0x3c>
cmpl   $0x0,-0xc(%rbp)
jle    118c <make_ring_buffer+0x3c>
mov    $0x20000,%eax
cltd
idivl  -0xc(%rbp)
cmp    $0x0,%edx
je     1199 <make_ring_buffer+0x49>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <make_ring_buffer+0x188>
lea    0xe60(%rip),%rdi
xor    %esi,%esi
call   1030 <my_memfd_create@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11c1 <make_ring_buffer+0x71>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <make_ring_buffer+0x188>
mov    -0x10(%rbp),%edi
mov    $0x20000,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11e4 <make_ring_buffer+0x94>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <make_ring_buffer+0x188>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    0x2ddc(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbb(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x60000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d83(%rip),%rcx
cmp    (%rcx),%rax
jne    123b <make_ring_buffer+0xeb>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <make_ring_buffer+0x188>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    12c8 <make_ring_buffer+0x178>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x11,%eax
cltq
add    %rax,%rdi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
mov    0x2d55(%rip),%rax
or     (%rax),%edx
mov    0x2d3c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d63(%rip),%rax
or     (%rax),%ecx
mov    -0x10(%rbp),%r8d
mov    $0x20000,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <make_ring_buffer+0x165>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <make_ring_buffer+0x188>
jmp    12ba <make_ring_buffer+0x16a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1242 <make_ring_buffer+0xf2>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_ring_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %eax,%ecx
lea    -0x20001(%rcx),%eax
cmp    $0xfffe0000,%eax
jb     1182 <make_ring_buffer+0x32>
xor    %ebx,%ebx
mov    $0x20000,%eax
xor    %edx,%edx
div    %ecx
test   %edx,%edx
je     1194 <make_ring_buffer+0x44>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe65(%rip),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <my_memfd_create@plt>
cmp    $0xffffffff,%eax
je     1182 <make_ring_buffer+0x32>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x20000,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12d1 <make_ring_buffer+0x181>
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2e00(%rip),%rax
mov    0x2de1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x60000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2daf(%rip),%r15
cmp    (%r15),%rax
je     1182 <make_ring_buffer+0x32>
mov    %rax,%r14
mov    0x2df0(%rip),%rax
mov    0x2db1(%rip),%r12
mov    (%r12),%edx
or     (%rax),%edx
mov    0x2d94(%rip),%rax
mov    0x2dbd(%rip),%r13
mov    0x0(%r13),%ecx
or     (%rax),%ecx
xor    %ebx,%ebx
mov    %r14,%rdi
mov    $0x20000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    (%r15),%rax
je     1182 <make_ring_buffer+0x32>
mov    (%r12),%edx
mov    0x2da6(%rip),%rax
or     (%rax),%edx
lea    0x20000(%r14),%rdi
mov    0x0(%r13),%ecx
mov    0x2d4a(%rip),%rax
or     (%rax),%ecx
xor    %ebx,%ebx
mov    $0x20000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    (%r15),%rax
je     1182 <make_ring_buffer+0x32>
mov    %r14,%rdi
mov    (%r12),%edx
mov    0x2d67(%rip),%rax
or     (%rax),%edx
add    $0x40000,%rdi
mov    0x0(%r13),%ecx
mov    0x2d0b(%rip),%rax
or     (%rax),%ecx
xor    %ebx,%ebx
mov    $0x20000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    (%r15),%rax
je     1182 <make_ring_buffer+0x32>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14,%rbx
jmp    1182 <make_ring_buffer+0x32>
xor    %ebx,%ebx
jmp    1182 <make_ring_buffer+0x32>

LLVM IR (-O0):
@.str = [5 x i8] c"ring\00"
@PROT_NONE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FIXED = global i32 0
@MAP_SHARED = global i32 0
define i8* @make_ring_buffer() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = call i32 (...) @getpagesize()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 131072, %8
br i1 %9, label %17, label %10
10: ; preds = %0
%11 = load i32, i32* %2
%12 = icmp sle i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = load i32, i32* %2
%15 = srem i32 131072, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13, %10, %0
store i8* null, i8** %1
br label %68
18: ; preds = %13
%19 = call i32 @my_memfd_create(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %18
store i8* null, i8** %1
br label %68
23: ; preds = %18
%24 = load i32, i32* %3
%25 = call i32 @ftruncate(i32 %24, i32 131072)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %23
store i8* null, i8** %1
br label %68
28: ; preds = %23
%29 = load i32, i32* @PROT_NONE
%30 = load i32, i32* @MAP_ANONYMOUS
%31 = load i32, i32* @MAP_PRIVATE
%32 = or i32 %30, %31
%33 = call i8* @mmap(i8* null, i32 393216, i32 %29, i32 %32, i32 -1, i32 0)
store i8* %33, i8** %4
%34 = load i8*, i8** %4
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %38
37: ; preds = %28
store i8* null, i8** %1
br label %68
38: ; preds = %28
store i32 0, i32* %5
br label %39
39: ; preds = %61, %38
%40 = load i32, i32* %5
%41 = icmp slt i32 %40, 3
br i1 %41, label %42, label %64
42: ; preds = %39
%43 = load i8*, i8** %4
%44 = load i32, i32* %5
%45 = mul nsw i32 %44, 131072
%46 = sext i32 %45 to i64
%47 = getelementptr i8, i8* %43, i64 %46
%48 = load i32, i32* @PROT_READ
%49 = load i32, i32* @PROT_WRITE
%50 = or i32 %48, %49
%51 = load i32, i32* @MAP_FIXED
%52 = load i32, i32* @MAP_SHARED
%53 = or i32 %51, %52
%54 = load i32, i32* %3
%55 = call i8* @mmap(i8* %47, i32 131072, i32 %50, i32 %53, i32 %54, i32 0)
store i8* %55, i8** %6
%56 = load i8*, i8** %6
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %60
59: ; preds = %42
store i8* null, i8** %1
br label %68
60: ; preds = %42
br label %61
61: ; preds = %60
%62 = load i32, i32* %5
%63 = add nsw i32 %62, 1
store i32 %63, i32* %5
br label %39
64: ; preds = %39
%65 = load i32, i32* %3
%66 = call i32 @close(i32 %65)
%67 = load i8*, i8** %4
store i8* %67, i8** %1
br label %68
68: ; preds = %64, %59, %37, %27, %22, %17
%69 = load i8*, i8** %1
ret i8* %69
}

LLVM IR (-O2):
@.str = [5 x i8] c"ring\00"
@PROT_NONE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FIXED = global i32 0
@MAP_SHARED = global i32 0
define i8* @make_ring_buffer() {
%1 = tail call i32 (...) @getpagesize()
%2 = add i32 %1, -131073
%3 = icmp ult i32 %2, -131072
br i1 %3, label %55, label %4
4: ; preds = %0
%5 = urem i32 131072, %1
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %55
7: ; preds = %4
%8 = tail call i32 @my_memfd_create(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %55, label %10
10: ; preds = %7
%11 = tail call i32 @ftruncate(i32 %8, i32 131072)
%12 = icmp eq i32 %11, -1
br i1 %12, label %55, label %13
13: ; preds = %10
%14 = load i32, i32* @PROT_NONE
%15 = load i32, i32* @MAP_ANONYMOUS
%16 = load i32, i32* @MAP_PRIVATE
%17 = or i32 %16, %15
%18 = tail call i8* @mmap(i8* null, i32 393216, i32 %14, i32 %17, i32 -1, i32 0)
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %55, label %21
21: ; preds = %13
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %23, %22
%25 = load i32, i32* @MAP_FIXED
%26 = load i32, i32* @MAP_SHARED
%27 = or i32 %26, %25
%28 = tail call i8* @mmap(i8* %18, i32 131072, i32 %24, i32 %27, i32 %8, i32 0)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %55, label %31
31: ; preds = %21
%32 = getelementptr i8, i8* %18, i64 131072
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_FIXED
%37 = load i32, i32* @MAP_SHARED
%38 = or i32 %37, %36
%39 = tail call i8* @mmap(i8* %32, i32 131072, i32 %35, i32 %38, i32 %8, i32 0)
%40 = load i8*, i8** @MAP_FAILED
%41 = icmp eq i8* %39, %40
br i1 %41, label %55, label %42
42: ; preds = %31
%43 = getelementptr i8, i8* %18, i64 262144
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @PROT_WRITE
%46 = or i32 %45, %44
%47 = load i32, i32* @MAP_FIXED
%48 = load i32, i32* @MAP_SHARED
%49 = or i32 %48, %47
%50 = tail call i8* @mmap(i8* %43, i32 131072, i32 %46, i32 %49, i32 %8, i32 0)
%51 = load i8*, i8** @MAP_FAILED
%52 = icmp eq i8* %50, %51
br i1 %52, label %55, label %53
53: ; preds = %42
%54 = tail call i32 @close(i32 %8)
br label %55
55: ; preds = %21, %31, %42, %7, %10, %13, %53, %0, %4
%56 = phi i8* [ null, %4 ], [ null, %0 ], [ null, %7 ], [ null, %10 ], [ %18, %53 ], [ null, %13 ], [ null, %42 ], [ null, %31 ], [ null, %21 ]
ret i8* %56
}