#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 */
double bcdfour(unsigned char *);
int /*<<< orphan*/ buildpacket(unsigned char *, int *, int,
                               int /*<<< orphan*/ *, int /*<<< orphan*/);
int listen(int, unsigned char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ write(int, unsigned char *, int);
int checkrom(int fd) {
  unsigned char pak[1024];
  unsigned char buf[1024];
  int len, res;
  buildpacket(pak, &len, 0x19, NULL, 0);
  write(fd, pak, len);
  res = listen(fd, buf);
  if (res > 1) {
    printf("Connected to CPU, firmware version %.2f.\n", bcdfour(&buf[4]));
    return 1;
  } else if (res == -2)
    printf("Bad checksum.\n");
  else
    printf("No response from CPU.  Check connection?\n");
  return 0;
}


x86 assembly (clang, -O0):
<checkrom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
lea    -0x814(%rbp),%rsi
mov    $0x19,%edx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1050 <buildpacket@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x814(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
call   1060 <listen@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x1,-0x818(%rbp)
jle    11de <checkrom+0x8e>
lea    -0x810(%rbp),%rdi
add    $0x4,%rdi
call   1070 <bcdfour@plt>
lea    0xe35(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1218 <checkrom+0xc8>
cmpl   $0xfffffffe,-0x818(%rbp)
jne    11fe <checkrom+0xae>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120c <checkrom+0xbc>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1211 <checkrom+0xc1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkrom>:
push   %r14
push   %rbx
sub    $0x818,%rsp
mov    %edi,%ebx
lea    0x410(%rsp),%r14
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
mov    $0x19,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <buildpacket@plt>
mov    0xc(%rsp),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <write@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1070 <listen@plt>
cmp    $0x2,%eax
jl     11c9 <checkrom+0x69>
lea    0x14(%rsp),%rdi
call   1080 <bcdfour@plt>
lea    0xe45(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    11e5 <checkrom+0x85>
cmp    $0xfffffffe,%eax
jne    11d7 <checkrom+0x77>
lea    0xe7e(%rip),%rdi
jmp    11de <checkrom+0x7e>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Connected to CPU, firmware version %.2f.\0A\00"
@.str.1 = [15 x i8] c"Bad checksum.\0A\00"
@.str.2 = [42 x i8] c"No response from CPU. Check connection?\0A\00"
define i32 @checkrom(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i32 @buildpacket(i8* %8, i32* %6, i32 25, i32* null, i32 0)
%10 = load i32, i32* %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* %6
%13 = call i32 @write(i32 %10, i8* %11, i32 %12)
%14 = load i32, i32* %3
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = call i32 @listen(i32 %14, i8* %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %23
19: ; preds = %1
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 4
%21 = call double @bcdfour(i8* %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), double %21)
store i32 1, i32* %2
br label %32
23: ; preds = %1
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, -2
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %30
28: ; preds = %23
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
br label %31
31: ; preds = %30
store i32 0, i32* %2
br label %32
32: ; preds = %31, %19
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [42 x i8] c"Connected to CPU, firmware version %.2f.\0A\00"
@str = [41 x i8] c"No response from CPU. Check connection?\00"
@str.3 = [14 x i8] c"Bad checksum.\00"
define i32 @checkrom(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = call i32 @buildpacket(i8* %5, i32* %4, i32 25, i32* null, i32 0)
%9 = load i32, i32* %4
%10 = call i32 @write(i32 %0, i8* %5, i32 %9)
%11 = call i32 @listen(i32 %0, i8* %6)
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 4
%15 = call double @bcdfour(i8* %14)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), double %15)
br label %23
17: ; preds = %1
%18 = icmp eq i32 %11, -2
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.3, i64 0, i64 0))
br label %23
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %21, %19, %13
%24 = phi i32 [ 1, %13 ], [ 0, %19 ], [ 0, %21 ]
ret i32 %24
}