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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int scgetc(int fd, char *cp, int ms) {
  fd_set fdset;
  struct timeval tv, *tvp;
  FD_ZERO(&fdset);
  FD_SET(fd, &fdset);
  if (ms == -1)
    tvp = NULL;
  else {
    tv.tv_sec = (ms) / 1000;
    tv.tv_usec = (ms % 1000) * 1000;
    tvp = &tv;
  }
  if (select(fd + 1, &fdset, NULL, NULL, tvp) != 1)
    return 3;
  if (read(fd, cp, 1) != 1)
    return 3;
  return 0;
}


x86 assembly (clang, -O0):
<scgetc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <FD_SET@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    117e <scgetc+0x3e>
movq   $0x0,-0x28(%rbp)
jmp    11a8 <scgetc+0x68>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x28(%rbp),%r8
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x1,%eax
je     11d7 <scgetc+0x97>
movl   $0x3,-0x4(%rbp)
jmp    1204 <scgetc+0xc4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     11fd <scgetc+0xbd>
movl   $0x3,-0x4(%rbp)
jmp    1204 <scgetc+0xc4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scgetc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
cmp    $0xffffffff,%ebp
je     11a3 <scgetc+0x63>
movslq %ebp,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,0x10(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%ebp
imul   $0x3e8,%ebp,%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%r8
jmp    11a6 <scgetc+0x66>
xor    %r8d,%r8d
lea    0x1(%r15),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    $0x1,%ecx
jne    11df <scgetc+0x9f>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
setne  %cl
lea    (%rcx,%rcx,2),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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