#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 /*<<< orphan*/ output(int, char *, unsigned char);
int /*<<< orphan*/ waitForByte(int, unsigned char *);
int readHCIResponse(int portFD, unsigned char packet[], int maxBytes) {
  unsigned char c;
  int i, ret, n;
  do {
    if (!waitForByte(portFD, &packet[0]))
      return 0;
    if (packet[0] != 0x04)
      output(1, "OOP[%02x] ", c);
  } while (packet[0] != 0x04);
  if (!waitForByte(portFD, &packet[1]))
    return 0;
  if (!waitForByte(portFD, &packet[2]))
    return 0;
  for (i = 0; i < packet[2]; i++) {
    if ((3 + i) >= maxBytes)
      return maxBytes;
    if (!waitForByte(portFD, &packet[3 + i]))
      return 0;
  }
  return packet[2] + 3;
}


x86 assembly (clang, -O0):
<readHCIResponse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <waitForByte@plt>
cmp    $0x0,%eax
jne    1153 <readHCIResponse+0x33>
movl   $0x0,-0x4(%rbp)
jmp    1258 <readHCIResponse+0x138>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x4,%eax
je     1178 <readHCIResponse+0x58>
mov    $0x1,%edi
lea    0xe91(%rip),%rsi
movzbl -0x15(%rbp),%edx
call   1030 <output@plt>
jmp    117d <readHCIResponse+0x5d>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x4,%eax
jne    1132 <readHCIResponse+0x12>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   1040 <waitForByte@plt>
cmp    $0x0,%eax
jne    11b2 <readHCIResponse+0x92>
movl   $0x0,-0x4(%rbp)
jmp    1258 <readHCIResponse+0x138>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
call   1040 <waitForByte@plt>
cmp    $0x0,%eax
jne    11d7 <readHCIResponse+0xb7>
movl   $0x0,-0x4(%rbp)
jmp    1258 <readHCIResponse+0x138>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
cmp    %ecx,%eax
jge    124a <readHCIResponse+0x12a>
mov    -0x1c(%rbp),%eax
add    $0x3,%eax
cmp    -0x14(%rbp),%eax
jl     120b <readHCIResponse+0xeb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1258 <readHCIResponse+0x138>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
add    $0x3,%eax
cltq
add    %rax,%rsi
call   1040 <waitForByte@plt>
cmp    $0x0,%eax
jne    1237 <readHCIResponse+0x117>
movl   $0x0,-0x4(%rbp)
jmp    1258 <readHCIResponse+0x138>
jmp    123c <readHCIResponse+0x11c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11de <readHCIResponse+0xbe>
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
add    $0x3,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readHCIResponse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xec6(%rip),%r12
xor    %r15d,%r15d
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <waitForByte@plt>
test   %eax,%eax
je     11fc <readHCIResponse+0xdc>
cmpb   $0x4,(%rbx)
je     116b <readHCIResponse+0x4b>
mov    $0x1,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <output@plt>
cmpb   $0x4,(%rbx)
jne    1140 <readHCIResponse+0x20>
lea    0x1(%rbx),%rsi
mov    %ebp,%edi
call   1040 <waitForByte@plt>
test   %eax,%eax
je     11fc <readHCIResponse+0xdc>
lea    0x2(%rbx),%r12
mov    %ebp,%edi
mov    %r12,%rsi
call   1040 <waitForByte@plt>
test   %eax,%eax
je     11fc <readHCIResponse+0xdc>
cmpb   $0x0,(%r12)
je     11e8 <readHCIResponse+0xc8>
cmp    $0x4,%r14d
mov    $0x3,%r13d
mov    %r14d,0x4(%rsp)
cmovge %r14d,%r13d
add    $0xfffffffd,%r13d
add    $0x3,%rbx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %r14,%r13
je     11f0 <readHCIResponse+0xd0>
lea    (%rbx,%r14,1),%rsi
mov    %ebp,%edi
call   1040 <waitForByte@plt>
test   %eax,%eax
je     11f9 <readHCIResponse+0xd9>
add    $0x1,%r14
movzbl (%r12),%r15d
cmp    %r15,%r14
jb     11c0 <readHCIResponse+0xa0>
add    $0x3,%r15d
jmp    11fc <readHCIResponse+0xdc>
mov    $0x3,%r15d
jmp    11fc <readHCIResponse+0xdc>
mov    0x4(%rsp),%eax
mov    %eax,%r15d
jmp    11fc <readHCIResponse+0xdc>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"OOP[%02x] \00"
define i32 @readHCIResponse(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %12
12: ; preds = %29, %3
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = call i32 @waitForByte(i32 %13, i8* %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %12
store i32 0, i32* %4
br label %84
19: ; preds = %12
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = icmp ne i32 %23, 4
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i8, i8* %8
%27 = call i32 @output(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 zeroext %26)
br label %28
28: ; preds = %25, %19
br label %29
29: ; preds = %28
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = icmp ne i32 %33, 4
br i1 %34, label %12, label %35
35: ; preds = %29
%36 = load i32, i32* %5
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = call i32 @waitForByte(i32 %36, i8* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %35
store i32 0, i32* %4
br label %84
42: ; preds = %35
%43 = load i32, i32* %5
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 2
%46 = call i32 @waitForByte(i32 %43, i8* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %42
store i32 0, i32* %4
br label %84
49: ; preds = %42
store i32 0, i32* %9
br label %50
50: ; preds = %75, %49
%51 = load i32, i32* %9
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i64 2
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = icmp slt i32 %51, %55
br i1 %56, label %57, label %78
57: ; preds = %50
%58 = load i32, i32* %9
%59 = add nsw i32 3, %58
%60 = load i32, i32* %7
%61 = icmp sge i32 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = load i32, i32* %7
store i32 %63, i32* %4
br label %84
64: ; preds = %57
%65 = load i32, i32* %5
%66 = load i8*, i8** %6
%67 = load i32, i32* %9
%68 = add nsw i32 3, %67
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %66, i64 %69
%71 = call i32 @waitForByte(i32 %65, i8* %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %74, label %73
73: ; preds = %64
store i32 0, i32* %4
br label %84
74: ; preds = %64
br label %75
75: ; preds = %74
%76 = load i32, i32* %9
%77 = add nsw i32 %76, 1
store i32 %77, i32* %9
br label %50
78: ; preds = %50
%79 = load i8*, i8** %6
%80 = getelementptr inbounds i8, i8* %79, i64 2
%81 = load i8, i8* %80
%82 = zext i8 %81 to i32
%83 = add nsw i32 %82, 3
store i32 %83, i32* %4
br label %84
84: ; preds = %78, %73, %62, %48, %41, %18
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
@.str = [11 x i8] c"OOP[%02x] \00"
define i32 @readHCIResponse(i32 %0, i8* %1, i32 %2) {
br label %4
4: ; preds = %10, %3
%5 = tail call i32 @waitForByte(i32 %0, i8* %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %45, label %7
7: ; preds = %4
%8 = load i8, i8* %1
%9 = icmp eq i8 %8, 4
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = tail call i32 @output(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 zeroext undef)
%12 = load i8, i8* %1
%13 = icmp eq i8 %12, 4
br i1 %13, label %14, label %4
14: ; preds = %7, %10
%15 = getelementptr inbounds i8, i8* %1, i64 1
%16 = tail call i32 @waitForByte(i32 %0, i8* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %45, label %18
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %1, i64 2
%20 = tail call i32 @waitForByte(i32 %0, i8* %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %45, label %22
22: ; preds = %18
%23 = load i8, i8* %19
%24 = icmp eq i8 %23, 0
br i1 %24, label %45, label %25
25: ; preds = %22
%26 = call i32 @llvm.smax.i32(i32 %2, i32 3)
%27 = add nsw i32 %26, -3
%28 = zext i32 %27 to i64
br label %33
29: ; preds = %36
%30 = load i8, i8* %19
%31 = zext i8 %30 to i64
%32 = icmp ult i64 %41, %31
br i1 %32, label %33, label %42
33: ; preds = %25, %29
%34 = phi i64 [ 0, %25 ], [ %41, %29 ]
%35 = icmp eq i64 %34, %28
br i1 %35, label %45, label %36
36: ; preds = %33
%37 = add nuw nsw i64 %34, 3
%38 = getelementptr inbounds i8, i8* %1, i64 %37
%39 = tail call i32 @waitForByte(i32 %0, i8* %38)
%40 = icmp eq i32 %39, 0
%41 = add nuw nsw i64 %34, 1
br i1 %40, label %45, label %29
42: ; preds = %29
%43 = zext i8 %30 to i32
%44 = add nuw nsw i32 %43, 3
br label %45
45: ; preds = %4, %36, %33, %42, %22, %18, %14
%46 = phi i32 [ 0, %14 ], [ 0, %18 ], [ %44, %42 ], [ 3, %22 ], [ 0, %36 ], [ %2, %33 ], [ 0, %4 ]
ret i32 %46
}