#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 */

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
unsigned int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getseed();
char *mmap(int /*<<< orphan*/, unsigned int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd;
  char *p, *name;
  unsigned char c;
  unsigned int count, i, off, hsize;
  if (argc < 2) {
    hsize = 1024;
    name = "test2";
  } else if (argc < 3) {
    hsize = 1024;
    name = argv[1];
  } else {
    hsize = atoi(argv[2]);
    name = argv[1];
  }
  fd = open(name, O_RDWR);
  if (fd < 0) {
    perror("open");
    exit(0);
  }
  p = mmap(0, hsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (p == MAP_FAILED) {
    perror("mmap");
    close(fd);
    exit(0);
  }
  srand(getseed());
  count = (unsigned)rand() % (hsize / 10);
  for (i = 0; i < count; i++) {
    off = rand() % hsize;
    c = rand() % 256;
    if ((rand() % 2) && c < 128)
      c |= 0x80;
    p[off] = c;
  }
  close(fd);
  munmap(p, hsize);
  return 0;
}


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   $0x2,-0x8(%rbp)
jge    11d7 <main+0x37>
movl   $0x400,-0x3c(%rbp)
lea    0xe32(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    121a <main+0x7a>
cmpl   $0x3,-0x8(%rbp)
jge    11f9 <main+0x59>
movl   $0x400,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1215 <main+0x75>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    121a <main+0x7a>
mov    -0x28(%rbp),%rdi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124c <main+0xac>
lea    0xdc6(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x3c(%rbp),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
mov    0x2d69(%rip),%rax
or     (%rax),%edx
mov    0x2d80(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d38(%rip),%rcx
cmp    (%rcx),%rax
jne    12ac <main+0x10c>
lea    0xd73(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1040 <getseed@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x0,%al
call   10c0 <rand@plt>
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
xor    %edx,%edx
div    %ecx
mov    %edx,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x30(%rbp),%eax
jae    1360 <main+0x1c0>
mov    $0x0,%al
call   10c0 <rand@plt>
xor    %edx,%edx
divl   -0x3c(%rbp)
mov    %edx,-0x38(%rbp)
mov    $0x0,%al
call   10c0 <rand@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x29(%rbp)
mov    $0x0,%al
call   10c0 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1345 <main+0x1a5>
movzbl -0x29(%rbp),%eax
cmp    $0x80,%eax
jge    1345 <main+0x1a5>
movzbl -0x29(%rbp),%eax
or     $0x80,%eax
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12e3 <main+0x143>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1070 <munmap@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jge    11bf <main+0x1f>
mov    $0x400,%r15d
lea    0xe43(%rip),%rdi
jmp    11da <main+0x3a>
mov    %rsi,%rbx
mov    $0x400,%r15d
je     11d6 <main+0x36>
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r15d
mov    0x8(%rbx),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     12d3 <main+0x133>
mov    0x2e01(%rip),%rsi
mov    0x2dca(%rip),%rcx
mov    (%rcx),%edx
or     (%rsi),%edx
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %eax,0x4(%rsp)
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r12
mov    0x2d97(%rip),%rax
cmp    (%rax),%r12
je     12e6 <main+0x146>
xor    %eax,%eax
call   1040 <getseed@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %eax,%eax
call   10c0 <rand@plt>
mov    %r15d,%ecx
mov    $0xcccccccd,%esi
imul   %rcx,%rsi
shr    $0x23,%rsi
xor    %edx,%edx
div    %esi
test   %edx,%edx
je     12ae <main+0x10e>
mov    %edx,%r13d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   10c0 <rand@plt>
xor    %edx,%edx
div    %r15d
mov    %edx,%ebp
xor    %eax,%eax
call   10c0 <rand@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <rand@plt>
mov    %ebx,%ecx
or     $0x80,%cl
movzbl %cl,%ecx
test   $0x80,%bl
cmovne %ebx,%ecx
test   $0x1,%al
cmove  %ebx,%ecx
mov    %cl,(%r12,%rbp,1)
add    $0x1,%r14d
cmp    %r13d,%r14d
jb     1270 <main+0xd0>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   1070 <munmap@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2c(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd1e(%rip),%rdi
call   1090 <perror@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"test2\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 1024, i32* %13
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %8
br label %33
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 3
br i1 %19, label %20, label %24
20: ; preds = %17
store i32 1024, i32* %13
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %8
br label %32
24: ; preds = %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %13
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
store i8* %31, i8** %8
br label %32
32: ; preds = %24, %20
br label %33
33: ; preds = %32, %16
%34 = load i8*, i8** %8
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %33
%43 = load i32, i32* %13
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @PROT_WRITE
%46 = or i32 %44, %45
%47 = load i32, i32* @MAP_SHARED
%48 = load i32, i32* %6
%49 = call i8* @mmap(i32 0, i32 %43, i32 %46, i32 %47, i32 %48, i32 0)
store i8* %49, i8** %7
%50 = load i8*, i8** %7
%51 = load i8*, i8** @MAP_FAILED
%52 = icmp eq i8* %50, %51
br i1 %52, label %53, label %58
53: ; preds = %42
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
%57 = call i32 @exit(i32 0)
unreachable
58: ; preds = %42
%59 = call i32 (...) @getseed()
%60 = call i32 @srand(i32 %59)
%61 = call i32 (...) @rand()
%62 = load i32, i32* %13
%63 = udiv i32 %62, 10
%64 = urem i32 %61, %63
store i32 %64, i32* %10
store i32 0, i32* %11
br label %65
65: ; preds = %94, %58
%66 = load i32, i32* %11
%67 = load i32, i32* %10
%68 = icmp ult i32 %66, %67
br i1 %68, label %69, label %97
69: ; preds = %65
%70 = call i32 (...) @rand()
%71 = load i32, i32* %13
%72 = urem i32 %70, %71
store i32 %72, i32* %12
%73 = call i32 (...) @rand()
%74 = srem i32 %73, 256
%75 = trunc i32 %74 to i8
store i8 %75, i8* %9
%76 = call i32 (...) @rand()
%77 = srem i32 %76, 2
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %88
79: ; preds = %69
%80 = load i8, i8* %9
%81 = zext i8 %80 to i32
%82 = icmp slt i32 %81, 128
br i1 %82, label %83, label %88
83: ; preds = %79
%84 = load i8, i8* %9
%85 = zext i8 %84 to i32
%86 = or i32 %85, 128
%87 = trunc i32 %86 to i8
store i8 %87, i8* %9
br label %88
88: ; preds = %83, %79, %69
%89 = load i8, i8* %9
%90 = load i8*, i8** %7
%91 = load i32, i32* %12
%92 = zext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %90, i64 %92
store i8 %89, i8* %93
br label %94
94: ; preds = %88
%95 = load i32, i32* %11
%96 = add i32 %95, 1
store i32 %96, i32* %11
br label %65
97: ; preds = %65
%98 = load i32, i32* %6
%99 = call i32 @close(i32 %98)
%100 = load i8*, i8** %7
%101 = load i32, i32* %13
%102 = call i32 @munmap(i8* %100, i32 %101)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"test2\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %4
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
br label %10
10: ; preds = %4, %6
%11 = phi i32 [ %9, %6 ], [ 1024, %4 ]
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
br label %14
14: ; preds = %10, %2
%15 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %2 ], [ %13, %10 ]
%16 = phi i32 [ 1024, %2 ], [ %11, %10 ]
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i8* %15, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %14
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %25, %24
%27 = load i32, i32* @MAP_SHARED
%28 = tail call i8* @mmap(i32 0, i32 %16, i32 %26, i32 %27, i32 %18, i32 0)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %35
31: ; preds = %23
%32 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @close(i32 %18)
%34 = tail call i32 @exit(i32 0)
unreachable
35: ; preds = %23
%36 = tail call i32 (...) @getseed()
%37 = tail call i32 @srand(i32 %36)
%38 = tail call i32 (...) @rand()
%39 = udiv i32 %16, 10
%40 = urem i32 %38, %39
%41 = icmp eq i32 %40, 0
br i1 %41, label %60, label %42
42: ; preds = %35, %42
%43 = phi i32 [ %58, %42 ], [ 0, %35 ]
%44 = tail call i32 (...) @rand()
%45 = urem i32 %44, %16
%46 = tail call i32 (...) @rand()
%47 = tail call i32 (...) @rand()
%48 = and i32 %47, 1
%49 = icmp ne i32 %48, 0
%50 = and i32 %46, 128
%51 = icmp eq i32 %50, 0
%52 = select i1 %49, i1 %51, i1 false
%53 = or i32 %46, 128
%54 = select i1 %52, i32 %53, i32 %46
%55 = trunc i32 %54 to i8
%56 = zext i32 %45 to i64
%57 = getelementptr inbounds i8, i8* %28, i64 %56
store i8 %55, i8* %57
%58 = add nuw i32 %43, 1
%59 = icmp ult i32 %58, %40
br i1 %59, label %42, label %60
60: ; preds = %42, %35
%61 = tail call i32 @close(i32 %18)
%62 = tail call i32 @munmap(i8* %28, i32 %16)
ret i32 0
}