#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 MAP_ANON;
int MAP_FIXED;
int MAP_PRIVATE;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
void *mmap(void *, int, int, int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mprotect(void *, int, int);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ puts(char *);
int rand();
int read(int /*<<< orphan*/, ...);
int /*<<< orphan*/ srand(unsigned int);
int /*<<< orphan*/ stdout;
void randop() {
  munmap((void *)0x0F000000, (4096 * 10));
  void *buf = mmap((void *)0x0F000000, (4096 * 10), PROT_READ | PROT_WRITE,
                   MAP_ANON | MAP_PRIVATE | MAP_FIXED, 0, 0);
  unsigned seed;
  if (read(0, &seed, 4) != 4)
    return;
  srand(seed);
  for (int i = 0; i < (4096 * 10) - 4; i += 3) {
    *(int *)&((char *)buf)[i] = rand();
    if (i % 66 == 0)
      ((char *)buf)[i] = 0xc3;
  }
  mprotect(buf, (4096 * 10), PROT_READ | PROT_EXEC);
  puts("ROP time!");
  fflush(stdout);
  size_t x, count = 0;
  do
    x = read(0, ((char *)&seed) + count, 555 - count);
  while (x > 0 && (count += x) < 555 && ((char *)&seed)[count - 1] != '\n');
}


x86 assembly (clang, -O0):
<randop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0xf000000,%edi
mov    $0xa000,%esi
call   1080 <munmap@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
mov    0x2e10(%rip),%rax
mov    (%rax),%ecx
mov    0x2dff(%rip),%rax
or     (%rax),%ecx
mov    0x2dee(%rip),%rax
or     (%rax),%ecx
mov    $0xf000000,%edi
mov    $0xa000,%esi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1040 <mmap@plt>
mov    %rax,-0x8(%rbp)
xor    %edi,%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <read@plt>
cmp    $0x4,%eax
je     11fd <randop+0x7d>
jmp    1311 <randop+0x191>
mov    -0xc(%rbp),%edi
call   1060 <srand@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x9ffc,-0x10(%rbp)
jge    1260 <randop+0xe0>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
mov    $0x42,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    124d <randop+0xcd>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0xc3,(%rax,%rcx,1)
jmp    1252 <randop+0xd2>
mov    -0x10(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x10(%rbp)
jmp    120c <randop+0x8c>
mov    -0x8(%rbp),%rdi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    0x2d6c(%rip),%rax
or     (%rax),%edx
mov    $0xa000,%esi
call   1090 <mprotect@plt>
lea    0xd79(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movq   $0x0,-0x20(%rbp)
lea    -0xc(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    $0x22b,%edx
sub    -0x20(%rbp),%rdx
xor    %edi,%edi
mov    $0x0,%al
call   1050 <read@plt>
cltq
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jbe    1306 <randop+0x186>
mov    -0x18(%rbp),%rcx
add    -0x20(%rbp),%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x22b,%rcx
mov    %al,-0x21(%rbp)
jae    1306 <randop+0x186>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
movsbl -0xc(%rbp,%rax,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    12a2 <randop+0x122>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xf000000,%edi
mov    $0xa000,%esi
call   1080 <munmap@plt>
mov    0x2e57(%rip),%rcx
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
or     (%rcx),%edx
mov    0x2e0d(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2deb(%rip),%rax
or     (%rax),%ecx
mov    $0xf000000,%edi
mov    $0xa000,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
lea    0x4(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <read@plt>
cmp    $0x4,%eax
jne    12d3 <randop+0x153>
mov    0x4(%rsp),%edi
call   1060 <srand@plt>
mov    $0xfffffffffffffffd,%rbx
xor    %r13d,%r13d
xor    %ebp,%ebp
jmp    1234 <randop+0xb4>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xfffffffd,%ebp
add    $0x3,%r13d
add    $0x3,%rbx
cmp    $0x9ff9,%rbx
jae    1264 <randop+0xe4>
movzwl %r13w,%eax
imul   $0xf83f,%eax,%eax
shr    $0x16,%eax
mov    %eax,%ecx
shl    $0x6,%ecx
lea    (%rcx,%rax,2),%r15d
xor    %eax,%eax
call   10a0 <rand@plt>
mov    %eax,0x3(%r12,%rbx,1)
add    %bp,%r15w
jne    1220 <randop+0xa0>
movb   $0xc3,0x3(%r12,%rbx,1)
jmp    1220 <randop+0xa0>
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
mov    0x2d84(%rip),%rax
or     (%rax),%edx
mov    %r12,%rdi
mov    $0xa000,%esi
call   1090 <mprotect@plt>
lea    0xd76(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
xor    %ebx,%ebx
nop
lea    (%rsp,%rbx,1),%rsi
add    $0x4,%rsi
mov    $0x22b,%edx
sub    %rbx,%rdx
xor    %edi,%edi
xor    %eax,%eax
call   1050 <read@plt>
test   %eax,%eax
je     12d3 <randop+0x153>
cltq
add    %rax,%rbx
cmp    $0x22a,%rbx
ja     12d3 <randop+0x153>
cmpb   $0xa,-0x1(%rbx,%r14,1)
jne    12a0 <randop+0x120>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FIXED = global i32 0
@PROT_EXEC = global i32 0
@.str = [10 x i8] c"ROP time!\00"
@stdout = global i32 0
define void @randop() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = call i32 @munmap(i8* inttoptr (i64 251658240 to i8*), i32 40960)
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %7, %8
%10 = load i32, i32* @MAP_ANON
%11 = load i32, i32* @MAP_PRIVATE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_FIXED
%14 = or i32 %12, %13
%15 = call i8* @mmap(i8* inttoptr (i64 251658240 to i8*), i32 40960, i32 %9, i32 %14, i32 0, i32 0)
store i8* %15, i8** %1
%16 = call i32 (i32, ...) @read(i32 0, i32* %2, i32 4)
%17 = icmp ne i32 %16, 4
br i1 %17, label %18, label %19
18: ; preds = %0
br label %79
19: ; preds = %0
%20 = load i32, i32* %2
%21 = call i32 @srand(i32 %20)
store i32 0, i32* %3
br label %22
22: ; preds = %41, %19
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 40956
br i1 %24, label %25, label %44
25: ; preds = %22
%26 = call i32 (...) @rand()
%27 = load i8*, i8** %1
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = bitcast i8* %30 to i32*
store i32 %26, i32* %31
%32 = load i32, i32* %3
%33 = srem i32 %32, 66
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %25
%36 = load i8*, i8** %1
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 -61, i8* %39
br label %40
40: ; preds = %35, %25
br label %41
41: ; preds = %40
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 3
store i32 %43, i32* %3
br label %22
44: ; preds = %22
%45 = load i8*, i8** %1
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @PROT_EXEC
%48 = or i32 %46, %47
%49 = call i32 @mprotect(i8* %45, i32 40960, i32 %48)
%50 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%51 = load i32, i32* @stdout
%52 = call i32 @fflush(i32 %51)
store i64 0, i64* %5
br label %53
53: ; preds = %77, %44
%54 = bitcast i32* %2 to i8*
%55 = load i64, i64* %5
%56 = getelementptr inbounds i8, i8* %54, i64 %55
%57 = load i64, i64* %5
%58 = sub i64 555, %57
%59 = call i32 (i32, ...) @read(i32 0, i8* %56, i64 %58)
%60 = sext i32 %59 to i64
store i64 %60, i64* %4
br label %61
61: ; preds = %53
%62 = load i64, i64* %4
%63 = icmp ugt i64 %62, 0
br i1 %63, label %64, label %77
64: ; preds = %61
%65 = load i64, i64* %4
%66 = load i64, i64* %5
%67 = add i64 %66, %65
store i64 %67, i64* %5
%68 = icmp ult i64 %67, 555
br i1 %68, label %69, label %77
69: ; preds = %64
%70 = bitcast i32* %2 to i8*
%71 = load i64, i64* %5
%72 = sub i64 %71, 1
%73 = getelementptr inbounds i8, i8* %70, i64 %72
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp ne i32 %75, 10
br label %77
77: ; preds = %69, %64, %61
%78 = phi i1 [ false, %64 ], [ false, %61 ], [ %76, %69 ]
br i1 %78, label %53, label %79
79: ; preds = %18, %77
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FIXED = global i32 0
@PROT_EXEC = global i32 0
@.str = [10 x i8] c"ROP time!\00"
@stdout = global i32 0
define void @randop() {
%1 = alloca i32
%2 = tail call i32 @munmap(i8* inttoptr (i64 251658240 to i8*), i32 40960)
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_ANON
%7 = load i32, i32* @MAP_PRIVATE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_FIXED
%10 = or i32 %8, %9
%11 = tail call i8* @mmap(i8* inttoptr (i64 251658240 to i8*), i32 40960, i32 %5, i32 %10, i32 0, i32 0)
%12 = bitcast i32* %1 to i8*
%13 = call i32 (i32, ...) @read(i32 0, i32* %1, i32 4)
%14 = icmp eq i32 %13, 4
br i1 %14, label %15, label %53
15: ; preds = %0
%16 = load i32, i32* %1
%17 = call i32 @srand(i32 %16)
br label %26
18: ; preds = %35
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_EXEC
%21 = or i32 %20, %19
%22 = call i32 @mprotect(i8* %11, i32 40960, i32 %21)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @stdout
%25 = call i32 @fflush(i32 %24)
br label %38
26: ; preds = %15, %35
%27 = phi i64 [ 0, %15 ], [ %36, %35 ]
%28 = call i32 (...) @rand()
%29 = getelementptr inbounds i8, i8* %11, i64 %27
%30 = bitcast i8* %29 to i32*
store i32 %28, i32* %30
%31 = trunc i64 %27 to i16
%32 = urem i16 %31, 66
%33 = icmp eq i16 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %26
store i8 -61, i8* %29
br label %35
35: ; preds = %26, %34
%36 = add nuw nsw i64 %27, 3
%37 = icmp ult i64 %27, 40953
br i1 %37, label %26, label %18
38: ; preds = %48, %18
%39 = phi i64 [ 0, %18 ], [ %46, %48 ]
%40 = getelementptr inbounds i8, i8* %12, i64 %39
%41 = sub i64 555, %39
%42 = call i32 (i32, ...) @read(i32 0, i8* %40, i64 %41)
%43 = icmp eq i32 %42, 0
br i1 %43, label %53, label %44
44: ; preds = %38
%45 = sext i32 %42 to i64
%46 = add i64 %39, %45
%47 = icmp ult i64 %46, 555
br i1 %47, label %48, label %53
48: ; preds = %44
%49 = add nsw i64 %46, -1
%50 = getelementptr inbounds i8, i8* %12, i64 %49
%51 = load i8, i8* %50
%52 = icmp eq i8 %51, 10
br i1 %52, label %53, label %38
53: ; preds = %48, %44, %38, %0
ret void
}