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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int errno;
int recv(int, unsigned char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int _ADSReadByte(int rfd, unsigned char *b, struct timeval *pt, int *error) {
  fd_set FDS;
  int rc;
  FD_ZERO(&FDS);
  FD_SET(rfd, &FDS);
  rc = select(rfd + 1, &FDS, NULL, NULL, pt);
  if (rc == -1) {
    ((void)0);
    if (error)
      *error = errno;
    return (0);
  } else if (rc == 0) {
    ((void)0);
    if (error)
      *error = 0;
    return (-1);
  }
  rc = recv(rfd, b, 1, 0);
  if (rc == 0) {
    ((void)0);
    *error = 0;
    return (-2);
  } else if (rc == -1) {
    ((void)0);
    if (error)
      *error = errno;
    return (0);
  }
  if (error)
    *error = 0;
  return (1);
}


x86 assembly (clang, -O0):
<_ADSReadByte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x18(%rbp),%r8
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11b9 <_ADSReadByte+0x79>
cmpq   $0x0,-0x20(%rbp)
je     11ad <_ADSReadByte+0x6d>
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1270 <_ADSReadByte+0x130>
cmpl   $0x0,-0x28(%rbp)
jne    11e4 <_ADSReadByte+0xa4>
cmpq   $0x0,-0x20(%rbp)
je     11d8 <_ADSReadByte+0x98>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <_ADSReadByte+0x130>
jmp    11e9 <_ADSReadByte+0xa9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    121f <_ADSReadByte+0xdf>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0xfffffffe,-0x4(%rbp)
jmp    1270 <_ADSReadByte+0x130>
cmpl   $0xffffffff,-0x28(%rbp)
jne    124f <_ADSReadByte+0x10f>
cmpq   $0x0,-0x20(%rbp)
je     1243 <_ADSReadByte+0x103>
mov    0x2da5(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1270 <_ADSReadByte+0x130>
jmp    1254 <_ADSReadByte+0x114>
cmpq   $0x0,-0x20(%rbp)
je     1269 <_ADSReadByte+0x129>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_ADSReadByte>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
xor    %r12d,%r12d
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1050 <select@plt>
test   %eax,%eax
je     119e <_ADSReadByte+0x5e>
cmp    $0xffffffff,%eax
jne    11aa <_ADSReadByte+0x6a>
test   %r14,%r14
je     11cd <_ADSReadByte+0x8d>
mov    0x2e49(%rip),%rax
mov    (%rax),%r12d
xor    %eax,%eax
jmp    11e5 <_ADSReadByte+0xa5>
mov    $0xffffffff,%eax
test   %r14,%r14
jne    11e5 <_ADSReadByte+0xa5>
jmp    11e8 <_ADSReadByte+0xa8>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r12d
test   %eax,%eax
je     11d1 <_ADSReadByte+0x91>
cmp    $0xffffffff,%r12d
jne    11d8 <_ADSReadByte+0x98>
test   %r14,%r14
jne    1190 <_ADSReadByte+0x50>
xor    %eax,%eax
jmp    11e8 <_ADSReadByte+0xa8>
mov    $0xfffffffe,%eax
jmp    11e5 <_ADSReadByte+0xa5>
xor    %r12d,%r12d
mov    $0x1,%eax
test   %r14,%r14
je     11e8 <_ADSReadByte+0xa8>
mov    %r12d,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@errno = global i32 0
define i32 @_ADSReadByte(i32 %0, i8* %1, %struct.timeval* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.timeval*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store %struct.timeval* %2, %struct.timeval** %8
store i32* %3, i32** %9
%12 = call i32 @FD_ZERO(i32* %10)
%13 = load i32, i32* %6
%14 = call i32 @FD_SET(i32 %13, i32* %10)
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
%17 = load %struct.timeval*, %struct.timeval** %8
%18 = call i32 @select(i32 %16, i32* %10, i32* null, i32* null, %struct.timeval* %17)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %28
21: ; preds = %4
%22 = load i32*, i32** %9
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @errno
%26 = load i32*, i32** %9
store i32 %25, i32* %26
br label %27
27: ; preds = %24, %21
store i32 0, i32* %5
br label %63
28: ; preds = %4
%29 = load i32, i32* %11
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = load i32*, i32** %9
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32*, i32** %9
store i32 0, i32* %35
br label %36
36: ; preds = %34, %31
store i32 -1, i32* %5
br label %63
37: ; preds = %28
br label %38
38: ; preds = %37
%39 = load i32, i32* %6
%40 = load i8*, i8** %7
%41 = call i32 @recv(i32 %39, i8* %40, i32 1, i32 0)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = load i32*, i32** %9
store i32 0, i32* %45
store i32 -2, i32* %5
br label %63
46: ; preds = %38
%47 = load i32, i32* %11
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %56
49: ; preds = %46
%50 = load i32*, i32** %9
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32, i32* @errno
%54 = load i32*, i32** %9
store i32 %53, i32* %54
br label %55
55: ; preds = %52, %49
store i32 0, i32* %5
br label %63
56: ; preds = %46
br label %57
57: ; preds = %56
%58 = load i32*, i32** %9
%59 = icmp ne i32* %58, null
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = load i32*, i32** %9
store i32 0, i32* %61
br label %62
62: ; preds = %60, %57
store i32 1, i32* %5
br label %63
63: ; preds = %62, %55, %44, %36, %27
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@errno = global i32 0
define i32 @_ADSReadByte(i32 %0, i8* %1, %struct.timeval* %2, i32* writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i32 @FD_ZERO(i32* %5)
%8 = call i32 @FD_SET(i32 %0, i32* %5)
%9 = add nsw i32 %0, 1
%10 = call i32 @select(i32 %9, i32* %5, i32* null, i32* null, %struct.timeval* %2)
switch i32 %10, label %17 [
i32 -1, label %11
i32 0, label %15
]
11: ; preds = %4
%12 = icmp eq i32* %3, null
br i1 %12, label %28, label %13
13: ; preds = %11
%14 = load i32, i32* @errno
br label %25
15: ; preds = %4
%16 = icmp eq i32* %3, null
br i1 %16, label %28, label %25
17: ; preds = %4
%18 = call i32 @recv(i32 %0, i8* %1, i32 1, i32 0)
switch i32 %18, label %23 [
i32 0, label %25
i32 -1, label %19
]
19: ; preds = %17
%20 = icmp eq i32* %3, null
br i1 %20, label %28, label %21
21: ; preds = %19
%22 = load i32, i32* @errno
br label %25
23: ; preds = %17
%24 = icmp eq i32* %3, null
br i1 %24, label %28, label %25
25: ; preds = %23, %17, %15, %13, %21
%26 = phi i32 [ %22, %21 ], [ %14, %13 ], [ 0, %15 ], [ %18, %17 ], [ 0, %23 ]
%27 = phi i32 [ 0, %21 ], [ 0, %13 ], [ -1, %15 ], [ -2, %17 ], [ 1, %23 ]
store i32 %26, i32* %3
br label %28
28: ; preds = %25, %23, %19, %15, %11
%29 = phi i32 [ 0, %11 ], [ -1, %15 ], [ 0, %19 ], [ 1, %23 ], [ %27, %25 ]
ret i32 %29
}