#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ FNDELAY;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ loadFirmware(unsigned char *);
int /*<<< orphan*/ output(int, char *, ...);
int readHCIResponse(int, unsigned char *, int);
int write(int, unsigned char *, int);
void doHCIComms(int portFD) {
  FILE *in;
  unsigned char data[1024];
  unsigned long tmp1, tmp2, tmp3;
  int i, ret, n, line;
  in = fopen("/etc/packets.txt", "rt");
  if (!in) {
    output(1, "Cannot open packets.txt\n");
    return;
  }
  fcntl(portFD, F_SETFL, FNDELAY);
  line = 1;
  while (!feof(in)) {
    ret = fscanf(in, "%x %x %x", &tmp1, &tmp2, &tmp3);
    if (ret != 3) {
      output(1, "Line %i: Need at least 3 bytes\n", line);
      break;
    }
    if (tmp1 == 0xAB && tmp2 == 0xAB && tmp3 == 0xAB) {
      fscanf(in, "%s %x", data, &tmp1);
      if (tmp1 != 0xFF) {
        output(1,
               "Line %i: Firmware read signature found but incorrect "
               "terminator.\n",
               line);
        break;
      }
      loadFirmware(data);
      continue;
    }
    data[0] = 0x01;
    data[1] = tmp1;
    data[2] = tmp2;
    data[3] = tmp3;
    n = (int)tmp3;
    if ((n + 4) > 1024) {
      output(1, "Line %i: Packet too long\n", line);
    }
    for (i = 0; i < n; i++) {
      ret = fscanf(in, "%x", &tmp1);
      data[4 + i] = tmp1;
      if (ret != 1) {
        output(1, "Line %i: Failed loading byte %i\n", line, i);
        break;
      }
    }
    ret = fscanf(in, "%x", &tmp1);
    if (ret != 1 || tmp1 != 0xFF) {
      output(1, "Line %i: Didn't or couldn't get 0xFF terminator\n", line);
      break;
    }
    output(1, "Sending: ");
    for (i = 0; i < n + 4; i++)
      output(1, "%02x ", data[i]);
    output(1, "...");
    ret = write(portFD, data, (n + 4));
    output(1, " sent %i of %i\n", ret, (n + 4));
    ret = readHCIResponse(portFD, data, 1024);
    output(1, "Received %i bytes: ", ret);
    if (ret > 0)
      for (i = 0; i < ret; i++)
        output(1, "%02x ", data[i]);
    output(1, "\n");
    line++;
  }
  fclose(in);
}


