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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ TIMER_ABSTIME;
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
void sleep_until(struct timespec *ts, int delay) {
  ts->tv_nsec += delay;
  if (ts->tv_nsec >= 1000 * 1000 * 1000) {
    ts->tv_nsec -= 1000 * 1000 * 1000;
    ts->tv_sec++;
  }
  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, ts, NULL);
}


x86 assembly (clang, -O0):
<sleep_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x3b9aca00,(%rax)
jl     1155 <sleep_until+0x45>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    $0x3b9aca00,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x4(%rax)
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <clock_nanosleep@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_until>:
mov    %rdi,%rdx
add    (%rdi),%esi
mov    %esi,(%rdi)
cmp    $0x3b9aca00,%esi
jl     112b <sleep_until+0x1b>
add    $0xc4653600,%esi
mov    %esi,(%rdx)
addl   $0x1,0x4(%rdx)
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
mov    0x2ead(%rip),%rax
mov    (%rax),%esi
xor    %ecx,%ecx
jmp    1030 <clock_nanosleep@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
define void @sleep_until(%struct.timespec* %0, i32 %1) {
%3 = alloca %struct.timespec*
%4 = alloca i32
store %struct.timespec* %0, %struct.timespec** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = load %struct.timespec*, %struct.timespec** %3
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%8 = load i32, i32* %7
%9 = add nsw i32 %8, %5
store i32 %9, i32* %7
%10 = load %struct.timespec*, %struct.timespec** %3
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = icmp sge i32 %12, 1000000000
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = load %struct.timespec*, %struct.timespec** %3
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = sub nsw i32 %17, 1000000000
store i32 %18, i32* %16
%19 = load %struct.timespec*, %struct.timespec** %3
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
store i32 %22, i32* %20
br label %23
23: ; preds = %14, %2
%24 = load i32, i32* @CLOCK_MONOTONIC
%25 = load i32, i32* @TIMER_ABSTIME
%26 = load %struct.timespec*, %struct.timespec** %3
%27 = call i32 @clock_nanosleep(i32 %24, i32 %25, %struct.timespec* %26, i32* null)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
define void @sleep_until(%struct.timespec* %0, i32 %1) {
%3 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = add nsw i32 %4, %1
store i32 %5, i32* %3
%6 = icmp sgt i32 %5, 999999999
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = add nsw i32 %5, -1000000000
store i32 %8, i32* %3
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%10 = load i32, i32* %9
%11 = add nsw i32 %10, 1
store i32 %11, i32* %9
br label %12
12: ; preds = %7, %2
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = load i32, i32* @TIMER_ABSTIME
%15 = tail call i32 @clock_nanosleep(i32 %13, i32 %14, %struct.timespec* %0, i32* null)
ret void
}