#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 /*<<< orphan*/ tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ tv_sub(struct timeval *, struct timeval *);
int do_sleep(int iters, int delay) {
  struct timeval tv1;
  struct timeval tv2;
  int i;
  for (i = 0; iters == -1 || i < iters; i++) {
    gettimeofday(&tv1, NULL);
    sleep(delay);
    gettimeofday(&tv2, NULL);
    tv_sub(&tv2, &tv1);
    printf("sleep(%d) took %ld.%06ld seconds\n", delay, tv2.tv_sec,
           tv2.tv_usec);
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x1,%al
cmpl   $0xffffffff,-0x4(%rbp)
mov    %al,-0x1d(%rbp)
je     1170 <do_sleep+0x30>
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1180 <do_sleep+0x40>
jmp    11d2 <do_sleep+0x92>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x8(%rbp),%edi
call   1060 <sleep@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <tv_sub@plt>
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe41(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1155 <do_sleep+0x15>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_sleep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%r13d
cmp    $0xffffffff,%edi
je     115d <do_sleep+0x1d>
test   %r13d,%r13d
jle    11c8 <do_sleep+0x88>
mov    $0x1,%eax
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0xe8d(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%ebp
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %ebx,%edi
call   1060 <sleep@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <tv_sub@plt>
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%edx
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
lea    0x1(%rbp),%eax
cmp    $0xffffffff,%r13d
je     1180 <do_sleep+0x40>
cmp    %r13d,%ebp
jl     1180 <do_sleep+0x40>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [34 x i8] c"sleep(%d) took %ld.%06ld seconds\0A\00"
define i32 @do_sleep(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %7
br label %8
8: ; preds = %29, %2
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = load i32, i32* %7
%13 = load i32, i32* %3
%14 = icmp slt i32 %12, %13
br label %15
15: ; preds = %11, %8
%16 = phi i1 [ true, %8 ], [ %14, %11 ]
br i1 %16, label %17, label %32
17: ; preds = %15
%18 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%19 = load i32, i32* %4
%20 = call i32 @sleep(i32 %19)
%21 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%22 = call i32 @tv_sub(%struct.timeval* %6, %struct.timeval* %5)
%23 = load i32, i32* %4
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %25, i32 %27)
br label %29
29: ; preds = %17
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %8
32: ; preds = %15
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [34 x i8] c"sleep(%d) took %ld.%06ld seconds\0A\00"
define i32 @do_sleep(i32 %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = icmp eq i32 %0, -1
%8 = icmp sgt i32 %0, 0
%9 = or i1 %7, %8
br i1 %9, label %10, label %25
10: ; preds = %2
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
br label %13
13: ; preds = %10, %13
%14 = phi i32 [ 0, %10 ], [ %22, %13 ]
%15 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%16 = call i32 @sleep(i32 %1)
%17 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%18 = call i32 @tv_sub(%struct.timeval* %4, %struct.timeval* %3)
%19 = load i32, i32* %11
%20 = load i32, i32* %12
%21 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %19, i32 %20)
%22 = add nuw nsw i32 %14, 1
%23 = icmp slt i32 %22, %0
%24 = select i1 %7, i1 true, i1 %23
br i1 %24, label %13, label %25
25: ; preds = %13, %2
ret i32 0
}