#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 */
unsigned int getTimeFromStart();
int /*<<< orphan*/ output(int, char *);
int read(int, unsigned char *, int);
int waitForByte(int portFD, unsigned char *c) {
  int i, ret;
  unsigned int t;
  unsigned int t0;
  t0 = getTimeFromStart();
  do {
    ret = read(portFD, c, 1);
    if (ret == 1) {
      return 1;
    }
  } while ((getTimeFromStart() - t0) < 2000000);
  output(1, "Timed out (2sec) waiting for some data.\n");
  return 0;
}


x86 assembly (clang, -O0):
<waitForByte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getTimeFromStart@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    1173 <waitForByte+0x43>
movl   $0x1,-0x4(%rbp)
jmp    11a5 <waitForByte+0x75>
jmp    1178 <waitForByte+0x48>
mov    $0x0,%al
call   1050 <getTimeFromStart@plt>
sub    -0x20(%rbp),%eax
cmp    $0x1e8480,%eax
jb     1149 <waitForByte+0x19>
mov    $0x1,%edi
lea    0xe67(%rip),%rsi
call   1030 <output@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitForByte>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
xor    %eax,%eax
call   1050 <getTimeFromStart@plt>
mov    %eax,%ebx
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1188 <waitForByte+0x58>
xor    %eax,%eax
call   1050 <getTimeFromStart@plt>
sub    %ebx,%eax
cmp    $0x1e8480,%eax
jb     1150 <waitForByte+0x20>
lea    0xe84(%rip),%rsi
mov    $0x1,%edi
call   1030 <output@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Timed out (2sec) waiting for some data.\0A\00"
define i32 @waitForByte(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = call i32 (...) @getTimeFromStart()
store i32 %10, i32* %9
br label %11
11: ; preds = %19, %2
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = call i32 @read(i32 %12, i8* %13, i32 1)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %11
store i32 1, i32* %3
br label %26
18: ; preds = %11
br label %19
19: ; preds = %18
%20 = call i32 (...) @getTimeFromStart()
%21 = load i32, i32* %9
%22 = sub i32 %20, %21
%23 = icmp ult i32 %22, 2000000
br i1 %23, label %11, label %24
24: ; preds = %19
%25 = call i32 @output(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %26
26: ; preds = %24, %17
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str = [41 x i8] c"Timed out (2sec) waiting for some data.\0A\00"
define i32 @waitForByte(i32 %0, i8* nocapture %1) {
%3 = tail call i32 (...) @getTimeFromStart()
br label %4
4: ; preds = %7, %2
%5 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = tail call i32 (...) @getTimeFromStart()
%9 = sub i32 %8, %3
%10 = icmp ult i32 %9, 2000000
br i1 %10, label %4, label %11
11: ; preds = %7
%12 = tail call i32 @output(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %4, %11
%14 = phi i32 [ 0, %11 ], [ 1, %4 ]
ret i32 %14
}