x86 assembly (clang, -O0):
<doHCIComms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe5b(%rip),%rdi
lea    0xe65(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d8 <doHCIComms+0x48>
mov    $0x1,%edi
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
jmp    15ec <doHCIComms+0x45c>
mov    -0x4(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
movl   $0x1,-0x438(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    121a <doHCIComms+0x8a>
jmp    15e3 <doHCIComms+0x453>
mov    -0x10(%rbp),%rdi
lea    0xe08(%rip),%rsi
lea    -0x418(%rbp),%rdx
lea    -0x420(%rbp),%rcx
lea    -0x428(%rbp),%r8
mov    $0x0,%al
call   1070 <fscanf@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0x3,-0x430(%rbp)
je     1272 <doHCIComms+0xe2>
mov    -0x438(%rbp),%edx
mov    $0x1,%edi
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
jmp    15e3 <doHCIComms+0x453>
cmpq   $0xab,-0x418(%rbp)
jne    1305 <doHCIComms+0x175>
cmpq   $0xab,-0x420(%rbp)
jne    1305 <doHCIComms+0x175>
cmpq   $0xab,-0x428(%rbp)
jne    1305 <doHCIComms+0x175>
mov    -0x10(%rbp),%rdi
lea    -0x410(%rbp),%rdx
lea    0xd9f(%rip),%rsi
lea    -0x418(%rbp),%rcx
mov    $0x0,%al
call   1070 <fscanf@plt>
cmpq   $0xff,-0x418(%rbp)
je     12f4 <doHCIComms+0x164>
mov    -0x438(%rbp),%edx
mov    $0x1,%edi
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
jmp    15e3 <doHCIComms+0x453>
lea    -0x410(%rbp),%rdi
call   10a0 <loadFirmware@plt>
jmp    11fc <doHCIComms+0x6c>
movb   $0x1,-0x410(%rbp)
mov    -0x418(%rbp),%rax
mov    %al,-0x40f(%rbp)
mov    -0x420(%rbp),%rax
mov    %al,-0x40e(%rbp)
mov    -0x428(%rbp),%rax
mov    %al,-0x40d(%rbp)
mov    -0x428(%rbp),%rax
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%eax
add    $0x4,%eax
cmp    $0x400,%eax
jle    136d <doHCIComms+0x1dd>
mov    -0x438(%rbp),%edx
mov    $0x1,%edi
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
movl   $0x0,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x434(%rbp),%eax
jge    140d <doHCIComms+0x27d>
mov    -0x10(%rbp),%rdi
lea    0xcc5(%rip),%rsi
lea    -0x418(%rbp),%rdx
mov    $0x0,%al
call   1070 <fscanf@plt>
mov    %eax,-0x430(%rbp)
mov    -0x418(%rbp),%rax
mov    %al,%cl
mov    -0x42c(%rbp),%eax
add    $0x4,%eax
cltq
mov    %cl,-0x410(%rbp,%rax,1)
cmpl   $0x1,-0x430(%rbp)
je     13f4 <doHCIComms+0x264>
mov    -0x438(%rbp),%edx
mov    -0x42c(%rbp),%ecx
mov    $0x1,%edi
lea    0xcd0(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
jmp    140d <doHCIComms+0x27d>
jmp    13f9 <doHCIComms+0x269>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    1377 <doHCIComms+0x1e7>
mov    -0x10(%rbp),%rdi
lea    0xc41(%rip),%rsi
lea    -0x418(%rbp),%rdx
mov    $0x0,%al
call   1070 <fscanf@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0x1,-0x430(%rbp)
jne    144a <doHCIComms+0x2ba>
cmpq   $0xff,-0x418(%rbp)
je     1468 <doHCIComms+0x2d8>
mov    -0x438(%rbp),%edx
mov    $0x1,%edi
lea    0xc7d(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
jmp    15e3 <doHCIComms+0x453>
mov    $0x1,%edi
lea    0xc96(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
movl   $0x0,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
mov    -0x434(%rbp),%ecx
add    $0x4,%ecx
cmp    %ecx,%eax
jge    14d2 <doHCIComms+0x342>
movslq -0x42c(%rbp),%rax
movzbl -0x410(%rbp,%rax,1),%edx
ff
mov    $0x1,%edi
lea    0xc5d(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    1485 <doHCIComms+0x2f5>
mov    $0x1,%edi
lea    0xc3c(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x434(%rbp),%edx
add    $0x4,%edx
call   1040 <write@plt>
mov    %eax,-0x430(%rbp)
mov    -0x430(%rbp),%edx
mov    -0x434(%rbp),%ecx
add    $0x4,%ecx
mov    $0x1,%edi
lea    0xc00(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1090 <readHCIResponse@plt>
mov    %eax,-0x430(%rbp)
mov    -0x430(%rbp),%edx
mov    $0x1,%edi
lea    0xbdd(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
cmpl   $0x0,-0x430(%rbp)
jle    15bc <doHCIComms+0x42c>
movl   $0x0,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x430(%rbp),%eax
jge    15b7 <doHCIComms+0x427>
movslq -0x42c(%rbp),%rax
movzbl -0x410(%rbp,%rax,1),%edx
ff
mov    $0x1,%edi
lea    0xb78(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    156f <doHCIComms+0x3df>
jmp    15bc <doHCIComms+0x42c>
mov    $0x1,%edi
lea    0xad4(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    -0x438(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x438(%rbp)
jmp    11fc <doHCIComms+0x6c>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doHCIComms>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %edi,%ebx
lea    0xe56(%rip),%rdi
lea    0xe60(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1481 <doHCIComms+0x2f1>
mov    %rax,%r12
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %ebx,0x1c(%rsp)
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    14c5 <doHCIComms+0x335>
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%rbx
lea    0x20(%rsp),%r14
lea    0x30(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
lea    0xe13(%rip),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
lea    0x28(%rsp),%r8
xor    %eax,%eax
call   1070 <fscanf@plt>
cmp    $0x3,%eax
jne    14a5 <doHCIComms+0x315>
mov    0x10(%rsp),%rax
mov    0x20(%rsp),%rcx
mov    0x28(%rsp),%r15
cmp    $0xab,%rax
jne    12a1 <doHCIComms+0x111>
cmp    $0xab,%rcx
jne    12a1 <doHCIComms+0x111>
cmp    $0xab,%r15
jne    12a1 <doHCIComms+0x111>
mov    %r12,%rdi
lea    0xdee(%rip),%rsi
mov    %r13,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1070 <fscanf@plt>
cmpq   $0xff,0x10(%rsp)
jne    14ae <doHCIComms+0x31e>
mov    %r13,%rdi
call   10a0 <loadFirmware@plt>
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     1210 <doHCIComms+0x80>
jmp    14c5 <doHCIComms+0x335>
movb   $0x1,0x30(%rsp)
mov    %al,0x31(%rsp)
mov    %cl,0x32(%rsp)
mov    %r15b,0x33(%rsp)
cmp    $0x3fd,%r15d
jl     12dc <doHCIComms+0x14c>
mov    $0x1,%edi
lea    0xdd6(%rip),%rsi
mov    0xc(%rsp),%edx
xor    %eax,%eax
call   1050 <output@plt>
lea    0xd7f(%rip),%rbp
jmp    12e8 <doHCIComms+0x158>
test   %r15d,%r15d
lea    0xd73(%rip),%rbp
jle    1336 <doHCIComms+0x1a6>
mov    %r15d,%ebx
xor    %r14d,%r14d
xchg   %ax,%ax
mov    %r12,%rdi
mov    %rbp,%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1070 <fscanf@plt>
movzbl 0x10(%rsp),%ecx
mov    %cl,0x34(%rsp,%r14,1)
cmp    $0x1,%eax
jne    131c <doHCIComms+0x18c>
add    $0x1,%r14
cmp    %r14,%rbx
jne    12f0 <doHCIComms+0x160>
jmp    1336 <doHCIComms+0x1a6>
mov    $0x1,%edi
lea    0xd90(%rip),%rsi
mov    0xc(%rsp),%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <output@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1070 <fscanf@plt>
cmp    $0x1,%eax
jne    14df <doHCIComms+0x34f>
cmpq   $0xff,0x10(%rsp)
jne    14df <doHCIComms+0x34f>
lea    0x4(%r15),%ebx
mov    $0x1,%edi
lea    0xd9a(%rip),%rsi
xor    %eax,%eax
call   1050 <output@plt>
cmp    $0xfffffffd,%r15d
lea    0xd92(%rip),%r15
jl     13bd <doHCIComms+0x22d>
cmp    $0x2,%ebx
mov    $0x1,%r14d
cmovge %ebx,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl 0x30(%rsp,%rbp,1),%edx
mov    $0x1,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <output@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    13a0 <doHCIComms+0x210>
mov    $0x1,%edi
lea    0xd51(%rip),%rsi
xor    %eax,%eax
call   1050 <output@plt>
mov    0x1c(%rsp),%ebp
mov    %ebp,%edi
mov    %r13,%r14
mov    %r13,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    $0x1,%edi
lea    0xd2f(%rip),%rsi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <output@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1090 <readHCIResponse@plt>
mov    %eax,%r14d
mov    $0x1,%edi
lea    0xd16(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <output@plt>
test   %r14d,%r14d
jle    144d <doHCIComms+0x2bd>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movzbl 0x30(%rsp,%rbx,1),%edx
mov    $0x1,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <output@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1430 <doHCIComms+0x2a0>
mov    $0x1,%edi
lea    0xc43(%rip),%rsi
xor    %eax,%eax
call   1050 <output@plt>
addl   $0x1,0xc(%rsp)
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
lea    0x10(%rsp),%rbx
lea    0x20(%rsp),%r14
je     1210 <doHCIComms+0x80>
jmp    14c5 <doHCIComms+0x335>
lea    0xb8c(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <output@plt>
lea    0xb8a(%rip),%rsi
jmp    14b5 <doHCIComms+0x325>
lea    0xba7(%rip),%rsi
mov    $0x1,%edi
mov    0xc(%rsp),%edx
xor    %eax,%eax
call   1050 <output@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbf3(%rip),%rsi
jmp    14b5 <doHCIComms+0x325>

LLVM IR (-O0):
@.str = [17 x i8] c"/etc/packets.txt\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [25 x i8] c"Cannot open packets.txt\0A\00"
@F_SETFL = global i32 0
@FNDELAY = global i32 0
@.str.3 = [9 x i8] c"%x %x %x\00"
@.str.4 = [32 x i8] c"Line %i: Need at least 3 bytes\0A\00"
@.str.5 = [6 x i8] c"%s %x\00"
@.str.6 = [66 x i8] c"Line %i: Firmware read signature found but incorrect terminator.\0A\00"
@.str.7 = [26 x i8] c"Line %i: Packet too long\0A\00"
@.str.8 = [3 x i8] c"%x\00"
@.str.9 = [33 x i8] c"Line %i: Failed loading byte %i\0A\00"
@.str.10 = [49 x i8] c"Line %i: Didn't or couldn't get 0xFF terminator\0A\00"
@.str.11 = [10 x i8] c"Sending: \00"
@.str.12 = [6 x i8] c"%02x \00"
@.str.13 = [4 x i8] c"...\00"
@.str.14 = [16 x i8] c" sent %i of %i\0A\00"
@.str.15 = [20 x i8] c"Received %i bytes: \00"
@.str.16 = [2 x i8] c"\0A\00"
define void @doHCIComms(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [1024 x i8]
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %2
%12 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %3
%13 = load i32*, i32** %3
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %168
17: ; preds = %1
%18 = load i32, i32* %2
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* @FNDELAY
%21 = call i32 @fcntl(i32 %18, i32 %19, i32 %20)
store i32 1, i32* %11
br label %22
22: ; preds = %161, %53, %17
%23 = load i32*, i32** %3
%24 = call i32 @feof(i32* %23)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br i1 %26, label %27, label %165
27: ; preds = %22
%28 = load i32*, i32** %3
%29 = call i32 (i32*, i8*, ...) @fscanf(i32* %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64* %5, i64* %6, i64* %7)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 3
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* %11
%34 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %165
35: ; preds = %27
%36 = load i64, i64* %5
%37 = icmp eq i64 %36, 171
br i1 %37, label %38, label %56
38: ; preds = %35
%39 = load i64, i64* %6
%40 = icmp eq i64 %39, 171
br i1 %40, label %41, label %56
41: ; preds = %38
%42 = load i64, i64* %7
%43 = icmp eq i64 %42, 171
br i1 %43, label %44, label %56
44: ; preds = %41
%45 = load i32*, i32** %3
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%47 = call i32 (i32*, i8*, ...) @fscanf(i32* %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %46, i64* %5)
%48 = load i64, i64* %5
%49 = icmp ne i64 %48, 255
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i32, i32* %11
%52 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.6, i64 0, i64 0), i32 %51)
br label %165
53: ; preds = %44
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%55 = call i32 @loadFirmware(i8* %54)
br label %22
56: ; preds = %41, %38, %35
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8 1, i8* %57
%58 = load i64, i64* %5
%59 = trunc i64 %58 to i8
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 1
store i8 %59, i8* %60
%61 = load i64, i64* %6
%62 = trunc i64 %61 to i8
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 2
store i8 %62, i8* %63
%64 = load i64, i64* %7
%65 = trunc i64 %64 to i8
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 3
store i8 %65, i8* %66
%67 = load i64, i64* %7
%68 = trunc i64 %67 to i32
store i32 %68, i32* %10
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 4
%71 = icmp sgt i32 %70, 1024
br i1 %71, label %72, label %75
72: ; preds = %56
%73 = load i32, i32* %11
%74 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %72, %56
store i32 0, i32* %8
br label %76
76: ; preds = %96, %75
%77 = load i32, i32* %8
%78 = load i32, i32* %10
%79 = icmp slt i32 %77, %78
br i1 %79, label %80, label %99
80: ; preds = %76
%81 = load i32*, i32** %3
%82 = call i32 (i32*, i8*, ...) @fscanf(i32* %81, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i64* %5)
store i32 %82, i32* %9
%83 = load i64, i64* %5
%84 = trunc i64 %83 to i8
%85 = load i32, i32* %8
%86 = add nsw i32 4, %85
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %87
store i8 %84, i8* %88
%89 = load i32, i32* %9
%90 = icmp ne i32 %89, 1
br i1 %90, label %91, label %95
91: ; preds = %80
%92 = load i32, i32* %11
%93 = load i32, i32* %8
%94 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i32 %92, i32 %93)
br label %99
95: ; preds = %80
br label %96
96: ; preds = %95
%97 = load i32, i32* %8
%98 = add nsw i32 %97, 1
store i32 %98, i32* %8
br label %76
99: ; preds = %91, %76
%100 = load i32*, i32** %3
%101 = call i32 (i32*, i8*, ...) @fscanf(i32* %100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i64* %5)
store i32 %101, i32* %9
%102 = load i32, i32* %9
%103 = icmp ne i32 %102, 1
br i1 %103, label %107, label %104
104: ; preds = %99
%105 = load i64, i64* %5
%106 = icmp ne i64 %105, 255
br i1 %106, label %107, label %110
107: ; preds = %104, %99
%108 = load i32, i32* %11
%109 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.10, i64 0, i64 0), i32 %108)
br label %165
110: ; preds = %104
%111 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
store i32 0, i32* %8
br label %112
112: ; preds = %124, %110
%113 = load i32, i32* %8
%114 = load i32, i32* %10
%115 = add nsw i32 %114, 4
%116 = icmp slt i32 %113, %115
br i1 %116, label %117, label %127
117: ; preds = %112
%118 = load i32, i32* %8
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %119
%121 = load i8, i8* %120
%122 = zext i8 %121 to i32
%123 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i32 %122)
br label %124
124: ; preds = %117
%125 = load i32, i32* %8
%126 = add nsw i32 %125, 1
store i32 %126, i32* %8
br label %112
127: ; preds = %112
%128 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i64 0, i64 0))
%129 = load i32, i32* %2
%130 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%131 = load i32, i32* %10
%132 = add nsw i32 %131, 4
%133 = call i32 @write(i32 %129, i8* %130, i32 %132)
store i32 %133, i32* %9
%134 = load i32, i32* %9
%135 = load i32, i32* %10
%136 = add nsw i32 %135, 4
%137 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.14, i64 0, i64 0), i32 %134, i32 %136)
%138 = load i32, i32* %2
%139 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%140 = call i32 @readHCIResponse(i32 %138, i8* %139, i32 1024)
store i32 %140, i32* %9
%141 = load i32, i32* %9
%142 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.15, i64 0, i64 0), i32 %141)
%143 = load i32, i32* %9
%144 = icmp sgt i32 %143, 0
br i1 %144, label %145, label %161
145: ; preds = %127
store i32 0, i32* %8
br label %146
146: ; preds = %157, %145
%147 = load i32, i32* %8
%148 = load i32, i32* %9
%149 = icmp slt i32 %147, %148
br i1 %149, label %150, label %160
150: ; preds = %146
%151 = load i32, i32* %8
%152 = sext i32 %151 to i64
%153 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %152
%154 = load i8, i8* %153
%155 = zext i8 %154 to i32
%156 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i32 %155)
br label %157
157: ; preds = %150
%158 = load i32, i32* %8
%159 = add nsw i32 %158, 1
store i32 %159, i32* %8
br label %146
160: ; preds = %146
br label %161
161: ; preds = %160, %127
%162 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.16, i64 0, i64 0))
%163 = load i32, i32* %11
%164 = add nsw i32 %163, 1
store i32 %164, i32* %11
br label %22
165: ; preds = %107, %50, %32, %22
%166 = load i32*, i32** %3
%167 = call i32 @fclose(i32* %166)
br label %168
168: ; preds = %165, %15
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/packets.txt\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [25 x i8] c"Cannot open packets.txt\0A\00"
@F_SETFL = global i32 0
@FNDELAY = global i32 0
@.str.3 = [9 x i8] c"%x %x %x\00"
@.str.4 = [32 x i8] c"Line %i: Need at least 3 bytes\0A\00"
@.str.5 = [6 x i8] c"%s %x\00"
@.str.6 = [66 x i8] c"Line %i: Firmware read signature found but incorrect terminator.\0A\00"
@.str.7 = [26 x i8] c"Line %i: Packet too long\0A\00"
@.str.8 = [3 x i8] c"%x\00"
@.str.9 = [33 x i8] c"Line %i: Failed loading byte %i\0A\00"
@.str.10 = [49 x i8] c"Line %i: Didn't or couldn't get 0xFF terminator\0A\00"
@.str.11 = [10 x i8] c"Sending: \00"
@.str.12 = [6 x i8] c"%02x \00"
@.str.13 = [4 x i8] c"...\00"
@.str.14 = [16 x i8] c" sent %i of %i\0A\00"
@.str.15 = [20 x i8] c"Received %i bytes: \00"
@.str.16 = [2 x i8] c"\0A\00"
define void @doHCIComms(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = bitcast i64* %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = bitcast i64* %5 to i8*
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %120
14: ; preds = %1
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* @FNDELAY
%17 = tail call i32 @fcntl(i32 %0, i32 %15, i32 %16)
%18 = call i32 @feof(i32* %10)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %118
20: ; preds = %14
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 2
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 3
br label %24
24: ; preds = %20, %110
%25 = phi i32 [ 1, %20 ], [ %112, %110 ]
br label %26
26: ; preds = %24, %42
%27 = call i32 (i32*, i8*, ...) @fscanf(i32* %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64* %3, i64* %4, i64* %5)
%28 = icmp eq i32 %27, 3
br i1 %28, label %29, label %115
29: ; preds = %26
%30 = load i64, i64* %3
%31 = icmp eq i64 %30, 171
%32 = load i64, i64* %4
%33 = icmp eq i64 %32, 171
%34 = select i1 %31, i1 %33, i1 false
%35 = load i64, i64* %5
%36 = icmp eq i64 %35, 171
%37 = select i1 %34, i1 %36, i1 false
br i1 %37, label %38, label %46
38: ; preds = %29
%39 = call i32 (i32*, i8*, ...) @fscanf(i32* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %6, i64* %3)
%40 = load i64, i64* %3
%41 = icmp eq i64 %40, 255
br i1 %41, label %42, label %115
42: ; preds = %38
%43 = call i32 @loadFirmware(i8* %6)
%44 = call i32 @feof(i32* %10)
%45 = icmp eq i32 %44, 0
br i1 %45, label %26, label %118
46: ; preds = %29
store i8 1, i8* %6
%47 = trunc i64 %30 to i8
store i8 %47, i8* %21
%48 = trunc i64 %32 to i8
store i8 %48, i8* %22
%49 = trunc i64 %35 to i8
store i8 %49, i8* %23
%50 = trunc i64 %35 to i32
%51 = add i32 %50, 4
%52 = icmp sgt i32 %50, 1020
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %25)
br label %57
55: ; preds = %46
%56 = icmp sgt i32 %50, 0
br i1 %56, label %57, label %73
57: ; preds = %53, %55
%58 = and i64 %35, 4294967295
br label %59
59: ; preds = %57, %70
%60 = phi i64 [ 0, %57 ], [ %71, %70 ]
%61 = call i32 (i32*, i8*, ...) @fscanf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i64* %3)
%62 = load i64, i64* %3
%63 = trunc i64 %62 to i8
%64 = add nuw nsw i64 %60, 4
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %64
store i8 %63, i8* %65
%66 = icmp eq i32 %61, 1
br i1 %66, label %70, label %67
67: ; preds = %59
%68 = trunc i64 %60 to i32
%69 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i32 %25, i32 %68)
br label %73
70: ; preds = %59
%71 = add nuw nsw i64 %60, 1
%72 = icmp eq i64 %71, %58
br i1 %72, label %73, label %59
73: ; preds = %70, %55, %67
%74 = call i32 (i32*, i8*, ...) @fscanf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i64* %3)
%75 = icmp ne i32 %74, 1
%76 = load i64, i64* %3
%77 = icmp ne i64 %76, 255
%78 = select i1 %75, i1 true, i1 %77
br i1 %78, label %115, label %79
79: ; preds = %73
%80 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
%81 = icmp sgt i32 %50, -4
br i1 %81, label %82, label %93
82: ; preds = %79
%83 = call i32 @llvm.smax.i32(i32 %51, i32 1)
%84 = zext i32 %83 to i64
br label %85
85: ; preds = %82, %85
%86 = phi i64 [ 0, %82 ], [ %91, %85 ]
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %86
%88 = load i8, i8* %87
%89 = zext i8 %88 to i32
%90 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i32 %89)
%91 = add nuw nsw i64 %86, 1
%92 = icmp eq i64 %91, %84
br i1 %92, label %93, label %85
93: ; preds = %85, %79
%94 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i64 0, i64 0))
%95 = call i32 @write(i32 %0, i8* %6, i32 %51)
%96 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.14, i64 0, i64 0), i32 %95, i32 %51)
%97 = call i32 @readHCIResponse(i32 %0, i8* %6, i32 1024)
%98 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.15, i64 0, i64 0), i32 %97)
%99 = icmp sgt i32 %97, 0
br i1 %99, label %100, label %110
100: ; preds = %93
%101 = zext i32 %97 to i64
br label %102
102: ; preds = %100, %102
%103 = phi i64 [ 0, %100 ], [ %108, %102 ]
%104 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %103
%105 = load i8, i8* %104
%106 = zext i8 %105 to i32
%107 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i32 %106)
%108 = add nuw nsw i64 %103, 1
%109 = icmp eq i64 %108, %101
br i1 %109, label %110, label %102
110: ; preds = %102, %93
%111 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.16, i64 0, i64 0))
%112 = add nuw nsw i32 %25, 1
%113 = call i32 @feof(i32* %10)
%114 = icmp eq i32 %113, 0
br i1 %114, label %24, label %118
115: ; preds = %73, %38, %26
%116 = phi i8* [ getelementptr inbounds ([66 x i8], [66 x i8]* @.str.6, i64 0, i64 0), %38 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), %26 ], [ getelementptr inbounds ([49 x i8], [49 x i8]* @.str.10, i64 0, i64 0), %73 ]
%117 = call i32 (i32, i8*, ...) @output(i32 1, i8* %116, i32 %25)
br label %118
118: ; preds = %110, %42, %115, %14
%119 = call i32 @fclose(i32* %10)
br label %120
120: ; preds = %118, %12
ret void
}