#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct cmd_info {
  int dummy;
};

/* Variables and functions */
int atoi(char const *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, unsigned long long);
int busy_loop(int argc, const char *argv[], const struct cmd_info *info) {
  unsigned long long count = 0;
  struct timeval t0;
  struct timeval t1;
  int reps;
  int printed = 0;
  if (argc == 2) {
    reps = atoi(argv[1]);
  } else {
    reps = -1;
  }
  gettimeofday(&t0, NULL);
  while (printed != reps) {
    count++;
    if ((count % 16384) == 0) {
      unsigned int elapsed;
      gettimeofday(&t1, NULL);
      elapsed = (t1.tv_sec - t0.tv_sec) * 1000000;
      elapsed += t1.tv_usec - t0.tv_usec;
      if (elapsed >= 1000000) {
        printf("%llu\n", count);
        gettimeofday(&t0, NULL);
        count = 0;
        if (reps != -1) {
          printed++;
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<busy_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x2,-0x4(%rbp)
jne    1171 <busy_loop+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x44(%rbp)
jmp    1178 <busy_loop+0x48>
movl   $0xffffffff,-0x44(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
je     1232 <busy_loop+0x102>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
and    $0x3fff,%rax
cmp    $0x0,%rax
jne    122d <busy_loop+0xfd>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x40(%rbp),%eax
sub    -0x30(%rbp),%eax
imul   $0xf4240,%eax,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rcx
sub    -0x28(%rbp),%rcx
mov    -0x4c(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x4c(%rbp)
cmpl   $0xf4240,-0x4c(%rbp)
jb     1228 <busy_loop+0xf8>
mov    -0x20(%rbp),%rsi
lea    0xe0a(%rip),%rdi
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movq   $0x0,-0x20(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
je     1223 <busy_loop+0xf3>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1228 <busy_loop+0xf8>
jmp    122d <busy_loop+0xfd>
jmp    1185 <busy_loop+0x55>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<busy_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jne    116f <busy_loop+0x3f>
mov    0x8(%rsi),%rdi
call   1050 <atoi@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
test   %ebp,%ebp
jne    1180 <busy_loop+0x50>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    $0xffffffff,%ebp
xor    %r13d,%r13d
cmp    $0xffffffff,%ebp
setne  %r13b
xor    %ebx,%ebx
lea    0x18(%rsp),%r14
lea    0x8(%rsp),%r12
xor    %r15d,%r15d
jmp    11a5 <busy_loop+0x75>
nopl   0x0(%rax,%rax,1)
cmp    %ebp,%r15d
je     115e <busy_loop+0x2e>
add    $0x1,%rbx
test   $0x3fff,%ebx
jne    11a0 <busy_loop+0x70>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x18(%rsp),%eax
mov    0x20(%rsp),%ecx
sub    0x8(%rsp),%eax
imul   $0xf4240,%eax,%eax
sub    0x10(%rsp),%ecx
add    %eax,%ecx
cmp    $0xf4240,%ecx
jb     11a0 <busy_loop+0x70>
lea    0xe1e(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
add    %r13d,%r15d
xor    %ebx,%ebx
jmp    11a0 <busy_loop+0x70>

LLVM IR (-O0):
%struct.cmd_info = type { i32 }
%struct.timeval = type { i32, i64 }
@.str = [6 x i8] c"%llu\0A\00"
define i32 @busy_loop(i32 %0, i8** %1, %struct.cmd_info* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.cmd_info*
%7 = alloca i64
%8 = alloca %struct.timeval
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.cmd_info* %2, %struct.cmd_info** %6
store i64 0, i64* %7
store i32 0, i32* %11
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %10
br label %21
20: ; preds = %3
store i32 -1, i32* %10
br label %21
21: ; preds = %20, %15
%22 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
br label %23
23: ; preds = %63, %21
%24 = load i32, i32* %11
%25 = load i32, i32* %10
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %64
27: ; preds = %23
%28 = load i64, i64* %7
%29 = add i64 %28, 1
store i64 %29, i64* %7
%30 = load i64, i64* %7
%31 = urem i64 %30, 16384
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %63
33: ; preds = %27
%34 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%38 = load i32, i32* %37
%39 = sub nsw i32 %36, %38
%40 = mul nsw i32 %39, 1000000
store i32 %40, i32* %12
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%42 = load i64, i64* %41
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%44 = load i64, i64* %43
%45 = sub nsw i64 %42, %44
%46 = load i32, i32* %12
%47 = zext i32 %46 to i64
%48 = add nsw i64 %47, %45
%49 = trunc i64 %48 to i32
store i32 %49, i32* %12
%50 = load i32, i32* %12
%51 = icmp uge i32 %50, 1000000
br i1 %51, label %52, label %62
52: ; preds = %33
%53 = load i64, i64* %7
%54 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %53)
%55 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
store i64 0, i64* %7
%56 = load i32, i32* %10
%57 = icmp ne i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %52
%59 = load i32, i32* %11
%60 = add nsw i32 %59, 1
store i32 %60, i32* %11
br label %61
61: ; preds = %58, %52
br label %62
62: ; preds = %61, %33
br label %63
63: ; preds = %62, %27
br label %23
64: ; preds = %23
ret i32 0
}

LLVM IR (-O2):
%struct.cmd_info = type { i32 }
%struct.timeval = type { i32, i64 }
@.str = [6 x i8] c"%llu\0A\00"
define i32 @busy_loop(i32 %0, i8** nocapture %1, %struct.cmd_info* nocapture readnone %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = icmp eq i32 %0, 2
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %17
11: ; preds = %3
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%16 = icmp eq i32 %14, 0
br i1 %16, label %51, label %17
17: ; preds = %9, %11
%18 = phi i32 [ -1, %9 ], [ %14, %11 ]
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%23 = icmp ne i32 %18, -1
%24 = zext i1 %23 to i32
br label %25
25: ; preds = %17, %47
%26 = phi i64 [ 0, %17 ], [ %49, %47 ]
%27 = phi i32 [ 0, %17 ], [ %48, %47 ]
%28 = add i64 %26, 1
%29 = and i64 %28, 16383
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %47
31: ; preds = %25
%32 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%33 = load i32, i32* %19
%34 = load i32, i32* %20
%35 = sub nsw i32 %33, %34
%36 = mul nsw i32 %35, 1000000
%37 = load i64, i64* %21
%38 = load i64, i64* %22
%39 = sub nsw i64 %37, %38
%40 = trunc i64 %39 to i32
%41 = add i32 %36, %40
%42 = icmp ugt i32 %41, 999999
br i1 %42, label %43, label %47
43: ; preds = %31
%44 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %28)
%45 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%46 = add nsw i32 %27, %24
br label %47
47: ; preds = %43, %31, %25
%48 = phi i32 [ %27, %25 ], [ %27, %31 ], [ %46, %43 ]
%49 = phi i64 [ %28, %25 ], [ %28, %31 ], [ 0, %43 ]
%50 = icmp eq i32 %48, %18
br i1 %50, label %51, label %25
51: ; preds = %47, %11
ret i32 0
}