#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_usec;
  int tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
int network_sleep(int fd, int micros) {
  int result;
  fd_set rfds;
  struct timeval timeout;
  struct timeval *t;
  if (micros != -1) {
    timeout.tv_usec = micros % 1000000;
    timeout.tv_sec = micros / 1000000;
    t = &timeout;
  } else {
    t = 0;
  }
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  result = select(fd + 1, &rfds, 0, 0, t);
  if (result >= 0 && FD_ISSET(fd, &rfds)) {
    return 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<network_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     1181 <network_sleep+0x41>
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1189 <network_sleep+0x49>
movq   $0x0,-0x28(%rbp)
lea    -0x14(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x28(%rbp),%r8
lea    -0x14(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1050 <select@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11e4 <network_sleep+0xa4>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11e4 <network_sleep+0xa4>
movl   $0x1,-0x4(%rbp)
jmp    11eb <network_sleep+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<network_sleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r15d
cmp    $0xffffffff,%esi
je     117f <network_sleep+0x3f>
movslq %esi,%rax
imul   $0x431bde83,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x32,%rax
add    %ecx,%eax
imul   $0xf4240,%eax,%ecx
sub    %ecx,%esi
mov    %esi,0x8(%rsp)
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%r14
jmp    1182 <network_sleep+0x42>
xor    %r14d,%r14d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%r15),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1050 <select@plt>
test   %eax,%eax
js     11cb <network_sleep+0x8b>
lea    0x4(%rsp),%rsi
mov    %r15d,%edi
call   1030 <FD_ISSET@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11cd <network_sleep+0x8d>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @network_sleep(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca %struct.timeval*
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* %5
%14 = srem i32 %13, 1000000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %5
%17 = sdiv i32 %16, 1000000
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i32 %17, i32* %18
store %struct.timeval* %8, %struct.timeval** %9
br label %20
19: ; preds = %2
store %struct.timeval* null, %struct.timeval** %9
br label %20
20: ; preds = %19, %12
%21 = call i32 @FD_ZERO(i32* %7)
%22 = load i32, i32* %4
%23 = call i32 @FD_SET(i32 %22, i32* %7)
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
%26 = load %struct.timeval*, %struct.timeval** %9
%27 = call i32 @select(i32 %25, i32* %7, i32 0, i32 0, %struct.timeval* %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %20
%31 = load i32, i32* %4
%32 = call i64 @FD_ISSET(i32 %31, i32* %7)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 1, i32* %3
br label %36
35: ; preds = %30, %20
store i32 0, i32* %3
br label %36
36: ; preds = %35, %34
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @network_sleep(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = icmp eq i32 %1, -1
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = srem i32 %1, 1000000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %9, i32* %10
%11 = sdiv i32 %1, 1000000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %11, i32* %12
br label %13
13: ; preds = %2, %8
%14 = phi %struct.timeval* [ %4, %8 ], [ null, %2 ]
%15 = call i32 @FD_ZERO(i32* %3)
%16 = call i32 @FD_SET(i32 %0, i32* %3)
%17 = add nsw i32 %0, 1
%18 = call i32 @select(i32 %17, i32* %3, i32 0, i32 0, %struct.timeval* %14)
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = call i64 @FD_ISSET(i32 %0, i32* %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20, %13
br label %24
24: ; preds = %20, %23
%25 = phi i32 [ 0, %23 ], [ 1, %20 ]
ret i32 %25
}