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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sched_yield();
int busy_wait(int u_seconds) {
  struct timeval start, end;
  gettimeofday(&start, NULL);
  long int start_usec = start.tv_usec;
  start_usec += u_seconds;
  if (start_usec > 999999999) {
    start.tv_sec++;
    start_usec -= 999999999;
    start.tv_usec = start_usec;
  }
  while (1) {
    gettimeofday(&end, NULL);
    if (start.tv_sec < end.tv_sec)
      break;
    else if (start_usec < end.tv_usec)
      break;
    sched_yield();
  }
  return 0;
}


x86 assembly (clang, -O0):
<busy_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movslq -0x4(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x3b9ac9ff,-0x30(%rbp)
3b
jle    117c <busy_wait+0x5c>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x30(%rbp),%rax
sub    $0x3b9ac9ff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1181 <busy_wait+0x61>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    11a1 <busy_wait+0x81>
jmp    11c5 <busy_wait+0xa5>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    11b4 <busy_wait+0x94>
jmp    11c5 <busy_wait+0xa5>
jmp    11b9 <busy_wait+0x99>
mov    $0x0,%al
call   1040 <sched_yield@plt>
jmp    1181 <busy_wait+0x61>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<busy_wait>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x10(%rsp),%rax
movslq %ebx,%rcx
lea    (%rax,%rcx,1),%rdx
xor    %ebx,%ebx
cmp    $0x3b9aca00,%rdx
setge  %bl
lea    -0x3b9ac9ff(%rax,%rcx,1),%r15
c4
cmovl  %rdx,%r15
add    0x18(%rsp),%rbx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    0x8(%rsp),%rbx
jl     119e <busy_wait+0x7e>
cmp    (%rsp),%r15
jl     119e <busy_wait+0x7e>
mov    %rsp,%r14
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <sched_yield@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    0x8(%rsp),%rbx
jl     119e <busy_wait+0x7e>
cmp    (%rsp),%r15
jge    1180 <busy_wait+0x60>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @busy_wait(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca i64
store i32 %0, i32* %2
%6 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%8 = load i64, i64* %7
store i64 %8, i64* %5
%9 = load i32, i32* %2
%10 = sext i32 %9 to i64
%11 = load i64, i64* %5
%12 = add nsw i64 %11, %10
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = icmp sgt i64 %13, 999999999
br i1 %14, label %15, label %23
15: ; preds = %1
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%17 = load i64, i64* %16
%18 = add nsw i64 %17, 1
store i64 %18, i64* %16
%19 = load i64, i64* %5
%20 = sub nsw i64 %19, 999999999
store i64 %20, i64* %5
%21 = load i64, i64* %5
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i64 %21, i64* %22
br label %23
23: ; preds = %15, %1
br label %24
24: ; preds = %23, %39
%25 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%27 = load i64, i64* %26
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%29 = load i64, i64* %28
%30 = icmp slt i64 %27, %29
br i1 %30, label %31, label %32
31: ; preds = %24
br label %41
32: ; preds = %24
%33 = load i64, i64* %5
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%35 = load i64, i64* %34
%36 = icmp slt i64 %33, %35
br i1 %36, label %37, label %38
37: ; preds = %32
br label %41
38: ; preds = %32
br label %39
39: ; preds = %38
%40 = call i32 (...) @sched_yield()
br label %24
41: ; preds = %37, %31
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @busy_wait(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = sext i32 %0 to i64
%10 = add nsw i64 %8, %9
%11 = icmp sgt i64 %10, 999999999
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%13 = load i64, i64* %12
%14 = add nsw i64 %10, -999999999
%15 = zext i1 %11 to i64
%16 = add nsw i64 %13, %15
%17 = select i1 %11, i64 %14, i64 %10
%18 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%20 = load i64, i64* %19
%21 = icmp slt i64 %16, %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp slt i64 %17, %23
%25 = select i1 %21, i1 true, i1 %24
br i1 %25, label %34, label %26
26: ; preds = %1, %26
%27 = tail call i32 (...) @sched_yield()
%28 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%29 = load i64, i64* %19
%30 = icmp slt i64 %16, %29
%31 = load i64, i64* %22
%32 = icmp slt i64 %17, %31
%33 = select i1 %30, i1 true, i1 %32
br i1 %33, label %34, label %26
34: ; preds = %26, %1
ret i32 0
}