#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 */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int uart_recv(int fd, char *buf, int len) {
  fd_set rdset;
  int ret;
  struct timeval tm;
  if (fd < 0 || buf == NULL || len == 0)
    return -1;
  FD_ZERO(&rdset);
  FD_SET(fd, &rdset);
  tm.tv_sec = 1;
  tm.tv_usec = 0;
  while (1) {
    ret = select(fd + 1, &rdset, NULL, NULL, &tm);
    if (ret > 0) {
      if (FD_ISSET(fd, &rdset))
        return read(fd, buf, len);
      else
        continue;
    } else if (ret == 0) {
      break;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<uart_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1181 <uart_recv+0x31>
cmpq   $0x0,-0x10(%rbp)
je     1181 <uart_recv+0x31>
cmpl   $0x0,-0x14(%rbp)
jne    118d <uart_recv+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <uart_recv+0xd9>
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0x1,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    120a <uart_recv+0xba>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1205 <uart_recv+0xb5>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x4(%rbp)
jmp    1229 <uart_recv+0xd9>
jmp    11b1 <uart_recv+0x61>
cmpl   $0x0,-0x1c(%rbp)
jne    1219 <uart_recv+0xc9>
jmp    1223 <uart_recv+0xd3>
jmp    121e <uart_recv+0xce>
jmp    11b1 <uart_recv+0x61>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%eax
test   %edi,%edi
js     11f9 <uart_recv+0xa9>
mov    %rsi,%r15
test   %rsi,%rsi
je     11f9 <uart_recv+0xa9>
mov    %edx,%r14d
test   %edx,%edx
je     11f9 <uart_recv+0xa9>
mov    %edi,%r12d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x1(%r12),%ebp
lea    0x8(%rsp),%r13
jmp    11c2 <uart_recv+0x72>
cs nopw 0x0(%rax,%rax,1)
je     11f7 <uart_recv+0xa7>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1060 <select@plt>
test   %eax,%eax
jle    11c0 <uart_recv+0x70>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11c2 <uart_recv+0x72>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
jmp    11f9 <uart_recv+0xa9>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @uart_recv(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timeval
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16, %13, %3
store i32 -1, i32* %4
br label %50
20: ; preds = %16
%21 = call i32 @FD_ZERO(i32* %8)
%22 = load i32, i32* %5
%23 = call i32 @FD_SET(i32 %22, i32* %8)
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 1, i32* %24
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %25
br label %26
26: ; preds = %20, %41, %47
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
%29 = call i32 @select(i32 %28, i32* %8, i32* null, i32* null, %struct.timeval* %10)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %26
%33 = load i32, i32* %5
%34 = call i64 @FD_ISSET(i32 %33, i32* %8)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* %5
%38 = load i8*, i8** %6
%39 = load i32, i32* %7
%40 = call i32 @read(i32 %37, i8* %38, i32 %39)
store i32 %40, i32* %4
br label %50
41: ; preds = %32
br label %26
42: ; preds = %26
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %42
br label %48
46: ; preds = %42
br label %47
47: ; preds = %46
br label %26
48: ; preds = %45
%49 = load i32, i32* %9
store i32 %49, i32* %4
br label %50
50: ; preds = %48, %36, %19
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @uart_recv(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = icmp slt i32 %0, 0
%9 = icmp eq i8* %1, null
%10 = or i1 %8, %9
%11 = icmp eq i32 %2, 0
%12 = or i1 %10, %11
br i1 %12, label %30, label %13
13: ; preds = %3
%14 = call i32 @FD_ZERO(i32* %4)
%15 = call i32 @FD_SET(i32 %0, i32* %4)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %17
%18 = add nuw nsw i32 %0, 1
br label %19
19: ; preds = %25, %13
%20 = call i32 @select(i32 %18, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = call i64 @FD_ISSET(i32 %0, i32* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22, %28
br label %19
26: ; preds = %22
%27 = call i32 @read(i32 %0, i8* %1, i32 %2)
br label %30
28: ; preds = %19
%29 = icmp eq i32 %20, 0
br i1 %29, label %30, label %25
30: ; preds = %28, %3, %26
%31 = phi i32 [ %27, %26 ], [ -1, %3 ], [ 0, %28 ]
ret i32 %31
}