#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 */
struct stat {
  size_t st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int shm_open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
size_t write(int, void *, size_t);
int main(int argc, char *argv[]) {
  void *shm_buf;
  size_t nextp = 0, prevp = 0, *shm_p;
  size_t shm_size, bufsize_bytes;
  int shm_fd;
  struct stat shm_stat;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s /name_of_shm\n", argv[0]);
    return 1;
  }
  shm_fd = shm_open(argv[1], O_RDONLY, 0644);
  if (shm_fd < 0) {
    perror("[shmread] shm_open failed");
    return 1;
  }
  if (fstat(shm_fd, &shm_stat) < 0) {
    perror("[shmread] fstat failed");
    return 1;
  }
  shm_size = shm_stat.st_size;
  bufsize_bytes = shm_size - sizeof(size_t);
  shm_buf = mmap(0, shm_size, PROT_READ, MAP_SHARED, shm_fd, 0);
  if (shm_buf == MAP_FAILED) {
    perror("[shmread] mmap failed");
    return 1;
  }
  shm_p = shm_buf + bufsize_bytes;
  prevp = *shm_p;
  assert(prevp < bufsize_bytes);
  for (;;) {
    size_t nb, nwritten;
    nextp = *shm_p;
    assert(nextp < bufsize_bytes);
    if (nextp > prevp) {
      nb = nextp - prevp;
      nwritten = write(1, shm_buf + prevp, nb);
      prevp += nwritten;
      assert(prevp <= bufsize_bytes);
    } else if (nextp < prevp) {
      nb = bufsize_bytes - prevp;
      nwritten = write(1, shm_buf + prevp, nb);
      prevp += nwritten;
      if (prevp == bufsize_bytes) {
        prevp = 0;
      }
      assert(prevp <= bufsize_bytes);
    } else {
      usleep(50000);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d8 <main+0x58>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x259>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <shm_open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1218 <main+0x98>
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x259>
mov    -0x44(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    1246 <main+0xc6>
lea    0xdfd(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x259>
mov    -0x50(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
sub    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rsi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    0x2d7a(%rip),%rax
mov    (%rax),%ecx
mov    -0x44(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%rax
jne    12af <main+0x12f>
lea    0xdab(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x259>
mov    -0x18(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x40(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x40(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    1355 <main+0x1d5>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x40(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
jmp    13d4 <main+0x254>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    13c5 <main+0x245>
mov    -0x40(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x40(%rbp),%rax
jne    13ab <main+0x22b>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x40(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
jmp    13cf <main+0x24f>
mov    $0xc350,%edi
call   10a0 <usleep@plt>
jmp    13d4 <main+0x254>
jmp    12db <main+0x15b>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     11bb <main+0x3b>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe5c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsi),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <shm_open@plt>
test   %eax,%eax
js     12c9 <main+0x149>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     12d2 <main+0x152>
mov    0x8(%rsp),%rbx
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    0x2de2(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    0x2d9e(%rip),%rax
cmp    (%rax),%r14
je     12db <main+0x15b>
add    $0xfffffffffffffff8,%rbx
mov    (%r14,%rbx,1),%r12
xor    %edi,%edi
cmp    %rbx,%r12
setb   %dil
call   1050 <assert@plt>
xor    %r15d,%r15d
jmp    1270 <main+0xf0>
lea    (%r14,%r12,1),%rsi
mov    $0x1,%edi
mov    %rbp,%rdx
call   1030 <write@plt>
add    %rax,%r12
xor    %edi,%edi
cmp    %rbx,%r12
setbe  %dil
call   1050 <assert@plt>
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,1),%rbp
xor    %edi,%edi
cmp    %rbx,%rbp
setb   %dil
call   1050 <assert@plt>
sub    %r12,%rbp
ja     1246 <main+0xc6>
jb     1295 <main+0x115>
mov    $0xc350,%edi
call   10a0 <usleep@plt>
jmp    1270 <main+0xf0>
mov    %rbx,%rdx
sub    %r12,%rdx
lea    (%r14,%r12,1),%rsi
mov    $0x1,%edi
call   1030 <write@plt>
mov    %rax,%rbp
add    %r12,%rbp
cmp    %rbx,%rbp
cmove  %r15,%rbp
xor    %edi,%edi
cmp    %rbx,%rbp
setbe  %dil
call   1050 <assert@plt>
mov    %rbp,%r12
jmp    1270 <main+0xf0>
lea    0xd48(%rip),%rdi
jmp    12e2 <main+0x162>
lea    0xd59(%rip),%rdi
jmp    12e2 <main+0x162>
lea    0xd67(%rip),%rdi
call   1070 <perror@plt>
jmp    11a9 <main+0x29>

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

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