#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 EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int errno;
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int wait_command(int fd, char *buffer, int timeout) {
  fd_set rfds;
  struct timeval tv;
  int retval;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  retval = select(fd + 1, &rfds, NULL, NULL, &tv);
  switch (retval) {
  case -1:
    return -errno;
  case 0:
    return -EAGAIN;
  default:
    return (read(fd, buffer, 4) == 4) ? 0 : -errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<wait_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    %rdi,-0x38(%rbp)
call   1040 <FD_ZERO@plt>
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1060 <FD_SET@plt>
mov    -0x38(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0xffffffff,%eax
je     11ba <wait_command+0x7a>
jmp    11aa <wait_command+0x6a>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     11cd <wait_command+0x8d>
jmp    11e0 <wait_command+0xa0>
xor    %eax,%eax
mov    0x2e1d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <wait_command+0xd8>
xor    %eax,%eax
mov    0x2dfa(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <wait_command+0xd8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
cmp    $0x4,%eax
jne    1204 <wait_command+0xc4>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    1212 <wait_command+0xd2>
xor    %eax,%eax
mov    0x2dd3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_command>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
mov    %ebx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x1(%r15),%edi
xor    %ebx,%ebx
lea    0x8(%rsp),%r8
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     11a5 <wait_command+0x65>
cmp    $0xffffffff,%eax
jne    11b0 <wait_command+0x70>
mov    0x2e41(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11d3 <wait_command+0x93>
mov    0x2e24(%rip),%rax
sub    (%rax),%ebx
jmp    11d3 <wait_command+0x93>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    0x2e19(%rip),%rcx
xor    %ebx,%ebx
sub    (%rcx),%ebx
xor    %ecx,%ecx
cmp    $0x4,%eax
cmove  %ecx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i32 0
@EAGAIN = global i32 0
define i32 @wait_command(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 i32
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 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %16
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
%19 = call i32 @select(i32 %18, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %19, i32* %10
%20 = load i32, i32* %10
switch i32 %20, label %27 [
i32 -1, label %21
i32 0, label %24
]
21: ; preds = %3
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %4
br label %38
24: ; preds = %3
%25 = load i32, i32* @EAGAIN
%26 = sub nsw i32 0, %25
store i32 %26, i32* %4
br label %38
27: ; preds = %3
%28 = load i32, i32* %5
%29 = load i8*, i8** %6
%30 = call i32 @read(i32 %28, i8* %29, i32 4)
%31 = icmp eq i32 %30, 4
br i1 %31, label %32, label %33
32: ; preds = %27
br label %36
33: ; preds = %27
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
br label %36
36: ; preds = %33, %32
%37 = phi i32 [ 0, %32 ], [ %35, %33 ]
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %24, %21
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i32 0
@EAGAIN = global i32 0
define i32 @wait_command(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 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %2, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %11
%12 = add nsw i32 %0, 1
%13 = call i32 @select(i32 %12, i32* %4, i32* null, i32* null, %struct.timeval* %5)
switch i32 %13, label %20 [
i32 -1, label %14
i32 0, label %17
]
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
br label %26
17: ; preds = %3
%18 = load i32, i32* @EAGAIN
%19 = sub nsw i32 0, %18
br label %26
20: ; preds = %3
%21 = call i32 @read(i32 %0, i8* %1, i32 4)
%22 = icmp eq i32 %21, 4
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
%25 = select i1 %22, i32 0, i32 %24
br label %26
26: ; preds = %20, %17, %14
%27 = phi i32 [ %25, %20 ], [ %19, %17 ], [ %16, %14 ]
ret i32 %27
}