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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int uart_read(int fd_uart, unsigned char *buf, int sec, int usec, int leng) {
  int ret;
  int cur_leng = 0;
  unsigned char tmp;
  fd_set fd_read;
  struct timeval tv;
  tv.tv_sec = sec;
  tv.tv_usec = usec;
  while (1) {
    FD_ZERO(&fd_read);
    FD_SET(fd_uart, &fd_read);
    ret = select(fd_uart + 1, &fd_read, NULL, NULL, &tv);
    if (ret < 0) {
      printf("select error\n");
      break;
    } else if (ret) {
      if (cur_leng >= leng)
        return cur_leng;
      ret = read(fd_uart, &tmp, 1);
      if (ret > 0) {
        cur_leng++;
        buf[cur_leng - 1] = tmp;
      }
    } else {
      break;
    }
  }
  if (cur_leng > 0)
    return cur_leng;
  return -1;
}


x86 assembly (clang, -O0):
<uart_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x2c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c9 <uart_read+0x79>
lea    0xe41(%rip),%rdi
call   1030 <printf@plt>
jmp    1238 <uart_read+0xe8>
cmpl   $0x0,-0x20(%rbp)
je     1229 <uart_read+0xd9>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     11ea <uart_read+0x9a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1254 <uart_read+0x104>
mov    -0x8(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1224 <uart_read+0xd4>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x25(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    122e <uart_read+0xde>
jmp    1238 <uart_read+0xe8>
jmp    1233 <uart_read+0xe3>
jmp    117c <uart_read+0x2c>
cmpl   $0x0,-0x24(%rbp)
jle    124d <uart_read+0xfd>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1254 <uart_read+0x104>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r15d
mov    %rsi,0x20(%rsp)
mov    %edi,%ebp
mov    %edx,0x10(%rsp)
mov    %ecx,0x14(%rsp)
lea    0x1c(%rsp),%r14
mov    %r14,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
lea    0x1(%rbp),%r12d
xor    %ebx,%ebx
lea    0x10(%rsp),%r8
mov    %r12d,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
js     121b <uart_read+0xcb>
xor    %ebx,%ebx
lea    0x1c(%rsp),%r13
lea    0x10(%rsp),%r14
jmp    11e8 <uart_read+0x98>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   1070 <FD_SET@plt>
mov    %r12d,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1060 <select@plt>
test   %eax,%eax
js     121b <uart_read+0xcb>
test   %eax,%eax
je     1227 <uart_read+0xd7>
cmp    %r15d,%ebx
jge    1231 <uart_read+0xe1>
mov    %ebp,%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11c0 <uart_read+0x70>
movzbl 0xf(%rsp),%eax
movslq %ebx,%rcx
add    $0x1,%ebx
mov    0x20(%rsp),%rdx
mov    %al,(%rdx,%rcx,1)
jmp    11c0 <uart_read+0x70>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
mov    $0xffffffff,%eax
cmovle %eax,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [14 x i8] c"select error\0A\00"
define i32 @uart_read(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8
%15 = alloca i32
%16 = alloca %struct.timeval
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %13
%17 = load i32, i32* %9
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %10
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i32 %19, i32* %20
br label %21
21: ; preds = %5, %58
%22 = call i32 @FD_ZERO(i32* %15)
%23 = load i32, i32* %7
%24 = call i32 @FD_SET(i32 %23, i32* %15)
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
%27 = call i32 @select(i32 %26, i32* %15, i32* null, i32* null, %struct.timeval* %16)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %59
32: ; preds = %21
%33 = load i32, i32* %12
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %56
35: ; preds = %32
%36 = load i32, i32* %13
%37 = load i32, i32* %11
%38 = icmp sge i32 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = load i32, i32* %13
store i32 %40, i32* %6
br label %65
41: ; preds = %35
%42 = load i32, i32* %7
%43 = call i32 @read(i32 %42, i8* %14, i32 1)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %55
46: ; preds = %41
%47 = load i32, i32* %13
%48 = add nsw i32 %47, 1
store i32 %48, i32* %13
%49 = load i8, i8* %14
%50 = load i8*, i8** %8
%51 = load i32, i32* %13
%52 = sub nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
store i8 %49, i8* %54
br label %55
55: ; preds = %46, %41
br label %57
56: ; preds = %32
br label %59
57: ; preds = %55
br label %58
58: ; preds = %57
br label %21
59: ; preds = %56, %30
%60 = load i32, i32* %13
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = load i32, i32* %13
store i32 %63, i32* %6
br label %65
64: ; preds = %59
store i32 -1, i32* %6
br label %65
65: ; preds = %64, %62, %39
%66 = load i32, i32* %6
ret i32 %66
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@str = [13 x i8] c"select error\00"
define i32 @uart_read(i32 %0, i8* nocapture writeonly %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = bitcast i32* %7 to i8*
%10 = bitcast %struct.timeval* %8 to i8*
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i32 %2, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i32 %3, i32* %12
%13 = call i32 @FD_ZERO(i32* %7)
%14 = call i32 @FD_SET(i32 %0, i32* %7)
%15 = add nsw i32 %0, 1
%16 = call i32 @select(i32 %15, i32* %7, i32* null, i32* null, %struct.timeval* %8)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %35, %5
%19 = phi i32 [ 0, %5 ], [ %36, %35 ]
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %41
21: ; preds = %5, %35
%22 = phi i32 [ %39, %35 ], [ %16, %5 ]
%23 = phi i32 [ %36, %35 ], [ 0, %5 ]
%24 = icmp eq i32 %22, 0
br i1 %24, label %41, label %25
25: ; preds = %21
%26 = icmp slt i32 %23, %4
br i1 %26, label %27, label %45
27: ; preds = %25
%28 = call i32 @read(i32 %0, i8* %6, i32 1)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = add nsw i32 %23, 1
%32 = load i8, i8* %6
%33 = sext i32 %23 to i64
%34 = getelementptr inbounds i8, i8* %1, i64 %33
store i8 %32, i8* %34
br label %35
35: ; preds = %30, %27
%36 = phi i32 [ %31, %30 ], [ %23, %27 ]
%37 = call i32 @FD_ZERO(i32* %7)
%38 = call i32 @FD_SET(i32 %0, i32* %7)
%39 = call i32 @select(i32 %15, i32* %7, i32* null, i32* null, %struct.timeval* %8)
%40 = icmp slt i32 %39, 0
br i1 %40, label %18, label %21
41: ; preds = %21, %18
%42 = phi i32 [ %19, %18 ], [ %23, %21 ]
%43 = icmp sgt i32 %42, 0
%44 = select i1 %43, i32 %42, i32 -1
br label %45
45: ; preds = %25, %41
%46 = phi i32 [ %44, %41 ], [ %23, %25 ]
ret i32 %46
}