#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;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ causeSigbus(char *, int, long);
int /*<<< orphan*/ causeSigsegv(char *, long, int);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fstat(int, struct stat *);
int /*<<< orphan*/ helpAndExit(char *, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
long sysconf(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (!(argc == 3 && strlen(argv[2]) == 1 &&
        (argv[2][0] == 's' || argv[2][0] == 'b')))
    helpAndExit(argv[0], EXIT_FAILURE);
  long pagesize;
  int fd;
  char *mem;
  struct stat st;
  pagesize = sysconf(_SC_PAGESIZE);
  if (pagesize == -1)
    pexit("sysconf");
  fd = open(argv[1], O_RDONLY);
  if (fd == -1)
    pexit("open");
  if (fstat(fd, &st) == -1)
    pexit("fstat");
  mem = mmap(NULL, 2 * st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (mem == MAP_FAILED)
    pexit("mmap");
  switch (argv[2][0]) {
  case 's':
    printf("Causing SIGSEGV...\n");
    causeSigsegv(mem, pagesize, 2 * st.st_size);
    break;
  case 'b':
    printf("Causing SIGBUS...\n");
    causeSigbus(mem, st.st_size, pagesize);
    break;
  default:
    printf("Unreachable\n");
    exit(EXIT_FAILURE);
  }
  if (close(fd) == -1)
    pexit("close");
  if (munmap(mem, 2 * st.st_size) == -1)
    pexit("munmap");
  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)
cmpl   $0x3,-0x8(%rbp)
jne    122e <main+0x5e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jne    122e <main+0x5e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x73,%eax
je     1243 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x62,%eax
je     1243 <main+0x73>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
call   10b0 <helpAndExit@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    126c <main+0x9c>
lea    0xd99(%rip),%rdi
call   10d0 <pexit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    129b <main+0xcb>
lea    0xd72(%rip),%rdi
call   10d0 <pexit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10e0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    12bc <main+0xec>
lea    0xd56(%rip),%rdi
call   10d0 <pexit@plt>
mov    -0x30(%rbp),%esi
shl    $0x1,%esi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
mov    0x2d16(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2cbd(%rip),%rcx
cmp    (%rcx),%rax
jne    1308 <main+0x138>
lea    0xd10(%rip),%rdi
call   10d0 <pexit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
mov    %eax,-0x34(%rbp)
sub    $0x62,%eax
je     1359 <main+0x189>
jmp    1324 <main+0x154>
mov    -0x34(%rbp),%eax
sub    $0x73,%eax
jne    137a <main+0x1aa>
jmp    1335 <main+0x165>
lea    0xcdc(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%edx
shl    $0x1,%edx
call   1080 <causeSigsegv@plt>
jmp    1394 <main+0x1c4>
lea    0xccc(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   10f0 <causeSigbus@plt>
jmp    1394 <main+0x1c4>
lea    0xcbe(%rip),%rdi
call   1050 <printf@plt>
mov    0x2c33(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    13b1 <main+0x1e1>
lea    0xca0(%rip),%rdi
call   10d0 <pexit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%esi
shl    $0x1,%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    13d5 <main+0x205>
lea    0xc82(%rip),%rdi
call   10d0 <pexit@plt>
mov    0x2bdc(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
cmp    $0x3,%edi
jne    1201 <main+0x31>
mov    0x10(%r12),%rdi
call   1040 <strlen@plt>
cmp    $0x1,%eax
jne    1201 <main+0x31>
mov    0x10(%r12),%rax
mov    (%rax),%al
cmp    $0x62,%al
je     1213 <main+0x43>
cmp    $0x73,%al
je     1213 <main+0x43>
mov    (%r12),%rdi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
call   10b0 <helpAndExit@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %rax,%r14
cmp    $0xffffffffffffffff,%rax
jne    1236 <main+0x66>
lea    0xdcf(%rip),%rdi
call   10d0 <pexit@plt>
mov    0x8(%r12),%rdi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    125c <main+0x8c>
lea    0xdb1(%rip),%rdi
call   10d0 <pexit@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
call   10e0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1277 <main+0xa7>
lea    0xd9b(%rip),%rdi
call   10d0 <pexit@plt>
mov    (%rsp),%r13d
lea    0x0(%r13,%r13,1),%r15d
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    0x2d58(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d04(%rip),%rax
cmp    (%rax),%rbx
jne    12bd <main+0xed>
lea    0xd5b(%rip),%rdi
call   10d0 <pexit@plt>
mov    0x10(%r12),%rax
movsbl (%rax),%eax
cmp    $0x62,%eax
je     12eb <main+0x11b>
cmp    $0x73,%eax
jne    1347 <main+0x177>
lea    0xd61(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1080 <causeSigsegv@plt>
jmp    1305 <main+0x135>
lea    0xd33(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %r14,%rdx
call   10f0 <causeSigbus@plt>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    131d <main+0x14d>
lea    0xd00(%rip),%rdi
call   10d0 <pexit@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1339 <main+0x169>
lea    0xcea(%rip),%rdi
call   10d0 <pexit@plt>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xcfc(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EXIT_FAILURE = global i32 0
@_SC_PAGESIZE = global i32 0
@.str = [8 x i8] c"sysconf\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [20 x i8] c"Causing SIGSEGV...\0A\00"
@.str.5 = [19 x i8] c"Causing SIGBUS...\0A\00"
@.str.6 = [13 x i8] c"Unreachable\0A\00"
@.str.7 = [6 x i8] c"close\00"
@.str.8 = [7 x i8] c"munmap\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 3
br i1 %11, label %12, label %34
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 2
%15 = load i8*, i8** %14
%16 = call i32 @strlen(i8* %15)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %34
18: ; preds = %12
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 115
br i1 %25, label %40, label %26
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 98
br i1 %33, label %40, label %34
34: ; preds = %26, %12, %2
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @helpAndExit(i8* %37, i32 %38)
br label %40
40: ; preds = %34, %26, %18
%41 = load i32, i32* @_SC_PAGESIZE
%42 = call i64 @sysconf(i32 %41)
store i64 %42, i64* %6
%43 = load i64, i64* %6
%44 = icmp eq i64 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %47
47: ; preds = %45, %40
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = load i32, i32* @O_RDONLY
%52 = call i32 @open(i8* %50, i32 %51)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %47
%56 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %57
57: ; preds = %55, %47
%58 = load i32, i32* %7
%59 = call i32 @fstat(i32 %58, %struct.stat* %9)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %61, %57
%64 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%65 = load i32, i32* %64
%66 = mul nsw i32 2, %65
%67 = load i32, i32* @PROT_READ
%68 = load i32, i32* @MAP_SHARED
%69 = load i32, i32* %7
%70 = call i8* @mmap(i32* null, i32 %66, i32 %67, i32 %68, i32 %69, i32 0)
store i8* %70, i8** %8
%71 = load i8*, i8** %8
%72 = load i8*, i8** @MAP_FAILED
%73 = icmp eq i8* %71, %72
br i1 %73, label %74, label %76
74: ; preds = %63
%75 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %76
76: ; preds = %74, %63
%77 = load i8**, i8*** %5
%78 = getelementptr inbounds i8*, i8** %77, i64 2
%79 = load i8*, i8** %78
%80 = getelementptr inbounds i8, i8* %79, i64 0
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
switch i32 %82, label %98 [
i32 115, label %83
i32 98, label %91
]
83: ; preds = %76
%84 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%85 = load i8*, i8** %8
%86 = load i64, i64* %6
%87 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%88 = load i32, i32* %87
%89 = mul nsw i32 2, %88
%90 = call i32 @causeSigsegv(i8* %85, i64 %86, i32 %89)
br label %102
91: ; preds = %76
%92 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%93 = load i8*, i8** %8
%94 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%95 = load i32, i32* %94
%96 = load i64, i64* %6
%97 = call i32 @causeSigbus(i8* %93, i32 %95, i64 %96)
br label %102
98: ; preds = %76
%99 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%100 = load i32, i32* @EXIT_FAILURE
%101 = call i32 @exit(i32 %100)
unreachable
102: ; preds = %91, %83
%103 = load i32, i32* %7
%104 = call i32 @close(i32 %103)
%105 = icmp eq i32 %104, -1
br i1 %105, label %106, label %108
106: ; preds = %102
%107 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %108
108: ; preds = %106, %102
%109 = load i8*, i8** %8
%110 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%111 = load i32, i32* %110
%112 = mul nsw i32 2, %111
%113 = call i32 @munmap(i8* %109, i32 %112)
%114 = icmp eq i32 %113, -1
br i1 %114, label %115, label %117
115: ; preds = %108
%116 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %117
117: ; preds = %115, %108
%118 = load i32, i32* @EXIT_SUCCESS
%119 = call i32 @exit(i32 %118)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EXIT_FAILURE = global i32 0
@_SC_PAGESIZE = global i32 0
@.str = [8 x i8] c"sysconf\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.7 = [6 x i8] c"close\00"
@.str.8 = [7 x i8] c"munmap\00"
@EXIT_SUCCESS = global i32 0
@str = [18 x i8] c"Causing SIGBUS...\00"
@str.9 = [19 x i8] c"Causing SIGSEGV...\00"
@str.10 = [12 x i8] c"Unreachable\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 3
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 2
%7 = load i8*, i8** %6
%8 = tail call i32 @strlen(i8* %7)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i8*, i8** %6
%12 = load i8, i8* %11
switch i8 %12, label %13 [
i8 115, label %17
i8 98, label %17
]
13: ; preds = %10, %5, %2
%14 = load i8*, i8** %1
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @helpAndExit(i8* %14, i32 %15)
br label %17
17: ; preds = %10, %10, %13
%18 = bitcast %struct.stat* %3 to i8*
%19 = load i32, i32* @_SC_PAGESIZE
%20 = tail call i64 @sysconf(i32 %19)
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 @open(i8* %26, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = call i32 @fstat(i32 %28, %struct.stat* %3)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = tail call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%39 = load i32, i32* %38
%40 = shl nsw i32 %39, 1
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @MAP_SHARED
%43 = tail call i8* @mmap(i32* null, i32 %40, i32 %41, i32 %42, i32 %28, i32 0)
%44 = load i8*, i8** @MAP_FAILED
%45 = icmp eq i8* %43, %44
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %37
%49 = getelementptr inbounds i8*, i8** %1, i64 2
%50 = load i8*, i8** %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
switch i32 %52, label %59 [
i32 115, label %53
i32 98, label %56
]
53: ; preds = %48
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
%55 = tail call i32 @causeSigsegv(i8* %43, i64 %20, i32 %40)
br label %63
56: ; preds = %48
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%58 = tail call i32 @causeSigbus(i8* %43, i32 %39, i64 %20)
br label %63
59: ; preds = %48
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
%61 = load i32, i32* @EXIT_FAILURE
%62 = tail call i32 @exit(i32 %61)
unreachable
63: ; preds = %56, %53
%64 = tail call i32 @close(i32 %28)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = tail call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %68
68: ; preds = %66, %63
%69 = tail call i32 @munmap(i8* %43, i32 %40)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = tail call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %73
73: ; preds = %71, %68
%74 = load i32, i32* @EXIT_SUCCESS
%75 = tail call i32 @exit(i32 %74)
unreachable
}