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

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ abort();
scalar_t__ chain(scalar_t__);
int getopt(int, char **, char *);
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned long);
int /*<<< orphan*/ strtoull(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  struct timeval stime, etime;
  size_t msec = 0;
  uint64_t bound = 40;
  uint64_t start = 100;
  uint64_t i;
  int count = 0;
  int opt;
  while ((opt = getopt(argc, argv, "b:s:")) != -1) {
    switch (opt) {
    case 'b':
      bound = (uint64_t)strtoull(optarg, NULL, 10);
      break;
    case 's':
      start = (uint64_t)strtoull(optarg, NULL, 10);
      break;
    default:
      abort();
    }
  }
  if (gettimeofday(&stime, NULL) == -1) {
    perror("gettimeofday");
    return EXIT_FAILURE;
  }
  for (i = 1; i <= start; i++) {
    if (chain(i) > bound)
      count++;
  }
  if (gettimeofday(&etime, NULL) == -1) {
    perror("gettimeofday");
    return EXIT_FAILURE;
  }
  msec = etime.tv_sec * 1000000;
  msec += etime.tv_usec;
  msec -= (stime.tv_sec * 1000000);
  msec -= stime.tv_usec;
  printf("Number of chains: %d\n", count);
  printf("Run time: %lu us\n", (long unsigned)msec);
}


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)
movq   $0x0,-0x38(%rbp)
movq   $0x28,-0x40(%rbp)
movq   $0x64,-0x48(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe4d(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x58(%rbp)
cmp    $0xffffffff,%eax
je     1239 <main+0xc9>
mov    -0x58(%rbp),%eax
mov    %eax,-0x5c(%rbp)
sub    $0x62,%eax
je     11e9 <main+0x79>
jmp    11d8 <main+0x68>
mov    -0x5c(%rbp),%eax
sub    $0x73,%eax
je     120b <main+0x9b>
jmp    122d <main+0xbd>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtoull@plt>
cltq
mov    %rax,-0x40(%rbp)
jmp    1234 <main+0xc4>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtoull@plt>
cltq
mov    %rax,-0x48(%rbp)
jmp    1234 <main+0xc4>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11a5 <main+0x35>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    126c <main+0xfc>
lea    0xdaf(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1344 <main+0x1d4>
movq   $0x1,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x48(%rbp),%rax
jg     12b4 <main+0x144>
mov    -0x50(%rbp),%rdi
call   1090 <chain@plt>
cmp    -0x40(%rbp),%rax
jle    129e <main+0x12e>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12a3 <main+0x133>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1274 <main+0x104>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
jne    12e7 <main+0x177>
lea    0xd34(%rip),%rdi
call   1070 <perror@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1344 <main+0x1d4>
imul   $0xf4240,-0x30(%rbp),%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
imul   $0xf4240,-0x20(%rbp),%eax
movslq %eax,%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
movslq -0x54(%rbp),%rsi
lea    0xce3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x38(%rbp),%rsi
lea    0xce9(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x64,%r15d
mov    $0x28,%r12d
lea    0xe6a(%rip),%r14
mov    0x2e5b(%rip),%r13
jmp    11b3 <main+0x43>
nop
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtoull@plt>
movslq %eax,%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <getopt@plt>
cmp    $0x73,%eax
je     11a0 <main+0x30>
cmp    $0xffffffff,%eax
je     11e8 <main+0x78>
cmp    $0x62,%eax
jne    12b7 <main+0x147>
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtoull@plt>
movslq %eax,%r12
jmp    11b3 <main+0x43>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     129f <main+0x12f>
xor    %ebx,%ebx
test   %r15,%r15
jle    1236 <main+0xc6>
neg    %r15
mov    $0x1,%ebp
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1090 <chain@plt>
xor    %ecx,%ecx
cmp    %r12,%rax
setg   %cl
add    %ecx,%ebx
lea    (%r15,%rbp,1),%rax
add    $0x1,%rax
add    $0x1,%rbp
cmp    $0x1,%rax
jne    1210 <main+0xa0>
mov    %ebx,%ebx
xor    %r14d,%r14d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     129f <main+0x12f>
movslq 0x8(%rsp),%rax
imul   $0xf4240,%rax,%rbp
movslq 0x18(%rsp),%rax
add    0x10(%rsp),%rbp
imul   $0xf4240,%rax,%rax
add    0x20(%rsp),%rax
sub    %rax,%rbp
lea    0xd9c(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xda3(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%r14d
jmp    128d <main+0x11d>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [5 x i8] c"b:s:\00"
@optarg = global i32 0
@.str.1 = [13 x i8] c"gettimeofday\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [22 x i8] c"Number of chains: %d\0A\00"
@.str.3 = [18 x i8] c"Run time: %lu us\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %8
store i64 40, i64* %9
store i64 100, i64* %10
store i32 0, i32* %12
br label %14
14: ; preds = %31, %2
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = call i32 @getopt(i32 %15, i8** %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %13
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %32
19: ; preds = %14
%20 = load i32, i32* %13
switch i32 %20, label %29 [
i32 98, label %21
i32 115, label %25
]
21: ; preds = %19
%22 = load i32, i32* @optarg
%23 = call i32 @strtoull(i32 %22, i32* null, i32 10)
%24 = sext i32 %23 to i64
store i64 %24, i64* %9
br label %31
25: ; preds = %19
%26 = load i32, i32* @optarg
%27 = call i32 @strtoull(i32 %26, i32* null, i32 10)
%28 = sext i32 %27 to i64
store i64 %28, i64* %10
br label %31
29: ; preds = %19
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %25, %21
br label %14
32: ; preds = %14
%33 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
br label %85
38: ; preds = %32
store i64 1, i64* %11
br label %39
39: ; preds = %52, %38
%40 = load i64, i64* %11
%41 = load i64, i64* %10
%42 = icmp sle i64 %40, %41
br i1 %42, label %43, label %55
43: ; preds = %39
%44 = load i64, i64* %11
%45 = call i64 @chain(i64 %44)
%46 = load i64, i64* %9
%47 = icmp sgt i64 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* %12
%50 = add nsw i32 %49, 1
store i32 %50, i32* %12
br label %51
51: ; preds = %48, %43
br label %52
52: ; preds = %51
%53 = load i64, i64* %11
%54 = add nsw i64 %53, 1
store i64 %54, i64* %11
br label %39
55: ; preds = %39
%56 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
store i32 %60, i32* %3
br label %85
61: ; preds = %55
%62 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%63 = load i32, i32* %62
%64 = mul nsw i32 %63, 1000000
%65 = sext i32 %64 to i64
store i64 %65, i64* %8
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%67 = load i64, i64* %66
%68 = load i64, i64* %8
%69 = add i64 %68, %67
store i64 %69, i64* %8
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%71 = load i32, i32* %70
%72 = mul nsw i32 %71, 1000000
%73 = sext i32 %72 to i64
%74 = load i64, i64* %8
%75 = sub i64 %74, %73
store i64 %75, i64* %8
%76 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%77 = load i64, i64* %76
%78 = load i64, i64* %8
%79 = sub i64 %78, %77
store i64 %79, i64* %8
%80 = load i32, i32* %12
%81 = sext i32 %80 to i64
%82 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %81)
%83 = load i64, i64* %8
%84 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %83)
br label %85
85: ; preds = %61, %58, %35
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [5 x i8] c"b:s:\00"
@optarg = global i32 0
@.str.1 = [13 x i8] c"gettimeofday\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [22 x i8] c"Number of chains: %d\0A\00"
@.str.3 = [18 x i8] c"Run time: %lu us\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
br label %7
7: ; preds = %13, %2
%8 = phi i64 [ %16, %13 ], [ 40, %2 ]
%9 = phi i64 [ %11, %13 ], [ 100, %2 ]
br label %10
10: ; preds = %7, %17
%11 = phi i64 [ %20, %17 ], [ %9, %7 ]
%12 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
switch i32 %12, label %21 [
i32 -1, label %23
i32 98, label %13
i32 115, label %17
]
13: ; preds = %10
%14 = load i32, i32* @optarg
%15 = tail call i32 @strtoull(i32 %14, i32* null, i32 10)
%16 = sext i32 %15 to i64
br label %7
17: ; preds = %10
%18 = load i32, i32* @optarg
%19 = tail call i32 @strtoull(i32 %18, i32* null, i32 10)
%20 = sext i32 %19 to i64
br label %10
21: ; preds = %10
%22 = tail call i32 (...) @abort()
unreachable
23: ; preds = %10
%24 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%25 = icmp eq i32 %24, -1
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = icmp slt i64 %11, 1
br i1 %27, label %42, label %31
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
br label %67
31: ; preds = %26, %31
%32 = phi i32 [ %37, %31 ], [ 0, %26 ]
%33 = phi i64 [ %38, %31 ], [ 1, %26 ]
%34 = tail call i64 @chain(i64 %33)
%35 = icmp sgt i64 %34, %8
%36 = zext i1 %35 to i32
%37 = add nuw nsw i32 %32, %36
%38 = add nuw i64 %33, 1
%39 = icmp eq i64 %33, %11
br i1 %39, label %40, label %31
40: ; preds = %31
%41 = zext i32 %37 to i64
br label %42
42: ; preds = %40, %26
%43 = phi i64 [ 0, %26 ], [ %41, %40 ]
%44 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
br label %67
49: ; preds = %42
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%51 = load i32, i32* %50
%52 = mul nsw i32 %51, 1000000
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%55 = load i64, i64* %54
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%57 = load i32, i32* %56
%58 = mul nsw i32 %57, 1000000
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%61 = load i64, i64* %60
%62 = add i64 %55, %53
%63 = add i64 %61, %59
%64 = sub i64 %62, %63
%65 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %43)
%66 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %64)
br label %67
67: ; preds = %49, %46, %28
%68 = phi i32 [ %30, %28 ], [ %48, %46 ], [ 0, %49 ]
ret i32 %68
}