#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 atoi(int /*<<< orphan*/);
int getopt(int, char **, char *);
scalar_t__ malloc(int);
scalar_t__ nop(int);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ srand(int);
int main(int argc, char **argv) {
  int SEED = 2;
  int MEM_SIZE = 10 * 1024 * 1024;
  int DURATION = 6 * 1000 * 1000;
  int DELAY_OPS = 1000;
  int opt;
  while ((opt = getopt(argc, argv, "s:d:p:")) != -1) {
    switch (opt) {
    case 's':
      MEM_SIZE = 1024 * 1024 * atoi(optarg);
      break;
    case 'd':
      DURATION = atoi(optarg);
      break;
    case 'p':
      DELAY_OPS = atoi(optarg);
      break;
    default:
      break;
    }
  }
  int *mem = (int *)malloc(sizeof(int) * MEM_SIZE);
  int elapsed = 0;
  int tmp;
  int count = 0;
  srand(SEED);
  while (elapsed < DURATION) {
    int read_addr = count % MEM_SIZE;
    tmp += mem[read_addr];
    elapsed += DELAY_OPS;
    tmp += nop(DELAY_OPS);
    count += 16;
  }
  printf("Sum is %d\n", tmp);
}


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   $0x2,-0x14(%rbp)
movl   $0xa00000,-0x18(%rbp)
movl   $0x5b8d80,-0x1c(%rbp)
movl   $0x3e8,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe60(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1236 <main+0xd6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0x64,%eax
je     1200 <main+0xa0>
jmp    11c5 <main+0x65>
mov    -0x44(%rbp),%eax
sub    $0x70,%eax
je     1216 <main+0xb6>
jmp    11d6 <main+0x76>
mov    -0x44(%rbp),%eax
sub    $0x73,%eax
jne    122c <main+0xcc>
jmp    11e7 <main+0x87>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
shl    $0x14,%eax
mov    %eax,-0x18(%rbp)
jmp    1231 <main+0xd1>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    1231 <main+0xd1>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
jmp    1231 <main+0xd1>
jmp    1231 <main+0xd1>
jmp    1192 <main+0x32>
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <srand@plt>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12b2 <main+0x152>
mov    -0x3c(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %edx,-0x40(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <nop@plt>
mov    %rax,%rcx
movslq -0x38(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x3c(%rbp)
jmp    125f <main+0xff>
mov    -0x38(%rbp),%esi
lea    0xd4b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%ebx
mov    $0xa00000,%r15d
mov    $0x5b8d80,%r14d
mov    $0x3e8,%r13d
lea    0xe77(%rip),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1070 <getopt@plt>
cmp    $0x6f,%eax
jg     11c0 <main+0x60>
cmp    $0xffffffff,%eax
je     11f4 <main+0x94>
cmp    $0x64,%eax
jne    1190 <main+0x30>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
mov    %eax,%r14d
jmp    1190 <main+0x30>
nop
cmp    $0x70,%eax
je     11e1 <main+0x81>
cmp    $0x73,%eax
jne    1190 <main+0x30>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
mov    %eax,%r15d
shl    $0x14,%r15d
jmp    1190 <main+0x30>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
mov    %eax,%r13d
jmp    1190 <main+0x30>
lea    0x0(,%r15,4),%edi
call   1060 <malloc@plt>
mov    %rax,(%rsp)
mov    $0x2,%edi
call   1050 <srand@plt>
xor    %ebp,%ebp
xor    %ebx,%ebx
mov    %r13d,%r12d
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%eax
cltd
idiv   %r15d
mov    %r15,%r13
mov    %r14d,%r15d
mov    %esi,%r14d
mov    (%rsp),%rax
add    (%rax,%rdx,4),%r14d
add    %r12d,%ebx
mov    %r12d,%edi
call   1040 <nop@plt>
mov    %rax,%rsi
add    %r14d,%esi
mov    %r15d,%r14d
mov    %r13,%r15
add    $0x10,%ebp
cmp    %r14d,%ebx
jl     1220 <main+0xc0>
lea    0xdaa(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"s:d:p:\00"
@optarg = global i32 0
@.str.1 = [11 x i8] c"Sum is %d\0A\00"
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 i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 2, i32* %6
store i32 10485760, i32* %7
store i32 6000000, i32* %8
store i32 1000, i32* %9
br label %16
16: ; preds = %34, %2
%17 = load i32, i32* %4
%18 = load i8**, i8*** %5
%19 = call i32 @getopt(i32 %17, i8** %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %19, i32* %10
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %35
21: ; preds = %16
%22 = load i32, i32* %10
switch i32 %22, label %33 [
i32 115, label %23
i32 100, label %27
i32 112, label %30
]
23: ; preds = %21
%24 = load i32, i32* @optarg
%25 = call i32 @atoi(i32 %24)
%26 = mul nsw i32 1048576, %25
store i32 %26, i32* %7
br label %34
27: ; preds = %21
%28 = load i32, i32* @optarg
%29 = call i32 @atoi(i32 %28)
store i32 %29, i32* %8
br label %34
30: ; preds = %21
%31 = load i32, i32* @optarg
%32 = call i32 @atoi(i32 %31)
store i32 %32, i32* %9
br label %34
33: ; preds = %21
br label %34
34: ; preds = %33, %30, %27, %23
br label %16
35: ; preds = %16
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = mul i64 4, %37
%39 = trunc i64 %38 to i32
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i32*
store i32* %41, i32** %11
store i32 0, i32* %12
store i32 0, i32* %14
%42 = load i32, i32* %6
%43 = call i32 @srand(i32 %42)
br label %44
44: ; preds = %48, %35
%45 = load i32, i32* %12
%46 = load i32, i32* %8
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %70
48: ; preds = %44
%49 = load i32, i32* %14
%50 = load i32, i32* %7
%51 = srem i32 %49, %50
store i32 %51, i32* %15
%52 = load i32*, i32** %11
%53 = load i32, i32* %15
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = load i32, i32* %13
%58 = add nsw i32 %57, %56
store i32 %58, i32* %13
%59 = load i32, i32* %9
%60 = load i32, i32* %12
%61 = add nsw i32 %60, %59
store i32 %61, i32* %12
%62 = load i32, i32* %9
%63 = call i64 @nop(i32 %62)
%64 = load i32, i32* %13
%65 = sext i32 %64 to i64
%66 = add nsw i64 %65, %63
%67 = trunc i64 %66 to i32
store i32 %67, i32* %13
%68 = load i32, i32* %14
%69 = add nsw i32 %68, 16
store i32 %69, i32* %14
br label %44
70: ; preds = %44
%71 = load i32, i32* %13
%72 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %71)
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [7 x i8] c"s:d:p:\00"
@optarg = global i32 0
@.str.1 = [11 x i8] c"Sum is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %21, %2
%4 = phi i32 [ %23, %21 ], [ 1000, %2 ]
%5 = phi i32 [ %8, %21 ], [ 6000000, %2 ]
%6 = phi i32 [ %11, %21 ], [ 10485760, %2 ]
br label %7
7: ; preds = %3, %18
%8 = phi i32 [ %5, %3 ], [ %20, %18 ]
%9 = phi i32 [ %6, %3 ], [ %11, %18 ]
br label %10
10: ; preds = %7, %14
%11 = phi i32 [ %9, %7 ], [ %17, %14 ]
br label %12
12: ; preds = %10, %12
%13 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
switch i32 %13, label %12 [
i32 -1, label %24
i32 115, label %14
i32 100, label %18
i32 112, label %21
]
14: ; preds = %12
%15 = load i32, i32* @optarg
%16 = tail call i32 @atoi(i32 %15)
%17 = shl nsw i32 %16, 20
br label %10
18: ; preds = %12
%19 = load i32, i32* @optarg
%20 = tail call i32 @atoi(i32 %19)
br label %7
21: ; preds = %12
%22 = load i32, i32* @optarg
%23 = tail call i32 @atoi(i32 %22)
br label %3
24: ; preds = %12
%25 = shl i32 %11, 2
%26 = tail call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i32*
%28 = tail call i32 @srand(i32 2)
%29 = icmp sgt i32 %8, 0
br label %30
30: ; preds = %24, %30
%31 = phi i32 [ %43, %30 ], [ 0, %24 ]
%32 = phi i32 [ %42, %30 ], [ undef, %24 ]
%33 = phi i32 [ %39, %30 ], [ 0, %24 ]
%34 = srem i32 %31, %11
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %27, i64 %35
%37 = load i32, i32* %36
%38 = add nsw i32 %37, %32
%39 = add nsw i32 %33, %4
%40 = tail call i64 @nop(i32 %4)
%41 = trunc i64 %40 to i32
%42 = add i32 %38, %41
%43 = add nuw nsw i32 %31, 16
%44 = icmp slt i32 %39, %8
br i1 %44, label %30, label %45
45: ; preds = %30
%46 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %42)
ret i32 0
}