#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 */
typedef int uint8_t;

/* Variables and functions */
int read(int, int *, int);
int /*<<< orphan*/ usleep(int);
uint8_t serial_read(int fd, int timeout) {
  uint8_t b[1];
  do {
    int n = read(fd, b, 1);
    if (n == -1)
      return -1;
    if (n == 0) {
      usleep(1 * 1000);
      timeout--;
      continue;
    }
    return b[0];
  } while (timeout > 0);
  return -1;
}


x86 assembly (clang, -O0):
<serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1158 <serial_read+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <serial_read+0x76>
cmpl   $0x0,-0x14(%rbp)
jne    117a <serial_read+0x5a>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1185 <serial_read+0x65>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <serial_read+0x76>
cmpl   $0x0,-0xc(%rbp)
jg     112e <serial_read+0xe>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
add    $0x1,%ebx
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jne    116c <serial_read+0x4c>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jg     1140 <serial_read+0x20>
mov    $0xffffffff,%eax
jmp    1175 <serial_read+0x55>
cmp    $0xffffffff,%eax
je     1175 <serial_read+0x55>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @serial_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1 x i32]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %8
8: ; preds = %25, %2
%9 = load i32, i32* %4
%10 = getelementptr inbounds [1 x i32], [1 x i32]* %6, i64 0, i64 0
%11 = call i32 @read(i32 %9, i32* %10, i32 1)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %8
store i32 -1, i32* %3
br label %29
15: ; preds = %8
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = call i32 @usleep(i32 1000)
%20 = load i32, i32* %5
%21 = add nsw i32 %20, -1
store i32 %21, i32* %5
br label %25
22: ; preds = %15
%23 = getelementptr inbounds [1 x i32], [1 x i32]* %6, i64 0, i64 0
%24 = load i32, i32* %23
store i32 %24, i32* %3
br label %29
25: ; preds = %18
%26 = load i32, i32* %5
%27 = icmp sgt i32 %26, 0
br i1 %27, label %8, label %28
28: ; preds = %25
store i32 -1, i32* %3
br label %29
29: ; preds = %28, %22, %14
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @serial_read(i32 %0, i32 %1) {
%3 = alloca [1 x i32]
%4 = bitcast [1 x i32]* %3 to i8*
%5 = getelementptr inbounds [1 x i32], [1 x i32]* %3, i64 0, i64 0
br label %6
6: ; preds = %11, %2
%7 = phi i32 [ %1, %2 ], [ %13, %11 ]
%8 = call i32 @read(i32 %0, i32* %5, i32 1)
switch i32 %8, label %9 [
i32 -1, label %15
i32 0, label %11
]
9: ; preds = %6
%10 = load i32, i32* %5
br label %15
11: ; preds = %6
%12 = tail call i32 @usleep(i32 1000)
%13 = add nsw i32 %7, -1
%14 = icmp sgt i32 %7, 1
br i1 %14, label %6, label %15
15: ; preds = %6, %11, %9
%16 = phi i32 [ %10, %9 ], [ %8, %6 ], [ -1, %11 ]
ret i32 %16
}