#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 */
typedef size_t ssize_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(int);
size_t atoll(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ftruncate(int, size_t);
void *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int /*<<< orphan*/, void *, size_t);
int shm_open(char *, int, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  void *shm_buf;
  ssize_t r;
  size_t p = 0, *shm_p;
  size_t bufsize_bytes = 0x400000, shm_size;
  int shm_fd;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s /name_of_shm [size_in_bytes]\n", argv[0]);
    return 1;
  }
  if (argc >= 3)
    bufsize_bytes = atoll(argv[2]);
  bufsize_bytes = (bufsize_bytes / 16) * 16;
  shm_size = bufsize_bytes + sizeof(size_t);
  shm_fd = shm_open(argv[1], O_CREAT | O_RDWR, 0644);
  if (shm_fd < 0) {
    perror("[shmwrite] shm_open failed");
    return 1;
  }
  if (ftruncate(shm_fd, shm_size) < 0) {
    perror("[shmwrite] ftruncate failed");
    return 1;
  }
  shm_buf = mmap(0, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  if (shm_buf == MAP_FAILED) {
    perror("[shmwrite] mmap failed");
    return 1;
  }
  shm_p = shm_buf + bufsize_bytes;
  p = *shm_p;
  if (p >= bufsize_bytes)
    p = 0;
  p = (p / 16) * 16;
  *shm_p = p;
  for (;;) {
    size_t bytestoread = bufsize_bytes - p;
    if (bytestoread > 0x10000)
      bytestoread = 0x10000;
    r = read(0, shm_buf + p, bytestoread);
    if (r <= 0)
      break;
    p += r;
    assert(p <= bufsize_bytes);
    if (p >= bufsize_bytes)
      p = 0;
    *shm_p = (p / 16) * 16;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x400000,-0x38(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d8 <main+0x58>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cb <main+0x24b>
cmpl   $0x3,-0x8(%rbp)
jl     11f3 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <atoll@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
shr    $0x4,%rax
shl    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <shm_open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1258 <main+0xd8>
lea    0xde1(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cb <main+0x24b>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
call   1040 <ftruncate@plt>
cmp    $0x0,%rax
jge    1286 <main+0x106>
lea    0xdce(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cb <main+0x24b>
mov    -0x40(%rbp),%rsi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
mov    0x2d1e(%rip),%rax
or     (%rax),%edx
mov    0x2d3d(%rip),%rax
mov    (%rax),%ecx
mov    -0x44(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2ced(%rip),%rcx
cmp    (%rcx),%rax
jne    12e4 <main+0x164>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cb <main+0x24b>
mov    -0x18(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jb     1311 <main+0x191>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x4,%rax
shl    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x10000,-0x50(%rbp)
jbe    134e <main+0x1ce>
movq   $0x10000,-0x50(%rbp)
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x50(%rbp),%rdx
xor    %edi,%edi
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
ja     1375 <main+0x1f5>
jmp    13c4 <main+0x244>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jb     13ac <main+0x22c>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
shr    $0x4,%rcx
shl    $0x4,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
jmp    132c <main+0x1ac>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11b8 <main+0x38>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe5d(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x400000,%r14d
cmp    $0x2,%edi
je     11d3 <main+0x53>
mov    0x10(%rbx),%rdi
call   1060 <atoll@plt>
mov    %rax,%r14
and    $0xfffffffffffffff0,%r14
mov    0x8(%rbx),%rdi
mov    0x2e1a(%rip),%rax
mov    0x2deb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <shm_open@plt>
test   %eax,%eax
js     12d9 <main+0x159>
mov    %eax,%r15d
mov    %r14,%rbp
or     $0x8,%rbp
mov    %eax,%edi
mov    %rbp,%rsi
call   1040 <ftruncate@plt>
test   %rax,%rax
js     12e2 <main+0x162>
mov    0x2dd1(%rip),%rax
mov    0x2d92(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2daf(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    0x2d61(%rip),%rax
cmp    (%rax),%r15
je     12eb <main+0x16b>
mov    (%r15,%r14,1),%rax
mov    %rax,%rbp
and    $0xfffffffffffffff0,%rbp
cmp    %r14,%rax
cmovae %rbx,%rbp
mov    %rbp,(%r15,%r14,1)
mov    %r14,%rdx
sub    %rbp,%rdx
cmp    $0x10000,%rdx
mov    $0x10000,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmovae %r12,%rdx
lea    (%r15,%rbp,1),%rsi
xor    %edi,%edi
call   1050 <read@plt>
test   %rax,%rax
je     11ad <main+0x2d>
add    %rax,%rbp
xor    %ebx,%ebx
xor    %edi,%edi
cmp    %r14,%rbp
setbe  %dil
cmovae %rbx,%rbp
call   1070 <assert@plt>
mov    %rbp,%rax
and    $0xfffffffffffffff0,%rax
mov    %rax,(%r15,%r14,1)
mov    %r14,%rdx
sub    %rbp,%rdx
cmp    $0x10000,%rdx
jmp    1290 <main+0x110>
lea    0xd48(%rip),%rdi
jmp    12f2 <main+0x172>
lea    0xd5a(%rip),%rdi
jmp    12f2 <main+0x172>
lea    0xd6d(%rip),%rdi
call   1090 <perror@plt>
jmp    11a8 <main+0x28>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s /name_of_shm [size_in_bytes]\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [27 x i8] c"[shmwrite] shm_open failed\00"
@.str.2 = [28 x i8] c"[shmwrite] ftruncate failed\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [23 x i8] c"[shmwrite] mmap failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %8
store i64 4194304, i64* %10
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %119
22: ; preds = %2
%23 = load i32, i32* %4
%24 = icmp sge i32 %23, 3
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i64 @atoll(i8* %28)
store i64 %29, i64* %10
br label %30
30: ; preds = %25, %22
%31 = load i64, i64* %10
%32 = udiv i64 %31, 16
%33 = mul i64 %32, 16
store i64 %33, i64* %10
%34 = load i64, i64* %10
%35 = add i64 %34, 8
store i64 %35, i64* %11
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = load i32, i32* @O_CREAT
%40 = load i32, i32* @O_RDWR
%41 = or i32 %39, %40
%42 = call i32 @shm_open(i8* %38, i32 %41, i32 420)
store i32 %42, i32* %12
%43 = load i32, i32* %12
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %30
%46 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %119
47: ; preds = %30
%48 = load i32, i32* %12
%49 = load i64, i64* %11
%50 = call i64 @ftruncate(i32 %48, i64 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %119
54: ; preds = %47
%55 = load i64, i64* %11
%56 = load i32, i32* @PROT_READ
%57 = load i32, i32* @PROT_WRITE
%58 = or i32 %56, %57
%59 = load i32, i32* @MAP_SHARED
%60 = load i32, i32* %12
%61 = call i8* @mmap(i32 0, i64 %55, i32 %58, i32 %59, i32 %60, i32 0)
store i8* %61, i8** %6
%62 = load i8*, i8** %6
%63 = load i8*, i8** @MAP_FAILED
%64 = icmp eq i8* %62, %63
br i1 %64, label %65, label %67
65: ; preds = %54
%66 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %119
67: ; preds = %54
%68 = load i8*, i8** %6
%69 = load i64, i64* %10
%70 = getelementptr i8, i8* %68, i64 %69
%71 = bitcast i8* %70 to i64*
store i64* %71, i64** %9
%72 = load i64*, i64** %9
%73 = load i64, i64* %72
store i64 %73, i64* %8
%74 = load i64, i64* %8
%75 = load i64, i64* %10
%76 = icmp uge i64 %74, %75
br i1 %76, label %77, label %78
77: ; preds = %67
store i64 0, i64* %8
br label %78
78: ; preds = %77, %67
%79 = load i64, i64* %8
%80 = udiv i64 %79, 16
%81 = mul i64 %80, 16
store i64 %81, i64* %8
%82 = load i64, i64* %8
%83 = load i64*, i64** %9
store i64 %82, i64* %83
br label %84
84: ; preds = %113, %78
%85 = load i64, i64* %10
%86 = load i64, i64* %8
%87 = sub i64 %85, %86
store i64 %87, i64* %13
%88 = load i64, i64* %13
%89 = icmp ugt i64 %88, 65536
br i1 %89, label %90, label %91
90: ; preds = %84
store i64 65536, i64* %13
br label %91
91: ; preds = %90, %84
%92 = load i8*, i8** %6
%93 = load i64, i64* %8
%94 = getelementptr i8, i8* %92, i64 %93
%95 = load i64, i64* %13
%96 = call i64 @read(i32 0, i8* %94, i64 %95)
store i64 %96, i64* %7
%97 = load i64, i64* %7
%98 = icmp ule i64 %97, 0
br i1 %98, label %99, label %100
99: ; preds = %91
br label %118
100: ; preds = %91
%101 = load i64, i64* %7
%102 = load i64, i64* %8
%103 = add i64 %102, %101
store i64 %103, i64* %8
%104 = load i64, i64* %8
%105 = load i64, i64* %10
%106 = icmp ule i64 %104, %105
%107 = zext i1 %106 to i32
%108 = call i32 @assert(i32 %107)
%109 = load i64, i64* %8
%110 = load i64, i64* %10
%111 = icmp uge i64 %109, %110
br i1 %111, label %112, label %113
112: ; preds = %100
store i64 0, i64* %8
br label %113
113: ; preds = %112, %100
%114 = load i64, i64* %8
%115 = udiv i64 %114, 16
%116 = mul i64 %115, 16
%117 = load i64*, i64** %9
store i64 %116, i64* %117
br label %84
118: ; preds = %99
store i32 0, i32* %3
br label %119
119: ; preds = %118, %65, %52, %45, %16
%120 = load i32, i32* %3
ret i32 %120
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s /name_of_shm [size_in_bytes]\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [27 x i8] c"[shmwrite] shm_open failed\00"
@.str.2 = [28 x i8] c"[shmwrite] ftruncate failed\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [23 x i8] c"[shmwrite] mmap failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %71
8: ; preds = %2
%9 = icmp eq i32 %0, 2
br i1 %9, label %15, label %10
10: ; preds = %8
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i64 @atoll(i8* %12)
%14 = and i64 %13, -16
br label %15
15: ; preds = %10, %8
%16 = phi i64 [ %14, %10 ], [ 4194304, %8 ]
%17 = or i64 %16, 8
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_RDWR
%22 = or i32 %21, %20
%23 = tail call i32 @shm_open(i8* %19, i32 %22, i32 420)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %71
27: ; preds = %15
%28 = tail call i64 @ftruncate(i32 %23, i64 %17)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %71
32: ; preds = %27
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_SHARED
%37 = tail call i8* @mmap(i32 0, i64 %17, i32 %35, i32 %36, i32 %23, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %71
42: ; preds = %32
%43 = getelementptr i8, i8* %37, i64 %16
%44 = bitcast i8* %43 to i64*
%45 = load i64, i64* %44
%46 = icmp ult i64 %45, %16
%47 = and i64 %45, -16
%48 = select i1 %46, i64 %47, i64 0
store i64 %48, i64* %44
%49 = sub i64 %16, %48
%50 = icmp ult i64 %49, 65536
%51 = select i1 %50, i64 %49, i64 65536
%52 = getelementptr i8, i8* %37, i64 %48
%53 = tail call i64 @read(i32 0, i8* %52, i64 %51)
%54 = icmp eq i64 %53, 0
br i1 %54, label %71, label %55
55: ; preds = %42, %55
%56 = phi i64 [ %69, %55 ], [ %53, %42 ]
%57 = phi i64 [ %63, %55 ], [ %48, %42 ]
%58 = add i64 %56, %57
%59 = icmp ule i64 %58, %16
%60 = zext i1 %59 to i32
%61 = tail call i32 @assert(i32 %60)
%62 = icmp ult i64 %58, %16
%63 = select i1 %62, i64 %58, i64 0
%64 = and i64 %63, -16
store i64 %64, i64* %44
%65 = sub i64 %16, %63
%66 = icmp ult i64 %65, 65536
%67 = select i1 %66, i64 %65, i64 65536
%68 = getelementptr i8, i8* %37, i64 %63
%69 = tail call i64 @read(i32 0, i8* %68, i64 %67)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %55
71: ; preds = %55, %42, %40, %30, %25, %4
%72 = phi i32 [ 1, %4 ], [ 1, %25 ], [ 1, %30 ], [ 1, %40 ], [ 0, %42 ], [ 0, %55 ]
ret i32 %72
}