#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 dummy;
};

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ compare(struct timeval *, struct timeval *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct timeval *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ save(struct timeval *, struct timeval *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ x_waste_a_second();
int main(int argc, char *argv[]) {
  struct timeval tp;
  struct timeval old_tp;
  int rval;
  int i;
  int num_tests = 200;
  memset(&old_tp, 0, sizeof(struct timeval));
  if (argc > 1) {
    num_tests = atoi(argv[1]);
  }
  for (i = 0; i < num_tests; i++) {
    rval = gettimeofday(&tp, NULL);
    if (rval < 0) {
      perror("gettimeofday");
      return 1;
    }
    if (!compare(&tp, &old_tp)) {
      fprintf(stderr, "Test failed on attempt %d\n", i + 1);
      exit(1);
    }
    save(&tp, &old_tp);
    x_waste_a_second();
  }
  printf("SUCCESS\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xc8,-0x2c(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
cmpl   $0x1,-0x8(%rbp)
jle    11e7 <main+0x47>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1289 <main+0xe9>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    122c <main+0x8c>
lea    0xde5(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129c <main+0xfc>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10c0 <compare@plt>
cmp    $0x0,%eax
jne    1267 <main+0xc7>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
add    $0x1,%edx
lea    0xdb5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <save@plt>
mov    $0x0,%al
call   1080 <x_waste_a_second@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ee <main+0x4e>
lea    0xd98(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    $0xc8,%r14d
cmp    $0x2,%ebp
jl     11db <main+0x3b>
mov    0x8(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    1232 <main+0x92>
neg    %r14d
mov    $0x1,%ebx
lea    0x8(%rsp),%r15
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
test   %eax,%eax
js     124d <main+0xad>
mov    %r15,%rdi
mov    %r12,%rsi
call   10c0 <compare@plt>
test   %eax,%eax
je     1260 <main+0xc0>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <save@plt>
xor    %eax,%eax
call   1080 <x_waste_a_second@plt>
lea    (%r14,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    11f0 <main+0x50>
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdac(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    1240 <main+0xa0>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
mov    %ebx,%edx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [13 x i8] c"gettimeofday\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Test failed on attempt %d\0A\00"
@.str.2 = [9 x i8] c"SUCCESS\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 i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 200, i32* %10
%11 = call i32 @memset(%struct.timeval* %7, i32 0, i32 4)
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %10
br label %19
19: ; preds = %14, %2
store i32 0, i32* %9
br label %20
20: ; preds = %42, %19
%21 = load i32, i32* %9
%22 = load i32, i32* %10
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %45
24: ; preds = %20
%25 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %47
30: ; preds = %24
%31 = call i32 @compare(%struct.timeval* %6, %struct.timeval* %7)
%32 = icmp ne i32 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = call i32 @save(%struct.timeval* %6, %struct.timeval* %7)
%41 = call i32 (...) @x_waste_a_second()
br label %42
42: ; preds = %39
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
br label %20
45: ; preds = %20
%46 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %47
47: ; preds = %45, %28
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [13 x i8] c"gettimeofday\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Test failed on attempt %d\0A\00"
@str = [8 x i8] c"SUCCESS\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = call i32 @memset(%struct.timeval* %4, i32 0, i32 4)
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @atoi(i8* %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %35
14: ; preds = %2, %9
%15 = phi i32 [ %12, %9 ], [ 200, %2 ]
br label %16
16: ; preds = %14, %30
%17 = phi i32 [ %33, %30 ], [ 0, %14 ]
%18 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %37
22: ; preds = %16
%23 = call i32 @compare(%struct.timeval* %3, %struct.timeval* %4)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = add nuw nsw i32 %17, 1
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = call i32 @save(%struct.timeval* %3, %struct.timeval* %4)
%32 = call i32 (...) @x_waste_a_second()
%33 = add nuw nsw i32 %17, 1
%34 = icmp eq i32 %33, %15
br i1 %34, label %35, label %16
35: ; preds = %30, %9
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %37
37: ; preds = %35, %20
%38 = phi i32 [ 1, %20 ], [ 0, %35 ]
ret i32 %38
}