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

/* Variables and functions */
scalar_t__ athame_select(int, int, int /*<<< orphan*/, int, int);
scalar_t__ nanosleep(struct timespec *, struct timespec *);
int athame_sleep(int msec, int char_break, int instream) {
  if (char_break) {
    return athame_select(instream, -1, 0, msec, 1) > 0;
  } else {
    struct timespec timeout, timeout2;
    timeout.tv_sec = 0;
    timeout.tv_nsec = msec * 1000000;
    while (nanosleep(&timeout, &timeout2) < 0) {
      timeout = timeout2;
    }
    return 0;
  }
}


x86 assembly (clang, -O0):
<athame_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1167 <athame_sleep+0x47>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%ecx
mov    $0xffffffff,%esi
xor    %edx,%edx
mov    $0x1,%r8d
call   1040 <athame_select@plt>
cmp    $0x0,%rax
setg   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    11ac <athame_sleep+0x8c>
movq   $0x0,-0x18(%rbp)
imul   $0xf4240,-0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
jge    11a5 <athame_sleep+0x85>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1179 <athame_sleep+0x59>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<athame_sleep>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ecx
test   %esi,%esi
je     114e <athame_sleep+0x2e>
mov    %edx,%edi
mov    $0xffffffff,%esi
xor    %edx,%edx
mov    $0x1,%r8d
call   1040 <athame_select@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setg   %al
jmp    119b <athame_sleep+0x7b>
movq   $0x0,0x8(%rsp)
imul   $0xf4240,%ecx,%eax
mov    %eax,(%rsp)
mov    %rsp,%rdi
lea    0x18(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
jns    1199 <athame_sleep+0x79>
mov    %rsp,%r14
lea    0x18(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
js     1180 <athame_sleep+0x60>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
define i32 @athame_sleep(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
%9 = alloca %struct.timespec
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = load i32, i32* %7
%14 = load i32, i32* %5
%15 = call i64 @athame_select(i32 %13, i32 -1, i32 0, i32 %14, i32 1)
%16 = icmp sgt i64 %15, 0
%17 = zext i1 %16 to i32
store i32 %17, i32* %4
br label %30
18: ; preds = %3
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i64 0, i64* %19
%20 = load i32, i32* %5
%21 = mul nsw i32 %20, 1000000
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 %21, i32* %22
br label %23
23: ; preds = %26, %18
%24 = call i64 @nanosleep(%struct.timespec* %8, %struct.timespec* %9)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = bitcast %struct.timespec* %8 to i8*
%28 = bitcast %struct.timespec* %9 to i8*
br label %23
29: ; preds = %23
store i32 0, i32* %4
br label %30
30: ; preds = %29, %12
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
define i32 @athame_sleep(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = icmp eq i32 %1, 0
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = tail call i64 @athame_select(i32 %2, i32 -1, i32 0, i32 %0, i32 1)
%9 = icmp sgt i64 %8, 0
%10 = zext i1 %9 to i32
br label %23
11: ; preds = %3
%12 = bitcast %struct.timespec* %4 to i8*
%13 = bitcast %struct.timespec* %5 to i8*
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
store i64 0, i64* %14
%15 = mul nsw i32 %0, 1000000
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
store i32 %15, i32* %16
%17 = call i64 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %11, %19
%20 = call i64 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
%21 = icmp slt i64 %20, 0
br i1 %21, label %19, label %22
22: ; preds = %19, %11
br label %23
23: ; preds = %22, %7
%24 = phi i32 [ %10, %7 ], [ 0, %22 ]
ret i32 %24
}