#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_sec;
  int tv_nsec;
};

/* Variables and functions */
scalar_t__ irq_inside_int();
int /*<<< orphan*/ thd_sleep(int);
int thrd_sleep(const struct timespec *duration, struct timespec *remaining) {
  int ms;
  if (irq_inside_int()) {
    if (remaining)
      *remaining = *duration;
    return -1;
  }
  ms = duration->tv_sec * 1000 + duration->tv_nsec / 1000000;
  if (duration->tv_nsec % 1000000)
    ++ms;
  if (ms < 0) {
    if (remaining)
      *remaining = *duration;
    return -1;
  }
  thd_sleep(ms);
  if (remaining) {
    remaining->tv_sec = 0;
    remaining->tv_nsec = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<thrd_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1040 <irq_inside_int@plt>
cmp    $0x0,%rax
je     1166 <thrd_sleep+0x46>
cmpq   $0x0,-0x18(%rbp)
je     115a <thrd_sleep+0x3a>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <thrd_sleep+0xeb>
mov    -0x10(%rbp),%rax
imul   $0x3e8,(%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11ad <thrd_sleep+0x8d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11dc <thrd_sleep+0xbc>
cmpq   $0x0,-0x18(%rbp)
je     11d0 <thrd_sleep+0xb0>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <thrd_sleep+0xeb>
mov    -0x1c(%rbp),%edi
call   1030 <thd_sleep@plt>
cmpq   $0x0,-0x18(%rbp)
je     1204 <thrd_sleep+0xe4>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thrd_sleep>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <irq_inside_int@plt>
test   %rax,%rax
je     1148 <thrd_sleep+0x28>
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     1199 <thrd_sleep+0x79>
mov    (%r14),%rcx
mov    %rcx,(%rbx)
jmp    1199 <thrd_sleep+0x79>
imul   $0x3e8,(%r14),%eax
movslq 0x4(%r14),%rcx
imul   $0x431bde83,%rcx,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x32,%rdx
add    %esi,%edx
add    %eax,%edx
imul   $0x68c26139,%ecx,%eax
add    $0x218c0,%eax
ror    $0x6,%eax
xor    %edi,%edi
cmp    $0x10c7,%eax
setae  %dil
add    %edx,%edi
js     1136 <thrd_sleep+0x16>
call   1030 <thd_sleep@plt>
xor    %eax,%eax
test   %rbx,%rbx
je     1199 <thrd_sleep+0x79>
movq   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define i32 @thrd_sleep(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca %struct.timespec*
%5 = alloca %struct.timespec*
%6 = alloca i32
store %struct.timespec* %0, %struct.timespec** %4
store %struct.timespec* %1, %struct.timespec** %5
%7 = call i64 (...) @irq_inside_int()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %2
%10 = load %struct.timespec*, %struct.timespec** %5
%11 = icmp ne %struct.timespec* %10, null
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load %struct.timespec*, %struct.timespec** %5
%14 = load %struct.timespec*, %struct.timespec** %4
%15 = bitcast %struct.timespec* %13 to i8*
%16 = bitcast %struct.timespec* %14 to i8*
br label %17
17: ; preds = %12, %9
store i32 -1, i32* %3
br label %59
18: ; preds = %2
%19 = load %struct.timespec*, %struct.timespec** %4
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, 1000
%23 = load %struct.timespec*, %struct.timespec** %4
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = sdiv i32 %25, 1000000
%27 = add nsw i32 %22, %26
store i32 %27, i32* %6
%28 = load %struct.timespec*, %struct.timespec** %4
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = srem i32 %30, 1000000
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %18
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %36
36: ; preds = %33, %18
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = load %struct.timespec*, %struct.timespec** %5
%41 = icmp ne %struct.timespec* %40, null
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load %struct.timespec*, %struct.timespec** %5
%44 = load %struct.timespec*, %struct.timespec** %4
%45 = bitcast %struct.timespec* %43 to i8*
%46 = bitcast %struct.timespec* %44 to i8*
br label %47
47: ; preds = %42, %39
store i32 -1, i32* %3
br label %59
48: ; preds = %36
%49 = load i32, i32* %6
%50 = call i32 @thd_sleep(i32 %49)
%51 = load %struct.timespec*, %struct.timespec** %5
%52 = icmp ne %struct.timespec* %51, null
br i1 %52, label %53, label %58
53: ; preds = %48
%54 = load %struct.timespec*, %struct.timespec** %5
%55 = getelementptr inbounds %struct.timespec, %struct.timespec* %54, i32 0, i32 0
store i32 0, i32* %55
%56 = load %struct.timespec*, %struct.timespec** %5
%57 = getelementptr inbounds %struct.timespec, %struct.timespec* %56, i32 0, i32 1
store i32 0, i32* %57
br label %58
58: ; preds = %53, %48
store i32 0, i32* %3
br label %59
59: ; preds = %58, %47, %17
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define i32 @thrd_sleep(%struct.timespec* nocapture %0, %struct.timespec* writeonly %1) {
%3 = tail call i64 (...) @irq_inside_int()
%4 = icmp eq i64 %3, 0
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = icmp eq %struct.timespec* %1, null
br i1 %6, label %36, label %7
7: ; preds = %5
%8 = bitcast %struct.timespec* %0 to i64*
%9 = bitcast %struct.timespec* %1 to i64*
%10 = load i64, i64* %8
store i64 %10, i64* %9
br label %36
11: ; preds = %2
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%13 = load i32, i32* %12
%14 = mul nsw i32 %13, 1000
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%16 = load i32, i32* %15
%17 = sdiv i32 %16, 1000000
%18 = add nsw i32 %17, %14
%19 = srem i32 %16, 1000000
%20 = icmp ne i32 %19, 0
%21 = zext i1 %20 to i32
%22 = add nsw i32 %18, %21
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %11
%25 = icmp eq %struct.timespec* %1, null
br i1 %25, label %36, label %26
26: ; preds = %24
%27 = bitcast %struct.timespec* %0 to i64*
%28 = bitcast %struct.timespec* %1 to i64*
%29 = load i64, i64* %27
store i64 %29, i64* %28
br label %36
30: ; preds = %11
%31 = tail call i32 @thd_sleep(i32 %22)
%32 = icmp eq %struct.timespec* %1, null
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 0, i32* %34
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 0, i32* %35
br label %36
36: ; preds = %30, %33, %24, %26, %5, %7
%37 = phi i32 [ -1, %7 ], [ -1, %5 ], [ -1, %26 ], [ -1, %24 ], [ 0, %33 ], [ 0, %30 ]
ret i32 %37
}