#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*/ *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int wait_response(int sock, int sec) {
  fd_set rfds;
  struct timeval tv;
  char r;
  FD_ZERO(&rfds);
  FD_SET(sock, &rfds);
  tv.tv_sec = sec;
  tv.tv_usec = 0;
  if (select(sock + 1, &rfds, NULL, NULL, &tv) <= 0)
    return 0;
  if (recv(sock, &r, 1, 0) <= 0)
    return 0;
  return r;
}


x86 assembly (clang, -O0):
<wait_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
jg     11a1 <wait_response+0x61>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <wait_response+0x91>
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jg     11ca <wait_response+0x8a>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <wait_response+0x91>
movsbl -0x21(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_response>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
mov    %ebp,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x1(%rbx),%edi
xor    %r14d,%r14d
lea    0x8(%rsp),%r8
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
jle    11af <wait_response+0x6f>
lea    0x3(%rsp),%rsi
xor    %r14d,%r14d
mov    %ebx,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    11af <wait_response+0x6f>
movsbl 0x3(%rsp),%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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