#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 int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ atol(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int ftruncate(int, int /*<<< orphan*/);
int getopt(int, char **, char *);
char *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int optind;
int /*<<< orphan*/ printf(char *, char *);
int shm_open(char *, int, int);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int c, fd, flags;
  char *ptr;
  off_t length;
  flags = O_RDWR | O_CREAT;
  while ((c = getopt(argc, argv, "e")) != -1) {
    switch (c) {
    case 'e':
      flags |= O_EXCL;
      break;
    }
  }
  if (argc - 2 != optind) {
    printf("usage: %s [-e] <name> <length>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  length = atol(argv[optind + 1]);
  if ((fd = shm_open(argv[optind], flags,
                     (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) == -1) {
    printf("shm_open error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (ftruncate(fd, length) == -1) {
    printf("ftruncate error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
      MAP_FAILED) {
    printf("mmap error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    0x2e52(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe47(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     11ef <main+0x6f>
mov    -0x14(%rbp),%eax
sub    $0x65,%eax
jne    11ea <main+0x6a>
jmp    11db <main+0x5b>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11ab <main+0x2b>
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%eax
je     1225 <main+0xa5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf0(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2d90(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <atol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d71(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
mov    0x2d5f(%rip),%rax
or     (%rax),%edx
mov    0x2d2e(%rip),%rax
or     (%rax),%edx
mov    0x2d1d(%rip),%rax
or     (%rax),%edx
call   1090 <shm_open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12b9 <main+0x139>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xd7c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12f8 <main+0x178>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xd51(%rip),%rdi
call   1040 <printf@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x2c(%rbp),%esi
mov    0x2cee(%rip),%rax
mov    (%rax),%edx
mov    0x2c7d(%rip),%rax
or     (%rax),%edx
mov    0x2ccc(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2c3f(%rip),%rcx
cmp    (%rcx),%rax
jne    1365 <main+0x1e5>
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xcf9(%rip),%rdi
call   1040 <printf@plt>
mov    0x2c32(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e3c(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%r15d
or     (%rax),%r15d
lea    0xe58(%rip),%r14
mov    0x2e01(%rip),%r12
jmp    11c0 <main+0x40>
or     (%r12),%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <getopt@plt>
cmp    $0x65,%eax
je     11b1 <main+0x31>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x40>
lea    -0x2(%rbp),%r12d
mov    0x2dde(%rip),%rax
cmp    (%rax),%r12d
jne    1249 <main+0xc9>
movslq %ebp,%rax
mov    -0x8(%rbx,%rax,8),%rdi
call   1060 <atol@plt>
mov    %eax,%r14d
mov    0x2d82(%rip),%rax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d90(%rip),%rax
or     (%rax),%edx
movslq %r12d,%rax
mov    (%rbx,%rax,8),%rdi
mov    0x2d78(%rip),%rax
or     (%rax),%edx
mov    %r15d,%esi
call   1090 <shm_open@plt>
cmp    $0xffffffff,%eax
jne    1255 <main+0xd5>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdde(%rip),%rdi
jmp    12c9 <main+0x149>
mov    (%rbx),%rsi
lea    0xdaf(%rip),%rdi
jmp    12cc <main+0x14c>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14d,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    127d <main+0xfd>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdbb(%rip),%rdi
jmp    12c9 <main+0x149>
mov    0x2d6c(%rip),%rax
mov    0x2cfd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d4a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2cc1(%rip),%rcx
cmp    (%rcx),%rax
jne    12df <main+0x15f>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd82(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [2 x i8] c"e\00"
@O_EXCL = global i32 0
@optind = global i32 0
@.str.1 = [32 x i8] c"usage: %s [-e] <name> <length>\0A\00"
@EXIT_FAILURE = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [20 x i8] c"shm_open error: %s\0A\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"ftruncate error: %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [16 x i8] c"mmap error: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
br label %6
6: ; preds = %10, %2
%7 = phi i32 [ %12, %10 ], [ %5, %2 ]
br label %8
8: ; preds = %6, %8
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %9, label %8 [
i32 -1, label %13
i32 101, label %10
]
10: ; preds = %8
%11 = load i32, i32* @O_EXCL
%12 = or i32 %11, %7
br label %6
13: ; preds = %8
%14 = add nsw i32 %0, -2
%15 = load i32, i32* @optind
%16 = icmp eq i32 %14, %15
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = load i8*, i8** %1
%19 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %13
%23 = add nsw i32 %0, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %1, i64 %24
%26 = load i8*, i8** %25
%27 = tail call i32 @atol(i8* %26)
%28 = sext i32 %14 to i64
%29 = getelementptr inbounds i8*, i8** %1, i64 %28
%30 = load i8*, i8** %29
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = load i32, i32* @S_IRGRP
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IROTH
%37 = or i32 %35, %36
%38 = tail call i32 @shm_open(i8* %30, i32 %7, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %46
40: ; preds = %22
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = tail call i32 @exit(i32 %44)
unreachable
46: ; preds = %22
%47 = tail call i32 @ftruncate(i32 %38, i32 %27)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32, i32* @errno
%51 = tail call i8* @strerror(i32 %50)
%52 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = load i32, i32* @EXIT_FAILURE
%54 = tail call i32 @exit(i32 %53)
unreachable
55: ; preds = %46
%56 = load i32, i32* @PROT_READ
%57 = load i32, i32* @PROT_WRITE
%58 = or i32 %57, %56
%59 = load i32, i32* @MAP_SHARED
%60 = tail call i8* @mmap(i32* null, i32 %27, i32 %58, i32 %59, i32 %38, i32 0)
%61 = load i8*, i8** @MAP_FAILED
%62 = icmp eq i8* %60, %61
br i1 %62, label %63, label %69
63: ; preds = %55
%64 = load i32, i32* @errno
%65 = tail call i8* @strerror(i32 %64)
%66 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %65)
%67 = load i32, i32* @EXIT_FAILURE
%68 = tail call i32 @exit(i32 %67)
unreachable
69: ; preds = %55
%70 = load i32, i32* @EXIT_SUCCESS
%71 = tail call i32 @exit(i32 %70)
unreachable
}