#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 timezone {
  int dummy;
};
struct timeval {
  int tv_usec;
  int tv_sec;
};
typedef int int64_t;

/* Variables and functions */
int atof(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ gettimeofday(struct timeval *, struct timezone *);
int /*<<< orphan*/ perror(char *);
scalar_t__ settimeofday(struct timeval *, struct timezone *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "usage: %s <delta>, where delta is in ms\n", argv[0]);
    return 1;
  }
  int64_t delta = atof(argv[1]) * 1000;
  int64_t delta_us = delta % 1000000;
  int64_t delta_s = (delta - delta_us) / 1000000;
  struct timeval time;
  struct timezone tz;
  if (0 != gettimeofday(&time, &tz)) {
    perror("gettimeofday");
    return 1;
  }
  time.tv_usec += delta_us;
  time.tv_sec += delta_s;
  while (time.tv_usec <= 1000000) {
    time.tv_sec -= 1;
    time.tv_usec += 1000000;
  }
  while (1000000 <= time.tv_usec) {
    time.tv_sec += 1;
    time.tv_usec -= 1000000;
  }
  if (0 != settimeofday(&time, &tz)) {
    perror("settimeofday");
    return 2;
  }
  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)
cmpl   $0x2,-0x8(%rbp)
jge    1198 <main+0x48>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x14e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <atof@plt>
imul   $0x3e8,%eax,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <gettimeofday@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1200 <main+0xb0>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x14e>
mov    -0x18(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xf4240,-0x28(%rbp)
jg     1238 <main+0xe8>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x28(%rbp)
jmp    1212 <main+0xc2>
jmp    123d <main+0xed>
mov    $0xf4240,%eax
cmp    -0x28(%rbp),%eax
jg     1264 <main+0x114>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x28(%rbp)
jmp    123d <main+0xed>
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <settimeofday@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1297 <main+0x147>
lea    0xdb0(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    129e <main+0x14e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     117c <main+0x2c>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe93(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1278 <main+0x128>
mov    0x8(%rsi),%rdi
call   1030 <atof@plt>
mov    %eax,%ebx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1040 <gettimeofday@plt>
test   %rax,%rax
jne    127e <main+0x12e>
imul   $0x3e8,%ebx,%eax
cltq
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
movslq %ebx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    (%rsp),%eax
add    %edx,%ecx
add    0x4(%rsp),%ecx
cmp    $0xf4240,%eax
jg     1226 <main+0xd6>
cmp    $0x2,%eax
mov    $0x1,%edx
cmovge %eax,%edx
xor    %esi,%esi
test   %eax,%eax
setle  %sil
sub    %eax,%edx
sub    %esi,%edx
imul   $0x431bde83,%rdx,%rdx
shr    $0x32,%rdx
add    %esi,%edx
imul   $0xf4240,%edx,%esi
lea    (%rax,%rsi,1),%edi
not    %edx
add    %edx,%ecx
add    %esi,%eax
add    $0xf4240,%eax
cmp    $0x7ff0bdbf,%edi
ja     125a <main+0x10a>
cmp    $0x1e847f,%eax
mov    $0x1e847f,%edx
cmovl  %eax,%edx
neg    %edx
add    %eax,%edx
add    $0xf423f,%edx
imul   $0x431bde83,%rdx,%rdx
shr    $0x32,%rdx
add    %edx,%ecx
add    $0x1,%ecx
imul   $0xfff0bdc0,%edx,%edx
add    %edx,%eax
add    $0xfff0bdc0,%eax
mov    %ecx,0x4(%rsp)
mov    %eax,(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1060 <settimeofday@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1291 <main+0x141>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xda4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    1278 <main+0x128>
lea    0xd9e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
jmp    1278 <main+0x128>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
@stderr = global i32 0
@.str = [41 x i8] c"usage: %s <delta>, where delta is in ms\0A\00"
@.str.1 = [13 x i8] c"gettimeofday\00"
@.str.2 = [13 x i8] c"settimeofday\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 %struct.timeval
%10 = alloca %struct.timezone
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %73
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atof(i8* %22)
%24 = mul nsw i32 %23, 1000
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = srem i32 %25, 1000000
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = sub nsw i32 %27, %28
%30 = sdiv i32 %29, 1000000
store i32 %30, i32* %8
%31 = call i64 @gettimeofday(%struct.timeval* %9, %struct.timezone* %10)
%32 = icmp ne i64 0, %31
br i1 %32, label %33, label %35
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %73
35: ; preds = %19
%36 = load i32, i32* %7
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%38 = load i32, i32* %37
%39 = add nsw i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* %8
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%42 = load i32, i32* %41
%43 = add nsw i32 %42, %40
store i32 %43, i32* %41
br label %44
44: ; preds = %48, %35
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%46 = load i32, i32* %45
%47 = icmp sle i32 %46, 1000000
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%50 = load i32, i32* %49
%51 = sub nsw i32 %50, 1
store i32 %51, i32* %49
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%53 = load i32, i32* %52
%54 = add nsw i32 %53, 1000000
store i32 %54, i32* %52
br label %44
55: ; preds = %44
br label %56
56: ; preds = %60, %55
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%58 = load i32, i32* %57
%59 = icmp sle i32 1000000, %58
br i1 %59, label %60, label %67
60: ; preds = %56
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%62 = load i32, i32* %61
%63 = add nsw i32 %62, 1
store i32 %63, i32* %61
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%65 = load i32, i32* %64
%66 = sub nsw i32 %65, 1000000
store i32 %66, i32* %64
br label %56
67: ; preds = %56
%68 = call i64 @settimeofday(%struct.timeval* %9, %struct.timezone* %10)
%69 = icmp ne i64 0, %68
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %73
72: ; preds = %67
store i32 0, i32* %3
br label %73
73: ; preds = %72, %70, %33, %13
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
@stderr = global i32 0
@.str = [41 x i8] c"usage: %s <delta>, where delta is in ms\0A\00"
@.str.1 = [13 x i8] c"gettimeofday\00"
@.str.2 = [13 x i8] c"settimeofday\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timezone
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %66
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atof(i8* %12)
%14 = bitcast %struct.timeval* %3 to i8*
%15 = bitcast %struct.timezone* %4 to i8*
%16 = call i64 @gettimeofday(%struct.timeval* %3, %struct.timezone* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %64
20: ; preds = %10
%21 = mul nsw i32 %13, 1000
%22 = srem i32 %21, 1000000
%23 = sdiv i32 %13, 1000
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = add i32 %25, %22
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%28 = load i32, i32* %27
%29 = add i32 %28, %23
%30 = icmp slt i32 %26, 1000001
br i1 %30, label %31, label %45
31: ; preds = %20
%32 = call i32 @llvm.smax.i32(i32 %26, i32 1)
%33 = icmp slt i32 %26, 1
%34 = zext i1 %33 to i32
%35 = add nsw i32 %26, %34
%36 = sub i32 %32, %35
%37 = udiv i32 %36, 1000000
%38 = add nuw nsw i32 %37, %34
%39 = mul i32 %38, 1000000
%40 = add i32 %26, %39
%41 = xor i32 %38, -1
%42 = add i32 %29, %41
%43 = add i32 %40, 1000000
%44 = icmp ult i32 %40, 2146483648
br i1 %44, label %45, label %57
45: ; preds = %20, %31
%46 = phi i32 [ %42, %31 ], [ %29, %20 ]
%47 = phi i32 [ %43, %31 ], [ %26, %20 ]
%48 = add nuw i32 %47, 999999
%49 = call i32 @llvm.smin.i32(i32 %47, i32 1999999)
%50 = sub nuw i32 %48, %49
%51 = udiv i32 %50, 1000000
%52 = add i32 %46, %51
%53 = add i32 %52, 1
%54 = mul nsw i32 %51, -1000000
%55 = add nsw i32 %47, -1000000
%56 = add nsw i32 %54, %55
br label %57
57: ; preds = %45, %31
%58 = phi i32 [ %43, %31 ], [ %56, %45 ]
%59 = phi i32 [ %42, %31 ], [ %53, %45 ]
store i32 %59, i32* %27
store i32 %58, i32* %24
%60 = call i64 @settimeofday(%struct.timeval* %3, %struct.timezone* %4)
%61 = icmp eq i64 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %57, %62, %18
%65 = phi i32 [ 1, %18 ], [ 2, %62 ], [ 0, %57 ]
br label %66
66: ; preds = %64, %6
%67 = phi i32 [ 1, %6 ], [ %65, %64 ]
ret i32 %67
}