#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*/ assert(int);
int /*<<< orphan*/ parse_packet(unsigned char *);
int /*<<< orphan*/ printf(char *, ...);
int read_byte(int);
int dump_packets(int file, int length) {
  unsigned char packet[6];
  int i;
  int received;
  assert(length <= 6);
  while (1) {
    printf("packet:");
    for (i = 0; i < length; i++) {
      received = read_byte(file);
      if (received < 0) {
        return received;
      }
      packet[i] = received;
      printf(" %02x", received);
    }
    if (length == 6) {
      parse_packet(packet);
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<dump_packets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x6,-0x8(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11ca <dump_packets+0x8a>
mov    -0x4(%rbp),%edi
call   1030 <read_byte@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    119e <dump_packets+0x5e>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x18(%rbp),%eax
mov    %al,%cl
movslq -0x14(%rbp),%rax
mov    %cl,-0xe(%rbp,%rax,1)
mov    -0x18(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1174 <dump_packets+0x34>
cmpl   $0x6,-0x8(%rbp)
jne    11dd <dump_packets+0x9d>
lea    -0xe(%rbp),%rdi
call   1060 <parse_packet@plt>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    115f <dump_packets+0x1f>

x86 assembly (clang, -O2):
<dump_packets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%ebp
xor    %edi,%edi
cmp    $0x7,%esi
setl   %dil
call   1060 <assert@plt>
mov    %r15d,%ebx
lea    0xe88(%rip),%r14
lea    0xe89(%rip),%r13
jmp    119a <dump_packets+0x4a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r15d,%r15d
jle    11d5 <dump_packets+0x85>
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %ebp,%edi
call   1040 <read_byte@plt>
test   %eax,%eax
js     11e7 <dump_packets+0x97>
mov    %al,0x2(%rsp,%r12,1)
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r12
cmp    %r12,%rbx
jne    11b0 <dump_packets+0x60>
cmp    $0x6,%r15d
jne    1190 <dump_packets+0x40>
lea    0x2(%rsp),%rdi
call   1070 <parse_packet@plt>
jmp    1190 <dump_packets+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"packet:\00"
@.str.1 = [6 x i8] c" %02x\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @dump_packets(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [6 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = icmp sle i32 %8, 6
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
br label %12
12: ; preds = %2, %42
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %14
14: ; preds = %33, %12
%15 = load i32, i32* %6
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %36
18: ; preds = %14
%19 = load i32, i32* %3
%20 = call i32 @read_byte(i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i32, i32* %7
ret i32 %24
25: ; preds = %18
%26 = load i32, i32* %7
%27 = trunc i32 %26 to i8
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 %29
store i8 %27, i8* %30
%31 = load i32, i32* %7
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %25
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %14
36: ; preds = %14
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 6
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%41 = call i32 @parse_packet(i8* %40)
br label %42
42: ; preds = %39, %36
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %12
}

LLVM IR (-O2):
@.str = [8 x i8] c"packet:\00"
@.str.1 = [6 x i8] c" %02x\00"
define i32 @dump_packets(i32 %0, i32 %1) {
%3 = alloca [6 x i8]
%4 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %1, 7
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp sgt i32 %1, 0
%9 = icmp eq i32 %1, 6
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %27, %2
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br i1 %8, label %13, label %24
13: ; preds = %11, %18
%14 = phi i64 [ %22, %18 ], [ 0, %11 ]
%15 = call i32 @read_byte(i32 %0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
ret i32 %15
18: ; preds = %13
%19 = trunc i32 %15 to i8
%20 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 %14
store i8 %19, i8* %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%22 = add nuw nsw i64 %14, 1
%23 = icmp eq i64 %22, %10
br i1 %23, label %24, label %13
24: ; preds = %18, %11
br i1 %9, label %25, label %27
25: ; preds = %24
%26 = call i32 @parse_packet(i8* %4)
br label %27
27: ; preds = %25, %24
%28 = call i32 @putchar(i32 10)
br label %11
}