#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 */
typedef int uint64_t;
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ dowork(char *, size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ timediff(struct timeval *, struct timeval *,
                            struct timeval *);
int main(int argc, char *argv[]) {
  size_t sz = (32ULL << 30);
  int i;
  int nservers = atoi(argv[1]);
  size_t szpervm = sz / nservers;
  size_t slicesz = (1ULL << 30) / nservers;
  if (slicesz > (256 << 20))
    slicesz = 256 << 20;
  int count = szpervm / slicesz;
  printf("Severs: %d slicesz: %ld MB, Count: %d\n", nservers, (slicesz >> 20),
         count);
  char *ptr1 = malloc(slicesz);
  struct timeval start, end, elp;
  gettimeofday(&start, NULL);
  for (i = 0; i < count; i++)
    (void)dowork(ptr1, slicesz);
  gettimeofday(&end, NULL);
  timediff(&start, &end, &elp);
  uint64_t elapsed = (elp.tv_sec * 1000) + (elp.tv_usec / 1000);
  printf("%ld\n", elapsed);
  free(ptr1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movabs $0x800000000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x28(%rbp)
movslq -0x20(%rbp),%rcx
mov    $0x40000000,%eax
xor    %edx,%edx
div    %rcx
mov    %rax,-0x30(%rbp)
cmpq   $0x10000000,-0x30(%rbp)
10
jbe    11dd <main+0x6d>
movq   $0x10000000,-0x30(%rbp)
10
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x30(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x30(%rbp),%rdx
shr    $0x14,%rdx
mov    -0x34(%rbp),%ecx
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1250 <main+0xe0>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <dowork@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1229 <main+0xb9>
lea    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x48(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x58(%rbp),%rdx
call   1090 <timediff@plt>
imul   $0x3e8,-0x58(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x54(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x60(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%esi
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%esi
movslq %eax,%rbx
xor    %edi,%edi
movabs $0x800000000,%rax
xor    %edx,%edx
div    %rbx
mov    %rax,%rcx
cmp    $0x40000000,%rbx
ja     11b1 <main+0x41>
mov    $0x40000000,%eax
xor    %edx,%edx
div    %esi
mov    %eax,%edi
cmp    $0x10000000,%rdi
mov    $0x10000000,%r15d
cmovb  %rdi,%r15
mov    %rcx,%rax
shr    $0x20,%rax
je     11d8 <main+0x68>
mov    %rcx,%rax
xor    %edx,%edx
div    %r15
mov    %rax,%r12
jmp    11e2 <main+0x72>
mov    %ecx,%eax
xor    %edx,%edx
div    %r15d
mov    %eax,%r12d
mov    %r15,%rdx
shr    $0x14,%rdx
lea    0xe10(%rip),%rdi
xor    %ebx,%ebx
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %r12d,%r12d
jle    1233 <main+0xc3>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <dowork@plt>
add    $0x1,%ebx
cmp    %r12d,%ebx
jne    1220 <main+0xb0>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x8(%rsp),%rdi
mov    %rsp,%rdx
mov    %rbx,%rsi
call   1090 <timediff@plt>
imul   $0x3e8,(%rsp),%eax
movslq 0x4(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rsi
mov    %rsi,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rsi
add    %ecx,%esi
add    %eax,%esi
lea    0xdac(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [39 x i8] c"Severs: %d slicesz: %ld MB, Count: %d\0A\00"
@.str.1 = [5 x i8] c"%ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca %struct.timeval
%14 = alloca %struct.timeval
%15 = alloca %struct.timeval
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 34359738368, i64* %6
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %8
%21 = load i64, i64* %6
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = udiv i64 %21, %23
store i64 %24, i64* %9
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = udiv i64 1073741824, %26
store i64 %27, i64* %10
%28 = load i64, i64* %10
%29 = icmp ugt i64 %28, 268435456
br i1 %29, label %30, label %31
30: ; preds = %2
store i64 268435456, i64* %10
br label %31
31: ; preds = %30, %2
%32 = load i64, i64* %9
%33 = load i64, i64* %10
%34 = udiv i64 %32, %33
%35 = trunc i64 %34 to i32
store i32 %35, i32* %11
%36 = load i32, i32* %8
%37 = load i64, i64* %10
%38 = lshr i64 %37, 20
%39 = load i32, i32* %11
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %36, i64 %38, i32 %39)
%41 = load i64, i64* %10
%42 = call i8* @malloc(i64 %41)
store i8* %42, i8** %12
%43 = call i32 @gettimeofday(%struct.timeval* %13, i32* null)
store i32 0, i32* %7
br label %44
44: ; preds = %52, %31
%45 = load i32, i32* %7
%46 = load i32, i32* %11
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = load i8*, i8** %12
%50 = load i64, i64* %10
%51 = call i32 @dowork(i8* %49, i64 %50)
br label %52
52: ; preds = %48
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
store i32 %54, i32* %7
br label %44
55: ; preds = %44
%56 = call i32 @gettimeofday(%struct.timeval* %14, i32* null)
%57 = call i32 @timediff(%struct.timeval* %13, %struct.timeval* %14, %struct.timeval* %15)
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%59 = load i32, i32* %58
%60 = mul nsw i32 %59, 1000
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%62 = load i32, i32* %61
%63 = sdiv i32 %62, 1000
%64 = add nsw i32 %60, %63
store i32 %64, i32* %16
%65 = load i32, i32* %16
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %65)
%67 = load i8*, i8** %12
%68 = call i32 @free(i8* %67)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [39 x i8] c"Severs: %d slicesz: %ld MB, Count: %d\0A\00"
@.str.1 = [5 x i8] c"%ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atoi(i8* %7)
%9 = sext i32 %8 to i64
%10 = udiv i64 34359738368, %9
%11 = udiv i64 1073741824, %9
%12 = icmp ult i64 %11, 268435456
%13 = select i1 %12, i64 %11, i64 268435456
%14 = udiv i64 %10, %13
%15 = trunc i64 %14 to i32
%16 = lshr i64 %13, 20
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %8, i64 %16, i32 %15)
%18 = tail call i8* @malloc(i64 %13)
%19 = bitcast %struct.timeval* %3 to i8*
%20 = bitcast %struct.timeval* %4 to i8*
%21 = bitcast %struct.timeval* %5 to i8*
%22 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%23 = icmp sgt i32 %15, 0
br i1 %23, label %24, label %29
24: ; preds = %2, %24
%25 = phi i32 [ %27, %24 ], [ 0, %2 ]
%26 = tail call i32 @dowork(i8* %18, i64 %13)
%27 = add nuw nsw i32 %25, 1
%28 = icmp eq i32 %27, %15
br i1 %28, label %29, label %24
29: ; preds = %24, %2
%30 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%31 = call i32 @timediff(%struct.timeval* %3, %struct.timeval* %4, %struct.timeval* %5)
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%33 = load i32, i32* %32
%34 = mul nsw i32 %33, 1000
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%36 = load i32, i32* %35
%37 = sdiv i32 %36, 1000
%38 = add nsw i32 %37, %34
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = call i32 @free(i8* %18)
ret i32 0
}