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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int ms_sleep(int mswait) {
  struct timeval tv;
  int retcode;
  if (mswait == 0) {
    return (0);
  }
  tv.tv_sec = 1;
  tv.tv_usec = 1000 * mswait;
  do {
    retcode = select(1, NULL, NULL, NULL, &tv);
  } while ((retcode == -1) && (errno == EINTR));
  return (retcode);
}


x86 assembly (clang, -O0):
<ms_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1131 <ms_sleep+0x21>
movl   $0x0,-0x4(%rbp)
jmp    1197 <ms_sleep+0x87>
movl   $0x1,-0x10(%rbp)
imul   $0x3e8,-0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <select@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jne    1186 <ms_sleep+0x76>
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1142 <ms_sleep+0x32>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ms_sleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     1172 <ms_sleep+0x62>
movl   $0x1,0x8(%rsp)
imul   $0x3e8,%edi,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%r15
mov    0x2ea5(%rip),%r14
mov    0x2e96(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1030 <select@plt>
cmp    $0xffffffff,%eax
jne    1174 <ms_sleep+0x64>
mov    (%rbx),%rcx
cmp    %rcx,(%r14)
je     1150 <ms_sleep+0x40>
jmp    1174 <ms_sleep+0x64>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @ms_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %27
9: ; preds = %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 1, i32* %10
%11 = load i32, i32* %3
%12 = mul nsw i32 1000, %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i32 %12, i32* %13
br label %14
14: ; preds = %23, %9
%15 = call i32 @select(i32 1, i32* null, i32* null, i32* null, %struct.timeval* %4)
store i32 %15, i32* %5
br label %16
16: ; preds = %14
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %14, label %25
25: ; preds = %23
%26 = load i32, i32* %5
store i32 %26, i32* %2
br label %27
27: ; preds = %25, %8
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @ms_sleep(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = icmp eq i32 %0, 0
br i1 %4, label %16, label %5
5: ; preds = %1
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 1, i32* %6
%7 = mul nsw i32 %0, 1000
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i32 %7, i32* %8
br label %9
9: ; preds = %9, %5
%10 = call i32 @select(i32 1, i32* null, i32* null, i32* null, %struct.timeval* %2)
%11 = icmp eq i32 %10, -1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %9, label %16
16: ; preds = %9, %1
%17 = phi i32 [ 0, %1 ], [ %10, %9 ]
ret i32 %17
}