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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
long recv(int, void *, long, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
long http_recv_timedout(int socket, void *buffer, long length, int flags,
                        int timeout) {
  fd_set fds;
  int n;
  struct timeval tv;
  FD_ZERO(&fds);
  FD_SET(socket, &fds);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  n = select(socket + 1, &fds, NULL, NULL, &tv);
  if (n == 0)
    return -2;
  if (n == -1)
    return -1;
  return recv(socket, buffer, length, 0);
}


x86 assembly (clang, -O0):
<http_recv_timedout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    11b1 <http_recv_timedout+0x71>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    11de <http_recv_timedout+0x9e>
cmpl   $0xffffffff,-0x30(%rbp)
jne    11c8 <http_recv_timedout+0x88>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11de <http_recv_timedout+0x9e>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_recv_timedout>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8d,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
lea    0xc(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
mov    %ebp,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x1(%rbx),%edi
lea    0x10(%rsp),%r8
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     11a1 <http_recv_timedout+0x61>
cmp    $0xffffffff,%eax
jne    11aa <http_recv_timedout+0x6a>
mov    $0xffffffffffffffff,%rax
jmp    11b9 <http_recv_timedout+0x79>
mov    $0xfffffffffffffffe,%rax
jmp    11b9 <http_recv_timedout+0x79>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i64 @http_recv_timedout(i32 %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = bitcast i32* %6 to i8*
%9 = bitcast %struct.timeval* %7 to i8*
%10 = call i32 @FD_ZERO(i32* %6)
%11 = call i32 @FD_SET(i32 %0, i32* %6)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %4, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %13
%14 = add nsw i32 %0, 1
%15 = call i32 @select(i32 %14, i32* %6, i32* null, i32* null, %struct.timeval* %7)
switch i32 %15, label %17 [
i32 0, label %19
i32 -1, label %16
]
16: ; preds = %5
br label %19
17: ; preds = %5
%18 = call i64 @recv(i32 %0, i8* %1, i64 %2, i32 0)
br label %19
19: ; preds = %5, %17, %16
%20 = phi i64 [ -1, %16 ], [ %18, %17 ], [ -2, %5 ]
ret i64 %20
}