#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ lseek(int /*<<< orphan*/, unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned int);
scalar_t__ read(int /*<<< orphan*/, unsigned char *, int);
int main() {
  ssize_t nbytes;
  unsigned char buf[2];
  unsigned int packet_length;
  unsigned int packet_counter = 0;
  unsigned int minimum = 20000;
  unsigned int maximum = 0;
  unsigned int total_size = 0;
  do {
    nbytes = read(STDIN_FILENO, buf, 2);
    packet_length = ((int)buf[0] << 8) | buf[1];
    total_size += packet_length;
    minimum = packet_length < minimum ? packet_length : minimum;
    maximum = packet_length > maximum ? packet_length : maximum;
    ++packet_counter;
    lseek(STDIN_FILENO, packet_length, SEEK_CUR);
  } while (nbytes != 0);
  printf("number of packets: %u\n", packet_counter);
  printf("total data (bytes): %u\n", total_size);
  printf("smallest packet (bytes): %u\n", minimum);
  printf("avg packet size (bytes): %u\n", total_size / packet_counter);
  printf("largest packet (bytes): %u\n", maximum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x4e20,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
mov    %rax,-0x10(%rbp)
movzbl -0x12(%rbp),%eax
shl    $0x8,%eax
movzbl -0x11(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    11a6 <main+0x76>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11ac <main+0x7c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
jbe    11c9 <main+0x99>
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    11cf <main+0x9f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmpq   $0x0,-0x10(%rbp)
jne    115b <main+0x2b>
mov    -0x1c(%rbp),%esi
lea    0xdf3(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%esi
lea    0xdfb(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xe04(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
xor    %edx,%edx
divl   -0x1c(%rbp)
mov    %eax,%esi
lea    0xe0b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe19(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x4e20,%ebp
xor    %r14d,%r14d
mov    0x2e8e(%rip),%r12
xor    %ebx,%ebx
xor    %r15d,%r15d
nop
mov    (%r12),%edi
lea    0x6(%rsp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
movzwl 0x6(%rsp),%esi
shl    $0x10,%esi
bswap  %esi
add    %esi,%r15d
cmp    %ebp,%esi
cmovb  %esi,%ebp
cmp    %ebx,%esi
cmova  %esi,%ebx
mov    %rax,%r13
add    $0x1,%r14d
mov    (%r12),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
test   %r13,%r13
jne    1150 <main+0x20>
lea    0xe61(%rip),%rdi
mov    %r14d,%esi
call   1030 <printf@plt>
lea    0xe69(%rip),%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
lea    0xe72(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %r15d,%eax
xor    %edx,%edx
div    %r14d
lea    0xe79(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
lea    0xe88(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@SEEK_CUR = global i32 0
@.str = [23 x i8] c"number of packets: %u\0A\00"
@.str.1 = [24 x i8] c"total data (bytes): %u\0A\00"
@.str.2 = [29 x i8] c"smallest packet (bytes): %u\0A\00"
@.str.3 = [29 x i8] c"avg packet size (bytes): %u\0A\00"
@.str.4 = [28 x i8] c"largest packet (bytes): %u\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
store i32 20000, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %48, %0
%10 = load i32, i32* @STDIN_FILENO
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%12 = call i64 @read(i32 %10, i8* %11, i32 2)
store i64 %12, i64* %2
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = shl i32 %15, 8
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = or i32 %16, %19
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = load i32, i32* %8
%23 = add i32 %22, %21
store i32 %23, i32* %8
%24 = load i32, i32* %4
%25 = load i32, i32* %6
%26 = icmp ult i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %9
%28 = load i32, i32* %4
br label %31
29: ; preds = %9
%30 = load i32, i32* %6
br label %31
31: ; preds = %29, %27
%32 = phi i32 [ %28, %27 ], [ %30, %29 ]
store i32 %32, i32* %6
%33 = load i32, i32* %4
%34 = load i32, i32* %7
%35 = icmp ugt i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = load i32, i32* %4
br label %40
38: ; preds = %31
%39 = load i32, i32* %7
br label %40
40: ; preds = %38, %36
%41 = phi i32 [ %37, %36 ], [ %39, %38 ]
store i32 %41, i32* %7
%42 = load i32, i32* %5
%43 = add i32 %42, 1
store i32 %43, i32* %5
%44 = load i32, i32* @STDIN_FILENO
%45 = load i32, i32* %4
%46 = load i32, i32* @SEEK_CUR
%47 = call i32 @lseek(i32 %44, i32 %45, i32 %46)
br label %48
48: ; preds = %40
%49 = load i64, i64* %2
%50 = icmp ne i64 %49, 0
br i1 %50, label %9, label %51
51: ; preds = %48
%52 = load i32, i32* %5
%53 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %8
%55 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %6
%57 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %56)
%58 = load i32, i32* %8
%59 = load i32, i32* %5
%60 = udiv i32 %58, %59
%61 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %7
%63 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %62)
ret i32 0
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@SEEK_CUR = global i32 0
@.str = [23 x i8] c"number of packets: %u\0A\00"
@.str.1 = [24 x i8] c"total data (bytes): %u\0A\00"
@.str.2 = [29 x i8] c"smallest packet (bytes): %u\0A\00"
@.str.3 = [29 x i8] c"avg packet size (bytes): %u\0A\00"
@.str.4 = [28 x i8] c"largest packet (bytes): %u\0A\00"
define i32 @main() {
%1 = alloca [2 x i8]
%2 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
br label %4
4: ; preds = %4, %0
%5 = phi i32 [ 0, %0 ], [ %22, %4 ]
%6 = phi i32 [ 20000, %0 ], [ %19, %4 ]
%7 = phi i32 [ 0, %0 ], [ %21, %4 ]
%8 = phi i32 [ 0, %0 ], [ %17, %4 ]
%9 = load i32, i32* @STDIN_FILENO
%10 = call i64 @read(i32 %9, i8* %2, i32 2)
%11 = load i8, i8* %2
%12 = zext i8 %11 to i32
%13 = shl nuw nsw i32 %12, 8
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = or i32 %13, %15
%17 = add i32 %16, %8
%18 = icmp ult i32 %16, %6
%19 = select i1 %18, i32 %16, i32 %6
%20 = icmp ugt i32 %16, %7
%21 = select i1 %20, i32 %16, i32 %7
%22 = add i32 %5, 1
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @SEEK_CUR
%25 = tail call i32 @lseek(i32 %23, i32 %16, i32 %24)
%26 = icmp eq i64 %10, 0
br i1 %26, label %27, label %4
27: ; preds = %4
%28 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %22)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%30 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%31 = udiv i32 %17, %22
%32 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %21)
ret i32 0
}