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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
void sdtools_nsleep(long ns) {
  if (ns > 1) {
    struct timeval tv1, tv2;
    volatile int i;
    gettimeofday(&tv1, 0);
    tv2.tv_sec = tv1.tv_sec;
    tv2.tv_usec = 0xffffffff & (tv1.tv_usec + 1 + (ns + 999) / 1000);
    if (tv2.tv_usec < tv1.tv_usec)
      tv2.tv_sec++;
    for (i = 0; i < 10000; i++) {
      gettimeofday(&tv1, 0);
      if (tv1.tv_sec > tv2.tv_sec ||
          (tv1.tv_sec == tv2.tv_sec && tv1.tv_usec >= tv2.tv_usec))
        break;
    }
  } else if (ns == 1) {
    struct timeval tv1;
    gettimeofday(&tv1, 0);
  }
}


x86 assembly (clang, -O0):
<sdtools_nsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x1,-0x8(%rbp)
jle    11f1 <sdtools_nsleep+0xe1>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
add    $0x3e7,%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
movabs $0xffffffff,%rcx
and    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    118c <sdtools_nsleep+0x7c>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    $0x2710,%eax
jge    11ec <sdtools_nsleep+0xdc>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jg     11d4 <sdtools_nsleep+0xc4>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11d9 <sdtools_nsleep+0xc9>
mov    -0x10(%rbp),%eax
cmp    -0x20(%rbp),%eax
jl     11d9 <sdtools_nsleep+0xc9>
jmp    11ec <sdtools_nsleep+0xdc>
jmp    11de <sdtools_nsleep+0xce>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1193 <sdtools_nsleep+0x83>
jmp    120c <sdtools_nsleep+0xfc>
cmpq   $0x1,-0x8(%rbp)
jne    1207 <sdtools_nsleep+0xf7>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
jmp    120c <sdtools_nsleep+0xfc>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdtools_nsleep>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
cmp    $0x2,%rdi
jl     11ab <sdtools_nsleep+0x9b>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
add    $0x3e7,%rbx
shr    $0x3,%rbx
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mov    %rbx,%rax
mul    %rcx
mov    0x18(%rsp),%eax
shr    $0x4,%rdx
lea    (%rdx,%rax,1),%ebp
add    $0x1,%ebp
xor    %ebx,%ebx
cmp    %eax,%ebp
setl   %bl
add    0x10(%rsp),%rbx
movl   $0x0,0xc(%rsp)
mov    0xc(%rsp),%eax
cmp    $0x270f,%eax
jg     11bd <sdtools_nsleep+0xad>
lea    0x10(%rsp),%r14
jmp    1190 <sdtools_nsleep+0x80>
addl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%eax
cmp    $0x2710,%eax
jge    11bd <sdtools_nsleep+0xad>
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    %rbx,0x10(%rsp)
jg     11bd <sdtools_nsleep+0xad>
jne    1180 <sdtools_nsleep+0x70>
cmp    %ebp,0x18(%rsp)
jl     1180 <sdtools_nsleep+0x70>
jmp    11bd <sdtools_nsleep+0xad>
cmp    $0x1,%rbx
jne    11bd <sdtools_nsleep+0xad>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
define void @sdtools_nsleep(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca %struct.timeval
store i64 %0, i64* %2
%7 = load i64, i64* %2
%8 = icmp sgt i64 %7, 1
br i1 %8, label %9, label %63
9: ; preds = %1
%10 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%12 = load i64, i64* %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i64 %12, i64* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = load i64, i64* %2
%19 = add nsw i64 %18, 999
%20 = sdiv i64 %19, 1000
%21 = add nsw i64 %17, %20
%22 = and i64 4294967295, %21
%23 = trunc i64 %22 to i32
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%28 = load i32, i32* %27
%29 = icmp slt i32 %26, %28
br i1 %29, label %30, label %34
30: ; preds = %9
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%32 = load i64, i64* %31
%33 = add nsw i64 %32, 1
store i64 %33, i64* %31
br label %34
34: ; preds = %30, %9
store volatile i32 0, i32* %5
br label %35
35: ; preds = %59, %34
%36 = load volatile i32, i32* %5
%37 = icmp slt i32 %36, 10000
br i1 %37, label %38, label %62
38: ; preds = %35
%39 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%41 = load i64, i64* %40
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%43 = load i64, i64* %42
%44 = icmp sgt i64 %41, %43
br i1 %44, label %57, label %45
45: ; preds = %38
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%47 = load i64, i64* %46
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%49 = load i64, i64* %48
%50 = icmp eq i64 %47, %49
br i1 %50, label %51, label %58
51: ; preds = %45
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%53 = load i32, i32* %52
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%55 = load i32, i32* %54
%56 = icmp sge i32 %53, %55
br i1 %56, label %57, label %58
57: ; preds = %51, %38
br label %62
58: ; preds = %51, %45
br label %59
59: ; preds = %58
%60 = load volatile i32, i32* %5
%61 = add nsw i32 %60, 1
store volatile i32 %61, i32* %5
br label %35
62: ; preds = %57, %35
br label %69
63: ; preds = %1
%64 = load i64, i64* %2
%65 = icmp eq i64 %64, 1
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = call i32 @gettimeofday(%struct.timeval* %6, i32 0)
br label %68
68: ; preds = %66, %63
br label %69
69: ; preds = %68, %62
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
define void @sdtools_nsleep(i64 %0) {
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = icmp sgt i64 %0, 1
br i1 %5, label %6, label %39
6: ; preds = %1
%7 = bitcast %struct.timeval* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = add nuw nsw i64 %0, 999
%15 = udiv i64 %14, 1000
%16 = trunc i64 %15 to i32
%17 = add i32 %16, 1
%18 = add i32 %17, %13
%19 = icmp slt i32 %18, %13
%20 = zext i1 %19 to i64
%21 = add nsw i64 %11, %20
store volatile i32 0, i32* %3
%22 = load volatile i32, i32* %3
%23 = icmp slt i32 %22, 10000
br i1 %23, label %24, label %38
24: ; preds = %6, %33
%25 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%26 = load i64, i64* %10
%27 = icmp sgt i64 %26, %21
br i1 %27, label %38, label %28
28: ; preds = %24
%29 = icmp ne i64 %26, %21
%30 = load i32, i32* %12
%31 = icmp slt i32 %30, %18
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load volatile i32, i32* %3
%35 = add nsw i32 %34, 1
store volatile i32 %35, i32* %3
%36 = load volatile i32, i32* %3
%37 = icmp slt i32 %36, 10000
br i1 %37, label %24, label %38
38: ; preds = %33, %24, %28, %6
br label %44
39: ; preds = %1
%40 = icmp eq i64 %0, 1
br i1 %40, label %41, label %44
41: ; preds = %39
%42 = bitcast %struct.timeval* %4 to i8*
%43 = call i32 @gettimeofday(%struct.timeval* %4, i32 0)
br label %44
44: ; preds = %39, %41, %38
ret void
}