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

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ send_req(int, int, int, int);
int /*<<< orphan*/ stderr;
int u_load_pkt(int fd, char **argv) {
  FILE *fp;
  uint8_t byte;
  int addr = 8;
  fp = fopen(argv[0], "r");
  if (!fp) {
    perror("cannot open packet file");
    return 1;
  }
  while (!feof(fp)) {
    if (fscanf(fp, "%hhx", &byte) < 1)
      break;
    fprintf(stderr, "\rLoading... %d", addr - 8);
    send_req(fd, 0x00, 0x80 | (byte << 8) | (addr++ << 16), 1);
    send_req(fd, 0x00, 0, 1);
  }
  fprintf(stderr, "\n");
  send_req(fd, 0x04, addr - 8, 1);
  return 0;
}


x86 assembly (clang, -O0):
<u_load_pkt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x8,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b0 <u_load_pkt+0x50>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1289 <u_load_pkt+0x129>
jmp    11b5 <u_load_pkt+0x55>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d3 <u_load_pkt+0x73>
jmp    1253 <u_load_pkt+0xf3>
mov    -0x18(%rbp),%rdi
lea    0xe3c(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jge    11f5 <u_load_pkt+0x95>
jmp    1253 <u_load_pkt+0xf3>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
sub    $0x8,%edx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%edx
shl    $0x8,%edx
or     $0x80,%edx
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x20(%rbp)
shl    $0x10,%eax
or     %eax,%edx
xor    %esi,%esi
mov    $0x1,%ecx
call   1030 <send_req@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    $0x1,%ecx
mov    %edx,%esi
call   1030 <send_req@plt>
jmp    11b5 <u_load_pkt+0x55>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%edx
sub    $0x8,%edx
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <send_req@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u_load_pkt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    (%rsi),%rdi
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1273 <u_load_pkt+0x113>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <feof@plt>
mov    $0x8,%ebx
test   %eax,%eax
jne    1232 <u_load_pkt+0xd2>
mov    $0x8,%ebx
mov    $0x80000,%r15d
lea    0x4(%rsp),%r12
lea    0xe6a(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
lea    0xe50(%rip),%rsi
mov    %r12,%rdx
call   1040 <fscanf@plt>
test   %eax,%eax
jle    1232 <u_load_pkt+0xd2>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbx),%edx
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x4(%rsp),%edx
shl    $0x8,%edx
add    $0x1,%ebx
or     %r15d,%edx
or     $0x80,%edx
mov    %r14d,%edi
xor    %esi,%esi
mov    $0x1,%ecx
call   1030 <send_req@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
mov    $0x1,%ecx
call   1030 <send_req@plt>
mov    %rbp,%rdi
call   1060 <feof@plt>
add    $0x10000,%r15d
test   %eax,%eax
je     11c0 <u_load_pkt+0x60>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0xfffffff8,%ebx
mov    %r14d,%edi
mov    $0x4,%esi
mov    %ebx,%edx
mov    $0x1,%ecx
call   1030 <send_req@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd88(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    1262 <u_load_pkt+0x102>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"cannot open packet file\00"
@.str.2 = [5 x i8] c"%hhx\00"
@stderr = global i32 0
@.str.3 = [15 x i8] c"\0DLoading... %d\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @u_load_pkt(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 8, i32* %8
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 0
%11 = load i8*, i8** %10
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %51
17: ; preds = %2
br label %18
18: ; preds = %28, %17
%19 = load i32*, i32** %6
%20 = call i32 @feof(i32* %19)
%21 = icmp ne i32 %20, 0
%22 = xor i1 %21, true
br i1 %22, label %23, label %44
23: ; preds = %18
%24 = load i32*, i32** %6
%25 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %23
br label %44
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i32, i32* %8
%31 = sub nsw i32 %30, 8
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %4
%34 = load i32, i32* %7
%35 = shl i32 %34, 8
%36 = or i32 128, %35
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
%39 = shl i32 %37, 16
%40 = or i32 %36, %39
%41 = call i32 @send_req(i32 %33, i32 0, i32 %40, i32 1)
%42 = load i32, i32* %4
%43 = call i32 @send_req(i32 %42, i32 0, i32 0, i32 1)
br label %18
44: ; preds = %27, %18
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* %4
%48 = load i32, i32* %8
%49 = sub nsw i32 %48, 8
%50 = call i32 @send_req(i32 %47, i32 4, i32 %49, i32 1)
store i32 0, i32* %3
br label %51
51: ; preds = %44, %15
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"cannot open packet file\00"
@.str.2 = [5 x i8] c"%hhx\00"
@stderr = global i32 0
@.str.3 = [15 x i8] c"\0DLoading... %d\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @u_load_pkt(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i8*, i8** %1
%6 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = call i32 @feof(i32* %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %31
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %37
13: ; preds = %8, %17
%14 = phi i32 [ %23, %17 ], [ 8, %8 ]
%15 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%16 = icmp slt i32 %15, 1
br i1 %16, label %31, label %17
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = add nsw i32 %14, -8
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %3
%22 = shl i32 %21, 8
%23 = add nuw nsw i32 %14, 1
%24 = shl i32 %14, 16
%25 = or i32 %24, %22
%26 = or i32 %25, 128
%27 = call i32 @send_req(i32 %0, i32 0, i32 %26, i32 1)
%28 = call i32 @send_req(i32 %0, i32 0, i32 0, i32 1)
%29 = call i32 @feof(i32* %6)
%30 = icmp eq i32 %29, 0
br i1 %30, label %13, label %31
31: ; preds = %17, %13, %8
%32 = phi i32 [ 8, %8 ], [ %14, %13 ], [ %23, %17 ]
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%35 = add nsw i32 %32, -8
%36 = call i32 @send_req(i32 %0, i32 4, i32 %35, i32 1)
br label %37
37: ; preds = %31, %11
%38 = phi i32 [ 0, %31 ], [ 1, %11 ]
ret i32 %38
}