#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 uint32_t;

/* Variables and functions */
int atoi(char *);
int copy_bytes(int /*<<< orphan*/, int /*<<< orphan*/, int);
scalar_t__ feof(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int n;
  char buf[16];
  if (argc != 2) {
    fprintf(stderr, "usage: %s packets\n", argv[0]);
    return 1;
  }
  n = atoi(argv[1]);
  if (copy_bytes(stdout, stdin, 24) < 24)
    return 1;
  for (int i = 0; i < n; i++) {
    uint32_t packet_length;
    size_t r = fread(buf, 1, 16, stdin);
    if (r < 16) {
      if (feof(stdin) && r == 0)
        return 0;
      else
        return 1;
    }
    fwrite(buf, 1, 16, stdout);
    packet_length = *((uint32_t *)(buf + 8));
    if (copy_bytes(stdout, stdin, packet_length) < packet_length)
      return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a8 <main+0x48>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c6 <main+0x166>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    $0x18,%edx
call   1060 <copy_bytes@plt>
cmp    $0x18,%eax
jge    11e9 <main+0x89>
movl   $0x1,-0x4(%rbp)
jmp    12c6 <main+0x166>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12c6 <main+0x166>
lea    -0x30(%rbp),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x10,-0x40(%rbp)
jae    1262 <main+0x102>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1256 <main+0xf6>
cmpq   $0x0,-0x40(%rbp)
jne    1256 <main+0xf6>
movl   $0x0,-0x4(%rbp)
jmp    12c6 <main+0x166>
movl   $0x1,-0x4(%rbp)
jmp    12c6 <main+0x166>
lea    -0x30(%rbp),%rdi
mov    0x2d63(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x10,%edx
call   1080 <fwrite@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%edx
call   1060 <copy_bytes@plt>
cmp    -0x38(%rbp),%eax
jge    12b3 <main+0x153>
movl   $0x1,-0x4(%rbp)
jmp    12c6 <main+0x166>
jmp    12b8 <main+0x158>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11f0 <main+0x90>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1218 <main+0xb8>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r15d
mov    0x2e46(%rip),%r12
mov    (%r12),%edi
mov    0x2e53(%rip),%r13
mov    0x0(%r13),%esi
mov    $0x18,%edx
call   1060 <copy_bytes@plt>
mov    $0x1,%ebp
cmp    $0x18,%eax
jl     1235 <main+0xd5>
test   %r15d,%r15d
jle    1214 <main+0xb4>
mov    %rsp,%r14
nopl   0x0(%rax)
mov    0x0(%r13),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
cmp    $0xf,%rax
jbe    1246 <main+0xe6>
mov    (%r12),%ecx
mov    $0x1,%ebp
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
call   1080 <fwrite@plt>
mov    0x8(%rsp),%ebx
mov    (%r12),%edi
mov    0x0(%r13),%esi
mov    %ebx,%edx
call   1060 <copy_bytes@plt>
cmp    %ebx,%eax
jl     1235 <main+0xd5>
add    $0xffffffff,%r15d
jne    11c0 <main+0x60>
xor    %ebp,%ebp
jmp    1235 <main+0xd5>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdd5(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%rbp
mov    0x0(%r13),%edi
call   1050 <feof@plt>
test   %rax,%rax
sete   %al
test   %rbp,%rbp
setne  %cl
or     %al,%cl
movzbl %cl,%ebp
jmp    1235 <main+0xd5>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"usage: %s packets\0A\00"
@stdout = global i32 0
@stdin = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [16 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %68
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %6
%24 = load i32, i32* @stdout
%25 = load i32, i32* @stdin
%26 = call i32 @copy_bytes(i32 %24, i32 %25, i32 24)
%27 = icmp slt i32 %26, 24
br i1 %27, label %28, label %29
28: ; preds = %19
store i32 1, i32* %3
br label %68
29: ; preds = %19
store i32 0, i32* %8
br label %30
30: ; preds = %65, %29
%31 = load i32, i32* %8
%32 = load i32, i32* %6
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %68
34: ; preds = %30
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%36 = load i32, i32* @stdin
%37 = call i64 @fread(i8* %35, i32 1, i32 16, i32 %36)
store i64 %37, i64* %10
%38 = load i64, i64* %10
%39 = icmp ult i64 %38, 16
br i1 %39, label %40, label %49
40: ; preds = %34
%41 = load i32, i32* @stdin
%42 = call i64 @feof(i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load i64, i64* %10
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 0, i32* %3
br label %68
48: ; preds = %44, %40
store i32 1, i32* %3
br label %68
49: ; preds = %34
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%51 = load i32, i32* @stdout
%52 = call i32 @fwrite(i8* %50, i32 1, i32 16, i32 %51)
%53 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%54 = getelementptr inbounds i8, i8* %53, i64 8
%55 = bitcast i8* %54 to i32*
%56 = load i32, i32* %55
store i32 %56, i32* %9
%57 = load i32, i32* @stdout
%58 = load i32, i32* @stdin
%59 = load i32, i32* %9
%60 = call i32 @copy_bytes(i32 %57, i32 %58, i32 %59)
%61 = load i32, i32* %9
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %64
63: ; preds = %49
store i32 1, i32* %3
br label %68
64: ; preds = %49
br label %65
65: ; preds = %64
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %30
68: ; preds = %13, %28, %47, %48, %63, %30
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"usage: %s packets\0A\00"
@stdout = global i32 0
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %46
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = load i32, i32* @stdout
%15 = load i32, i32* @stdin
%16 = tail call i32 @copy_bytes(i32 %14, i32 %15, i32 24)
%17 = icmp slt i32 %16, 24
br i1 %17, label %46, label %18
18: ; preds = %10
%19 = icmp sgt i32 %13, 0
br i1 %19, label %20, label %46
20: ; preds = %18
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 8
%22 = bitcast i8* %21 to i32*
br label %25
23: ; preds = %37
%24 = icmp eq i32 %45, %13
br i1 %24, label %46, label %25
25: ; preds = %20, %23
%26 = phi i32 [ 0, %20 ], [ %45, %23 ]
%27 = load i32, i32* @stdin
%28 = call i64 @fread(i8* %4, i32 1, i32 16, i32 %27)
%29 = icmp ult i64 %28, 16
br i1 %29, label %30, label %37
30: ; preds = %25
%31 = load i32, i32* @stdin
%32 = call i64 @feof(i32 %31)
%33 = icmp eq i64 %32, 0
%34 = icmp ne i64 %28, 0
%35 = select i1 %33, i1 true, i1 %34
%36 = zext i1 %35 to i32
br label %46
37: ; preds = %25
%38 = load i32, i32* @stdout
%39 = call i32 @fwrite(i8* %4, i32 1, i32 16, i32 %38)
%40 = load i32, i32* %22
%41 = load i32, i32* @stdout
%42 = load i32, i32* @stdin
%43 = call i32 @copy_bytes(i32 %41, i32 %42, i32 %40)
%44 = icmp slt i32 %43, %40
%45 = add nuw nsw i32 %26, 1
br i1 %44, label %46, label %23
46: ; preds = %23, %37, %18, %30, %10, %6
%47 = phi i32 [ 1, %6 ], [ 1, %10 ], [ %36, %30 ], [ 0, %18 ], [ 0, %23 ], [ 1, %37 ]
ret i32 %47
}