#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 */

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


x86 assembly (clang, -O0):
<serialport_readByte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1158 <serialport_readByte+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <serialport_readByte+0x86>
cmpl   $0x0,-0x14(%rbp)
jne    1190 <serialport_readByte+0x70>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    118b <serialport_readByte+0x6b>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11a6 <serialport_readByte+0x86>
jmp    1195 <serialport_readByte+0x75>
jmp    119f <serialport_readByte+0x7f>
cmpl   $0x0,-0xc(%rbp)
jg     112e <serialport_readByte+0xe>
movsbl -0xd(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_readByte>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
add    $0x1,%ebx
lea    0xf(%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 <serialport_readByte+0x4c>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
cmp    $0x2,%ebx
je     1178 <serialport_readByte+0x58>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jg     1140 <serialport_readByte+0x20>
jmp    1171 <serialport_readByte+0x51>
cmp    $0xffffffff,%eax
je     117d <serialport_readByte+0x5d>
movsbl 0xf(%rsp),%eax
jmp    117d <serialport_readByte+0x5d>
mov    $0xfffffffe,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @serialport_readByte(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %8
8: ; preds = %27, %2
%9 = load i32, i32* %4
%10 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %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 %34
15: ; preds = %8
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %26
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
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 -2, i32* %3
br label %34
25: ; preds = %18
br label %27
26: ; preds = %15
br label %30
27: ; preds = %25
%28 = load i32, i32* %5
%29 = icmp sgt i32 %28, 0
br i1 %29, label %8, label %30
30: ; preds = %27, %26
%31 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
store i32 %33, i32* %3
br label %34
34: ; preds = %30, %24, %14
%35 = load i32, i32* %3
ret i32 %35
}

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