#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 {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
unsigned char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fstat(int, struct stat *);
unsigned char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                    int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int shm_open(char *, int /*<<< orphan*/, int);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int i, fd;
  struct stat stat;
  unsigned char c, *ptr;
  if (2 != argc) {
    printf("usage: %s <name>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((fd = shm_open(argv[1], O_RDONLY,
                     (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) == -1) {
    printf("shm_open error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (fstat(fd, &stat) == -1) {
    printf("fstat error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((ptr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
      MAP_FAILED) {
    printf("mmap error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (close(fd) == -1) {
    printf("close error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < stat.st_size; ++i) {
    if ((c = *ptr++) != (i % 256)) {
      printf("error: ptr[%d] = %d\n", i, c);
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11b7 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    0x2df8(%rip),%rax
or     (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
mov    0x2dc6(%rip),%rax
or     (%rax),%edx
call   1070 <shm_open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    122a <main+0xba>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    126c <main+0xfc>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%esi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
mov    0x2d69(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    0x2cf4(%rip),%rcx
cmp    (%rcx),%rax
jne    12d2 <main+0x162>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1310 <main+0x1a0>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    137b <main+0x20b>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x34(%rbp),%eax
cmp    %edx,%eax
je     1368 <main+0x1f8>
mov    -0x14(%rbp),%esi
movzbl -0x21(%rbp),%edx
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    136d <main+0x1fd>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1317 <main+0x1a7>
mov    0x2c16(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11d1 <main+0x61>
mov    0x8(%rsi),%rdi
mov    0x2e19(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3f(%rip),%rax
mov    0x2e18(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%edx
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x70>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe46(%rip),%rdi
jmp    1275 <main+0x105>
mov    (%rsi),%rsi
lea    0xe25(%rip),%rdi
jmp    1278 <main+0x108>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1208 <main+0x98>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe20(%rip),%rdi
jmp    1275 <main+0x105>
mov    (%rsp),%r14d
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    0x2dcc(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2d58(%rip),%rax
cmp    (%rax),%rbx
jne    1254 <main+0xe4>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde5(%rip),%rdi
jmp    1275 <main+0x105>
mov    %ebp,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    128d <main+0x11d>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdd2(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
test   %r14d,%r14d
jle    12c3 <main+0x153>
lea    0xdbf(%rip),%r15
xor    %ebp,%ebp
jmp    12a9 <main+0x139>
nopl   (%rax)
add    $0x1,%rbp
cmp    %ebp,%r14d
je     12c3 <main+0x153>
movzbl (%rbx,%rbp,1),%edx
movzbl %bpl,%eax
cmp    %edx,%eax
je     12a0 <main+0x130>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12a0 <main+0x130>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [18 x i8] c"usage: %s <name>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [20 x i8] c"shm_open error: %s\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"fstat error: %s\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [16 x i8] c"mmap error: %s\0A\00"
@.str.4 = [17 x i8] c"close error: %s\0A\00"
@.str.5 = [21 x i8] c"error: ptr[%d] = %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 2, %11
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IROTH
%31 = or i32 %29, %30
%32 = call i32 @shm_open(i8* %23, i32 %24, i32 %31)
store i32 %32, i32* %7
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %20
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %20
%41 = load i32, i32* %7
%42 = call i32 @fstat(i32 %41, %struct.stat* %8)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %40
%51 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load i32, i32* @PROT_READ
%54 = load i32, i32* @MAP_SHARED
%55 = load i32, i32* %7
%56 = call i8* @mmap(i32* null, i32 %52, i32 %53, i32 %54, i32 %55, i32 0)
store i8* %56, i8** %10
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %65
59: ; preds = %50
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %61)
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %50
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %75
69: ; preds = %65
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %71)
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %65
store i32 0, i32* %6
br label %76
76: ; preds = %95, %75
%77 = load i32, i32* %6
%78 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%79 = load i32, i32* %78
%80 = icmp slt i32 %77, %79
br i1 %80, label %81, label %98
81: ; preds = %76
%82 = load i8*, i8** %10
%83 = getelementptr inbounds i8, i8* %82, i32 1
store i8* %83, i8** %10
%84 = load i8, i8* %82
store i8 %84, i8* %9
%85 = zext i8 %84 to i32
%86 = load i32, i32* %6
%87 = srem i32 %86, 256
%88 = icmp ne i32 %85, %87
br i1 %88, label %89, label %94
89: ; preds = %81
%90 = load i32, i32* %6
%91 = load i8, i8* %9
%92 = zext i8 %91 to i32
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %90, i32 %92)
br label %94
94: ; preds = %89, %81
br label %95
95: ; preds = %94
%96 = load i32, i32* %6
%97 = add nsw i32 %96, 1
store i32 %97, i32* %6
br label %76
98: ; preds = %76
%99 = load i32, i32* @EXIT_SUCCESS
%100 = call i32 @exit(i32 %99)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [18 x i8] c"usage: %s <name>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [20 x i8] c"shm_open error: %s\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"fstat error: %s\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [16 x i8] c"mmap error: %s\0A\00"
@.str.4 = [17 x i8] c"close error: %s\0A\00"
@.str.5 = [21 x i8] c"error: ptr[%d] = %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = load i32, i32* @S_IRGRP
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IROTH
%21 = or i32 %19, %20
%22 = tail call i32 @shm_open(i8* %13, i32 %14, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %11
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %11
%31 = call i32 @fstat(i32 %22, %struct.stat* %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = tail call i32 @exit(i32 %37)
unreachable
39: ; preds = %30
%40 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%41 = load i32, i32* %40
%42 = load i32, i32* @PROT_READ
%43 = load i32, i32* @MAP_SHARED
%44 = tail call i8* @mmap(i32* null, i32 %41, i32 %42, i32 %43, i32 %22, i32 0)
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %44, %45
br i1 %46, label %47, label %53
47: ; preds = %39
%48 = load i32, i32* @errno
%49 = tail call i8* @strerror(i32 %48)
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = tail call i32 @exit(i32 %51)
unreachable
53: ; preds = %39
%54 = tail call i32 @close(i32 %22)
%55 = icmp eq i32 %54, -1
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = icmp sgt i32 %41, 0
br i1 %57, label %64, label %77
58: ; preds = %53
%59 = load i32, i32* @errno
%60 = tail call i8* @strerror(i32 %59)
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %60)
%62 = load i32, i32* @EXIT_FAILURE
%63 = tail call i32 @exit(i32 %62)
unreachable
64: ; preds = %56, %74
%65 = phi i8* [ %67, %74 ], [ %44, %56 ]
%66 = phi i32 [ %75, %74 ], [ 0, %56 ]
%67 = getelementptr inbounds i8, i8* %65, i64 1
%68 = load i8, i8* %65
%69 = zext i8 %68 to i32
%70 = and i32 %66, 255
%71 = icmp eq i32 %70, %69
br i1 %71, label %74, label %72
72: ; preds = %64
%73 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %66, i32 %69)
br label %74
74: ; preds = %64, %72
%75 = add nuw nsw i32 %66, 1
%76 = icmp eq i32 %75, %41
br i1 %76, label %77, label %64
77: ; preds = %74, %56
%78 = load i32, i32* @EXIT_SUCCESS
%79 = tail call i32 @exit(i32 %78)
unreachable
}