#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 */
typedef int /*<<< orphan*/ uint8_t;
struct timeval {
  int member_0;
  int /*<<< orphan*/ member_1;
};
typedef scalar_t__ int8_t;
typedef scalar_t__ int16_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(scalar_t__);
scalar_t__ errno;
int /*<<< orphan*/ exit(scalar_t__);
int /*<<< orphan*/ perror(char *);
scalar_t__ recv(scalar_t__, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timeval *);
int8_t attoHTTPGetByte(void *read, uint8_t *byte) {
  int16_t sock = *(int16_t *)read;
  int16_t ret = 0;
  struct timeval timeout = {1, 0};
  fd_set active;
  if (sock > 0) {
    FD_ZERO(&active);
    FD_SET(sock, &active);
    do {
      ret = select(FD_SETSIZE, &active, NULL, NULL, &timeout);
      if (ret < 0) {
        if (errno != EINTR) {
          perror("select");
          close(sock);
          exit(errno);
        }
      } else if ((ret > 0) && FD_ISSET(sock, &active)) {
        ret = recv(sock, byte, 1, 0);
      }
    } while (ret < 0);
  }
  return ret;
}


x86 assembly (clang, -O0):
<attoHTTPGetByte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0xe56(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    1292 <attoHTTPGetByte+0x112>
lea    -0x2c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    123e <attoHTTPGetByte+0xbe>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
je     1239 <attoHTTPGetByte+0xb9>
lea    0xdec(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <close@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%rdi
call   1090 <exit@plt>
jmp    127d <attoHTTPGetByte+0xfd>
cmpq   $0x0,-0x20(%rbp)
jle    1278 <attoHTTPGetByte+0xf8>
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1278 <attoHTTPGetByte+0xf8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x20(%rbp)
jmp    127d <attoHTTPGetByte+0xfd>
jmp    1282 <attoHTTPGetByte+0x102>
cmpq   $0x0,-0x20(%rbp)
jl     11cf <attoHTTPGetByte+0x4f>
jmp    1292 <attoHTTPGetByte+0x112>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attoHTTPGetByte>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    (%rdi),%r13
movq   $0x1,0x8(%rsp)
test   %r13,%r13
jle    1233 <attoHTTPGetByte+0xb3>
mov    %rsi,%r14
lea    0x14(%rsp),%r15
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   10a0 <FD_SET@plt>
mov    0x2e1b(%rip),%rbp
lea    0x8(%rsp),%r12
jmp    11eb <attoHTTPGetByte+0x6b>
nopl   0x0(%rax)
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    1247 <attoHTTPGetByte+0xc7>
test   %rbx,%rbx
jns    1235 <attoHTTPGetByte+0xb5>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1070 <select@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11d0 <attoHTTPGetByte+0x50>
je     1233 <attoHTTPGetByte+0xb3>
mov    %r13,%rdi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1235 <attoHTTPGetByte+0xb5>
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%rbx
test   %rbx,%rbx
js     11eb <attoHTTPGetByte+0x6b>
jmp    1235 <attoHTTPGetByte+0xb5>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    %r13,%rdi
call   1050 <close@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%rdi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.attoHTTPGetByte.timeout = %struct.timeval { i32 1, i32 0 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [7 x i8] c"select\00"
define i64 @attoHTTPGetByte(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca %struct.timeval
%8 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%9 = load i8*, i8** %3
%10 = bitcast i8* %9 to i64*
%11 = load i64, i64* %10
store i64 %11, i64* %5
store i64 0, i64* %6
%12 = bitcast %struct.timeval* %7 to i8*
%13 = load i64, i64* %5
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %52
15: ; preds = %2
%16 = call i32 @FD_ZERO(i32* %8)
%17 = load i64, i64* %5
%18 = call i32 @FD_SET(i64 %17, i32* %8)
br label %19
19: ; preds = %48, %15
%20 = load i32, i32* @FD_SETSIZE
%21 = call i64 @select(i32 %20, i32* %8, i32* null, i32* null, %struct.timeval* %7)
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%30 = load i64, i64* %5
%31 = call i32 @close(i64 %30)
%32 = load i64, i64* @errno
%33 = call i32 @exit(i64 %32)
unreachable
34: ; preds = %24
br label %47
35: ; preds = %19
%36 = load i64, i64* %6
%37 = icmp sgt i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %35
%39 = load i64, i64* %5
%40 = call i64 @FD_ISSET(i64 %39, i32* %8)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i64, i64* %5
%44 = load i32*, i32** %4
%45 = call i64 @recv(i64 %43, i32* %44, i32 1, i32 0)
store i64 %45, i64* %6
br label %46
46: ; preds = %42, %38, %35
br label %47
47: ; preds = %46, %34
br label %48
48: ; preds = %47
%49 = load i64, i64* %6
%50 = icmp slt i64 %49, 0
br i1 %50, label %19, label %51
51: ; preds = %48
br label %52
52: ; preds = %51, %2
%53 = load i64, i64* %6
ret i64 %53
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [7 x i8] c"select\00"
define i64 @attoHTTPGetByte(i8* nocapture %0, i32* %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timeval*
%5 = alloca i32
%6 = bitcast i8* %0 to i64*
%7 = load i64, i64* %6
%8 = bitcast i64* %3 to i8*
store i64 1, i64* %3
%9 = bitcast i32* %5 to i8*
%10 = icmp sgt i64 %7, 0
br i1 %10, label %11, label %37
11: ; preds = %2
%12 = call i32 @FD_ZERO(i32* %5)
%13 = call i32 @FD_SET(i64 %7, i32* %5)
br label %14
14: ; preds = %34, %11
%15 = load i32, i32* @FD_SETSIZE
%16 = call i64 @select(i32 %15, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %34, label %22
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @close(i64 %7)
%25 = load i64, i64* @errno
%26 = call i32 @exit(i64 %25)
unreachable
27: ; preds = %14
%28 = icmp eq i64 %16, 0
br i1 %28, label %37, label %29
29: ; preds = %27
%30 = call i64 @FD_ISSET(i64 %7, i32* %5)
%31 = icmp eq i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %29
%33 = call i64 @recv(i64 %7, i32* %1, i32 1, i32 0)
br label %34
34: ; preds = %18, %32
%35 = phi i64 [ %16, %18 ], [ %33, %32 ]
%36 = icmp slt i64 %35, 0
br i1 %36, label %14, label %37
37: ; preds = %27, %29, %34, %2
%38 = phi i64 [ 0, %2 ], [ %16, %29 ], [ 0, %27 ], [ %35, %34 ]
ret i64 %38
}