#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 {
  unsigned long tv_sec;
  unsigned long tv_nsec;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void delay_internal(unsigned long seconds, unsigned long us) {
  struct timespec time;
  time.tv_sec = seconds;
  time.tv_nsec = us * 1000UL;
  int res;
  do {
    res = nanosleep(&time, &time);
  } while (res == -1 && errno == EINTR);
  if (res)
    fprintf(stderr, "nanosleep failed: %s\n", strerror(errno));
}


x86 assembly (clang, -O0):
<delay_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
imul   $0x3e8,-0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jne    118c <delay_internal+0x5c>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1154 <delay_internal+0x24>
cmpl   $0x0,-0x24(%rbp)
je     11ce <delay_internal+0x9e>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe37(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delay_internal>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,(%rsp)
imul   $0x3e8,%rsi,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rbx
mov    0x2e85(%rip),%r14
mov    0x2e76(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
mov    (%r14),%rdi
cmp    $0xffffffff,%eax
jne    1178 <delay_internal+0x48>
cmp    (%r15),%rdi
je     1160 <delay_internal+0x30>
test   %eax,%eax
je     119b <delay_internal+0x6b>
mov    0x2e5d(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe6f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [22 x i8] c"nanosleep failed: %s\0A\00"
define void @delay_internal(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca %struct.timespec
%6 = alloca i32
store i64 %0, i64* %3
store i64 %1, i64* %4
%7 = load i64, i64* %3
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i64 %7, i64* %8
%9 = load i64, i64* %4
%10 = mul i64 %9, 1000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 %10, i64* %11
br label %12
12: ; preds = %21, %2
%13 = call i32 @nanosleep(%struct.timespec* %5, %struct.timespec* %5)
store i32 %13, i32* %6
br label %14
14: ; preds = %12
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %17, %14
%22 = phi i1 [ false, %14 ], [ %20, %17 ]
br i1 %22, label %12, label %23
23: ; preds = %21
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %26, %23
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [22 x i8] c"nanosleep failed: %s\0A\00"
define void @delay_internal(i64 %0, i64 %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i64 %0, i64* %5
%6 = mul i64 %1, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 %6, i64* %7
br label %8
8: ; preds = %8, %2
%9 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
%10 = icmp eq i32 %9, -1
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %8, label %15
15: ; preds = %8
%16 = icmp eq i32 %9, 0
br i1 %16, label %21, label %17
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = call i8* @strerror(i64 %11)
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %17, %15
ret void
}