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

/* Variables and functions */
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ time(unsigned int *);
unsigned sleep(unsigned nsec) {
  int n;
  unsigned slept;
  time_t start, end;
  struct timeval tv;
  tv.tv_sec = nsec;
  tv.tv_usec = 0;
  time(&start);
  n = select(0, NULL, NULL, NULL, &tv);
  if (n == 0)
    return (0);
  time(&end);
  slept = end - start;
  if (slept >= nsec)
    return (0);
  return (nsec - slept);
}


x86 assembly (clang, -O0):
<sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <time@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1170 <sleep+0x50>
movl   $0x0,-0x4(%rbp)
jmp    11a3 <sleep+0x83>
lea    -0x18(%rbp),%rdi
call   1030 <time@plt>
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jb     119a <sleep+0x7a>
movl   $0x0,-0x4(%rbp)
jmp    11a3 <sleep+0x83>
mov    -0x8(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %edi,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%rdi
call   1030 <time@plt>
xor    %r14d,%r14d
lea    0x8(%rsp),%r8
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
test   %eax,%eax
je     1172 <sleep+0x52>
mov    %rsp,%rdi
call   1030 <time@plt>
mov    (%rsp),%eax
sub    0x4(%rsp),%eax
xor    %ecx,%ecx
sub    %eax,%ebx
cmovb  %ecx,%ebx
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %11
%12 = call i32 @time(i32* %6)
%13 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %8)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 0, i32* %2
br label %30
17: ; preds = %1
%18 = call i32 @time(i32* %7)
%19 = load i32, i32* %7
%20 = load i32, i32* %6
%21 = sub i32 %19, %20
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = load i32, i32* %3
%24 = icmp uge i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 0, i32* %2
br label %30
26: ; preds = %17
%27 = load i32, i32* %3
%28 = load i32, i32* %5
%29 = sub i32 %27, %28
store i32 %29, i32* %2
br label %30
30: ; preds = %26, %25, %16
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %0, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %9
%10 = call i32 @time(i32* %2)
%11 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %1
%14 = call i32 @time(i32* %3)
%15 = load i32, i32* %3
%16 = load i32, i32* %2
%17 = sub i32 %15, %16
%18 = call i32 @llvm.usub.sat.i32(i32 %0, i32 %17)
br label %19
19: ; preds = %13, %1
%20 = phi i32 [ 0, %1 ], [ %18, %13 ]
ret i32 %20
}