#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 */
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, offset = 0;
  if (argc < 2) {
    hsize = 1024;
    name = "test2";
  } else if (argc < 3) {
    hsize = 1024;
    name = argv[1];
  } else if (argc < 4) {
    hsize = atoi(argv[2]);
    name = argv[1];
  } else {
    offset = atoi(argv[3]);
    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 ((long)p == -1L) {
    perror("mmap");
    close(fd);
    exit(0);
  }
  srand(getseed());
  count = (unsigned)rand() % (hsize / 10);
  for (i = 0; i < count; i++) {
    do {
      off = offset + (rand() % hsize);
    } while (off >= 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    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11de <main+0x3e>
movl   $0x400,-0x3c(%rbp)
lea    0xe2b(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1261 <main+0xc1>
cmpl   $0x3,-0x8(%rbp)
jge    1200 <main+0x60>
movl   $0x400,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    125c <main+0xbc>
cmpl   $0x4,-0x8(%rbp)
jge    122b <main+0x8b>
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    1257 <main+0xb7>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x40(%rbp)
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    125c <main+0xbc>
jmp    1261 <main+0xc1>
mov    -0x28(%rbp),%rdi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1293 <main+0xf3>
lea    0xd7f(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x3c(%rbp),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
mov    0x2d22(%rip),%rax
or     (%rax),%edx
mov    0x2d39(%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
cmp    $0xffffffffffffffff,%rax
jne    12ed <main+0x14d>
lea    0xd32(%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,-0x44(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
xor    %edx,%edx
div    %ecx
mov    %edx,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x30(%rbp),%eax
jae    13bd <main+0x21d>
jmp    1335 <main+0x195>
mov    -0x40(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   10c0 <rand@plt>
xor    %edx,%edx
divl   -0x3c(%rbp)
mov    -0x48(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jae    1335 <main+0x195>
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     13a2 <main+0x202>
movzbl -0x29(%rbp),%eax
cmp    $0x80,%eax
jge    13a2 <main+0x202>
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    1324 <main+0x184>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1070 <munmap@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jge    11c4 <main+0x24>
xor    %r13d,%r13d
mov    $0x400,%ebp
lea    0xe3e(%rip),%rdi
jmp    11f6 <main+0x56>
mov    %rsi,%r14
jne    11d3 <main+0x33>
xor    %r13d,%r13d
mov    $0x400,%ebp
jmp    11f2 <main+0x52>
xor    %r13d,%r13d
cmp    $0x4,%edi
jb     11e7 <main+0x47>
mov    0x18(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r13d
mov    0x10(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
mov    0x8(%r14),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     12f5 <main+0x155>
mov    0x2de5(%rip),%rsi
mov    0x2dae(%rip),%rcx
mov    (%rcx),%edx
or     (%rsi),%edx
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %eax,0xc(%rsp)
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,0x10(%rsp)
cmp    $0xffffffffffffffff,%rax
je     1308 <main+0x168>
xor    %eax,%eax
call   1040 <getseed@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %eax,%eax
call   10c0 <rand@plt>
mov    %ebp,%ecx
mov    $0xcccccccd,%esi
imul   %rcx,%rsi
shr    $0x23,%rsi
xor    %edx,%edx
div    %esi
test   %edx,%edx
je     12cf <main+0x12f>
mov    %edx,%r12d
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10c0 <rand@plt>
xor    %edx,%edx
div    %ebp
mov    %edx,%ebx
add    %r13d,%ebx
cmp    %ebp,%ebx
jae    1280 <main+0xe0>
xor    %eax,%eax
call   10c0 <rand@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10c0 <rand@plt>
mov    %r14d,%ecx
or     $0x80,%cl
movzbl %cl,%ecx
test   $0x80,%r14b
cmovne %r14d,%ecx
test   $0x1,%al
cmove  %r14d,%ecx
mov    %ebx,%eax
mov    0x10(%rsp),%rdx
mov    %cl,(%rdx,%rax,1)
add    $0x1,%r15d
cmp    %r12d,%r15d
jb     1280 <main+0xe0>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%rdi
mov    %ebp,%esi
call   1070 <munmap@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0a(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xcfc(%rip),%rdi
call   1090 <perror@plt>
mov    0xc(%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
@.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
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %14
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %18
17: ; 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 %50
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %25
21: ; preds = %18
store i32 1024, i32* %13
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %8
br label %49
25: ; preds = %18
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 4
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %13
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
store i8* %35, i8** %8
br label %48
36: ; preds = %25
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 3
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %14
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i32 @atoi(i8* %43)
store i32 %44, i32* %13
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
store i8* %47, i8** %8
br label %48
48: ; preds = %36, %28
br label %49
49: ; preds = %48, %21
br label %50
50: ; preds = %49, %17
%51 = load i8*, i8** %8
%52 = load i32, i32* @O_RDWR
%53 = call i32 @open(i8* %51, i32 %52)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%58 = call i32 @exit(i32 0)
unreachable
59: ; preds = %50
%60 = load i32, i32* %13
%61 = load i32, i32* @PROT_READ
%62 = load i32, i32* @PROT_WRITE
%63 = or i32 %61, %62
%64 = load i32, i32* @MAP_SHARED
%65 = load i32, i32* %6
%66 = call i8* @mmap(i32 0, i32 %60, i32 %63, i32 %64, i32 %65, i32 0)
store i8* %66, i8** %7
%67 = load i8*, i8** %7
%68 = ptrtoint i8* %67 to i64
%69 = icmp eq i64 %68, -1
br i1 %69, label %70, label %75
70: ; preds = %59
%71 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%72 = load i32, i32* %6
%73 = call i32 @close(i32 %72)
%74 = call i32 @exit(i32 0)
unreachable
75: ; preds = %59
%76 = call i32 (...) @getseed()
%77 = call i32 @srand(i32 %76)
%78 = call i32 (...) @rand()
%79 = load i32, i32* %13
%80 = udiv i32 %79, 10
%81 = urem i32 %78, %80
store i32 %81, i32* %10
store i32 0, i32* %11
br label %82
82: ; preds = %119, %75
%83 = load i32, i32* %11
%84 = load i32, i32* %10
%85 = icmp ult i32 %83, %84
br i1 %85, label %86, label %122
86: ; preds = %82
br label %87
87: ; preds = %93, %86
%88 = load i32, i32* %14
%89 = call i32 (...) @rand()
%90 = load i32, i32* %13
%91 = urem i32 %89, %90
%92 = add i32 %88, %91
store i32 %92, i32* %12
br label %93
93: ; preds = %87
%94 = load i32, i32* %12
%95 = load i32, i32* %13
%96 = icmp uge i32 %94, %95
br i1 %96, label %87, label %97
97: ; preds = %93
%98 = call i32 (...) @rand()
%99 = srem i32 %98, 256
%100 = trunc i32 %99 to i8
store i8 %100, i8* %9
%101 = call i32 (...) @rand()
%102 = srem i32 %101, 2
%103 = icmp ne i32 %102, 0
br i1 %103, label %104, label %113
104: ; preds = %97
%105 = load i8, i8* %9
%106 = zext i8 %105 to i32
%107 = icmp slt i32 %106, 128
br i1 %107, label %108, label %113
108: ; preds = %104
%109 = load i8, i8* %9
%110 = zext i8 %109 to i32
%111 = or i32 %110, 128
%112 = trunc i32 %111 to i8
store i8 %112, i8* %9
br label %113
113: ; preds = %108, %104, %97
%114 = load i8, i8* %9
%115 = load i8*, i8** %7
%116 = load i32, i32* %12
%117 = zext i32 %116 to i64
%118 = getelementptr inbounds i8, i8* %115, i64 %117
store i8 %114, i8* %118
br label %119
119: ; preds = %113
%120 = load i32, i32* %11
%121 = add i32 %120, 1
store i32 %121, i32* %11
br label %82
122: ; preds = %82
%123 = load i32, i32* %6
%124 = call i32 @close(i32 %123)
%125 = load i8*, i8** %7
%126 = load i32, i32* %13
%127 = call i32 @munmap(i8* %125, i32 %126)
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
@.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 %22, label %4
4: ; preds = %2
%5 = icmp eq i32 %0, 2
br i1 %5, label %17, label %6
6: ; preds = %4
%7 = icmp ult i32 %0, 4
br i1 %7, label %12, label %8
8: ; preds = %6
%9 = getelementptr inbounds i8*, i8** %1, i64 3
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
br label %12
12: ; preds = %6, %8
%13 = phi i32 [ %11, %8 ], [ 0, %6 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
br label %17
17: ; preds = %12, %4
%18 = phi i32 [ 1024, %4 ], [ %16, %12 ]
%19 = phi i32 [ 0, %4 ], [ %13, %12 ]
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
br label %22
22: ; preds = %17, %2
%23 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %2 ], [ %21, %17 ]
%24 = phi i32 [ 1024, %2 ], [ %18, %17 ]
%25 = phi i32 [ 0, %2 ], [ %19, %17 ]
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 @open(i8* %23, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = tail call i32 @exit(i32 0)
unreachable
32: ; preds = %22
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_SHARED
%37 = tail call i8* @mmap(i32 0, i32 %24, i32 %35, i32 %36, i32 %27, i32 0)
%38 = icmp eq i8* %37, inttoptr (i64 -1 to i8*)
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%41 = tail call i32 @close(i32 %27)
%42 = tail call i32 @exit(i32 0)
unreachable
43: ; preds = %32
%44 = tail call i32 (...) @getseed()
%45 = tail call i32 @srand(i32 %44)
%46 = tail call i32 (...) @rand()
%47 = udiv i32 %24, 10
%48 = urem i32 %46, %47
%49 = icmp eq i32 %48, 0
br i1 %49, label %72, label %50
50: ; preds = %43, %57
%51 = phi i32 [ %70, %57 ], [ 0, %43 ]
br label %52
52: ; preds = %50, %52
%53 = tail call i32 (...) @rand()
%54 = urem i32 %53, %24
%55 = add i32 %54, %25
%56 = icmp ult i32 %55, %24
br i1 %56, label %57, label %52
57: ; preds = %52
%58 = tail call i32 (...) @rand()
%59 = tail call i32 (...) @rand()
%60 = and i32 %59, 1
%61 = icmp ne i32 %60, 0
%62 = and i32 %58, 128
%63 = icmp eq i32 %62, 0
%64 = select i1 %61, i1 %63, i1 false
%65 = or i32 %58, 128
%66 = select i1 %64, i32 %65, i32 %58
%67 = trunc i32 %66 to i8
%68 = zext i32 %55 to i64
%69 = getelementptr inbounds i8, i8* %37, i64 %68
store i8 %67, i8* %69
%70 = add nuw i32 %51, 1
%71 = icmp ult i32 %70, %48
br i1 %71, label %50, label %72
72: ; preds = %57, %43
%73 = tail call i32 @close(i32 %27)
%74 = tail call i32 @munmap(i8* %37, i32 %24)
ret i32 0
}