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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int count_packets(int /*<<< orphan*/ *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned char,
                         int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *, unsigned char, unsigned short,
                          unsigned char, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char, unsigned char,
                            unsigned char, unsigned char);
int print_summary(FILE *fp) {
  unsigned char bytes[20];
  if (fread(bytes, sizeof(char), 20, fp) != 20) {
    perror("Failed to read ip header data from log file");
    return 1;
  }
  unsigned char ihl = bytes[0] & 0x0F;
  unsigned short int packetlen = (bytes[2] << 8) | bytes[3];
  char srcaddr[16];
  char destaddr[16];
  snprintf(srcaddr, 16, "%u.%u.%u.%u", bytes[12], bytes[13], bytes[14],
           bytes[15]);
  snprintf(destaddr, 16, "%u.%u.%u.%u", bytes[16], bytes[17], bytes[18],
           bytes[19]);
  fseek(fp, (ihl * 4) + 12, SEEK_SET);
  if (fread(bytes, sizeof(char), 1, fp) != 1) {
    perror("Failed to read tcp header data from log file");
    return 1;
  }
  unsigned char tcphl = (bytes[0] & 0xF0) >> 4;
  int npackets = count_packets(fp);
  if (npackets < 0) {
    return 1;
  }
  printf("%s %s %u %u %u %d\n", srcaddr, destaddr, ihl, packetlen, tcphl,
         npackets);
  return 0;
}


x86 assembly (clang, -O0):
<print_summary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x14,%edx
call   1030 <fread@plt>
cmp    $0x14,%eax
je     11a4 <print_summary+0x44>
lea    0xe6d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12de <print_summary+0x17e>
movzbl -0x30(%rbp),%eax
and    $0xf,%eax
mov    %al,-0x31(%rbp)
movzbl -0x2e(%rbp),%eax
shl    $0x8,%eax
movzbl -0x2d(%rbp),%ecx
or     %ecx,%eax
mov    %ax,-0x34(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x24(%rbp),%cl
mov    -0x23(%rbp),%r8b
mov    -0x22(%rbp),%al
mov    $0x10,%esi
lea    0xe53(%rip),%rdx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %al,%r9d
movzbl -0x21(%rbp),%eax
mov    %eax,(%rsp)
call   1050 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%cl
mov    -0x1f(%rbp),%r8b
mov    -0x1e(%rbp),%al
mov    $0x10,%esi
lea    0xe22(%rip),%rdx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %al,%r9d
movzbl -0x1d(%rbp),%eax
mov    %eax,(%rsp)
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rdi
movzbl -0x31(%rbp),%eax
shl    $0x2,%eax
add    $0xc,%eax
mov    0x2daa(%rip),%rcx
mov    (%rcx),%edx
movzbl %al,%esi
call   1060 <fseek@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1275 <print_summary+0x115>
lea    0xdd4(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12de <print_summary+0x17e>
movzbl -0x30(%rbp),%eax
and    $0xf0,%eax
sar    $0x4,%eax
mov    %al,-0x61(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <count_packets@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
jge    12a6 <print_summary+0x146>
movl   $0x1,-0x4(%rbp)
jmp    12de <print_summary+0x17e>
lea    -0x50(%rbp),%rsi
lea    -0x60(%rbp),%rdx
mov    -0x31(%rbp),%cl
mov    -0x34(%rbp),%r8w
mov    -0x61(%rbp),%r9b
mov    -0x68(%rbp),%eax
lea    0xda1(%rip),%rdi
movzbl %cl,%ecx
movzwl %r8w,%r8d
movzbl %r9b,%r9d
mov    %eax,(%rsp)
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_summary>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
mov    $0x1,%r12d
mov    $0x1,%esi
mov    $0x14,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
cmp    $0x14,%eax
jne    1291 <print_summary+0x131>
mov    0x10(%rsp),%r14b
and    $0xf,%r14b
movzwl 0x12(%rsp),%r15d
movzbl 0x1c(%rsp),%ecx
movzbl 0x1d(%rsp),%r8d
movzbl 0x1e(%rsp),%r9d
movzbl 0x1f(%rsp),%eax
mov    %eax,(%rsp)
lea    0xe68(%rip),%rbx
lea    0x40(%rsp),%rdi
mov    $0x10,%esi
mov    %rbx,%rdx
call   1050 <snprintf@plt>
movzbl 0x20(%rsp),%ecx
movzbl 0x21(%rsp),%r8d
movzbl 0x22(%rsp),%r9d
movzbl 0x23(%rsp),%eax
mov    %eax,(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x10,%esi
mov    %rbx,%rdx
call   1050 <snprintf@plt>
lea    0x0(,%r14,4),%eax
add    $0xc,%al
mov    0x2dce(%rip),%rcx
mov    (%rcx),%edx
movzbl %al,%esi
mov    %rbp,%rdi
call   1060 <fseek@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%r12d
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    129f <print_summary+0x13f>
mov    0x10(%rsp),%bl
mov    %rbp,%rdi
call   1070 <count_packets@plt>
test   %eax,%eax
js     1281 <print_summary+0x121>
rol    $0x8,%r15w
shr    $0x4,%bl
mov    %eax,(%rsp)
movzbl %r14b,%ecx
movzwl %r15w,%r8d
movzbl %bl,%r9d
lea    0xdf6(%rip),%rdi
lea    0x40(%rsp),%rsi
lea    0x30(%rsp),%rdx
call   1040 <printf@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd68(%rip),%rdi
call   1080 <perror@plt>
jmp    1281 <print_summary+0x121>
lea    0xd92(%rip),%rdi
call   1080 <perror@plt>
jmp    1281 <print_summary+0x121>

LLVM IR (-O0):
@.str = [44 x i8] c"Failed to read ip header data from log file\00"
@.str.1 = [12 x i8] c"%u.%u.%u.%u\00"
@SEEK_SET = global i32 0
@.str.2 = [45 x i8] c"Failed to read tcp header data from log file\00"
@.str.3 = [19 x i8] c"%s %s %u %u %u %d\0A\00"
define i32 @print_summary(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [20 x i8]
%5 = alloca i8
%6 = alloca i16
%7 = alloca [16 x i8]
%8 = alloca [16 x i8]
%9 = alloca i8
%10 = alloca i32
store i32* %0, i32** %3
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%12 = load i32*, i32** %3
%13 = call i32 @fread(i8* %11, i32 1, i32 20, i32* %12)
%14 = icmp ne i32 %13, 20
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %86
17: ; preds = %1
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = and i32 %20, 15
%22 = trunc i32 %21 to i8
store i8 %22, i8* %5
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 2
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = shl i32 %25, 8
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 3
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = or i32 %26, %29
%31 = trunc i32 %30 to i16
store i16 %31, i16* %6
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 12
%34 = load i8, i8* %33
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 13
%36 = load i8, i8* %35
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 14
%38 = load i8, i8* %37
%39 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 15
%40 = load i8, i8* %39
%41 = call i32 @snprintf(i8* %32, i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %34, i8 zeroext %36, i8 zeroext %38, i8 zeroext %40)
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%43 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 16
%44 = load i8, i8* %43
%45 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 17
%46 = load i8, i8* %45
%47 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 18
%48 = load i8, i8* %47
%49 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 19
%50 = load i8, i8* %49
%51 = call i32 @snprintf(i8* %42, i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %44, i8 zeroext %46, i8 zeroext %48, i8 zeroext %50)
%52 = load i32*, i32** %3
%53 = load i8, i8* %5
%54 = zext i8 %53 to i32
%55 = mul nsw i32 %54, 4
%56 = add nsw i32 %55, 12
%57 = trunc i32 %56 to i8
%58 = load i32, i32* @SEEK_SET
%59 = call i32 @fseek(i32* %52, i8 zeroext %57, i32 %58)
%60 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%61 = load i32*, i32** %3
%62 = call i32 @fread(i8* %60, i32 1, i32 1, i32* %61)
%63 = icmp ne i32 %62, 1
br i1 %63, label %64, label %66
64: ; preds = %17
%65 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %86
66: ; preds = %17
%67 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = and i32 %69, 240
%71 = ashr i32 %70, 4
%72 = trunc i32 %71 to i8
store i8 %72, i8* %9
%73 = load i32*, i32** %3
%74 = call i32 @count_packets(i32* %73)
store i32 %74, i32* %10
%75 = load i32, i32* %10
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %66
store i32 1, i32* %2
br label %86
78: ; preds = %66
%79 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%80 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%81 = load i8, i8* %5
%82 = load i16, i16* %6
%83 = load i8, i8* %9
%84 = load i32, i32* %10
%85 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %79, i8* %80, i8 zeroext %81, i16 zeroext %82, i8 zeroext %83, i32 %84)
store i32 0, i32* %2
br label %86
86: ; preds = %78, %77, %64, %15
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
@.str = [44 x i8] c"Failed to read ip header data from log file\00"
@.str.1 = [12 x i8] c"%u.%u.%u.%u\00"
@SEEK_SET = global i32 0
@.str.2 = [45 x i8] c"Failed to read tcp header data from log file\00"
@.str.3 = [19 x i8] c"%s %s %u %u %u %d\0A\00"
define i32 @print_summary(i32* %0) {
%2 = alloca [20 x i8]
%3 = alloca [16 x i8]
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%6 = call i32 @fread(i8* %5, i32 1, i32 20, i32* %0)
%7 = icmp eq i32 %6, 20
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %58
10: ; preds = %1
%11 = load i8, i8* %5
%12 = and i8 %11, 15
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 2
%14 = load i8, i8* %13
%15 = zext i8 %14 to i16
%16 = shl nuw i16 %15, 8
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 3
%18 = load i8, i8* %17
%19 = zext i8 %18 to i16
%20 = or i16 %16, %19
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 12
%24 = load i8, i8* %23
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 13
%26 = load i8, i8* %25
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 14
%28 = load i8, i8* %27
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 15
%30 = load i8, i8* %29
%31 = call i32 @snprintf(i8* %21, i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %24, i8 zeroext %26, i8 zeroext %28, i8 zeroext %30)
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 16
%33 = load i8, i8* %32
%34 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 17
%35 = load i8, i8* %34
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 18
%37 = load i8, i8* %36
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 19
%39 = load i8, i8* %38
%40 = call i32 @snprintf(i8* %22, i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %33, i8 zeroext %35, i8 zeroext %37, i8 zeroext %39)
%41 = shl nuw nsw i8 %12, 2
%42 = add nuw nsw i8 %41, 12
%43 = load i32, i32* @SEEK_SET
%44 = call i32 @fseek(i32* %0, i8 zeroext %42, i32 %43)
%45 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %0)
%46 = icmp eq i32 %45, 1
br i1 %46, label %49, label %47
47: ; preds = %10
%48 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %56
49: ; preds = %10
%50 = load i8, i8* %5
%51 = call i32 @count_packets(i32* %0)
%52 = icmp slt i32 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %49
%54 = lshr i8 %50, 4
%55 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %21, i8* %22, i8 zeroext %12, i16 zeroext %20, i8 zeroext %54, i32 %51)
br label %56
56: ; preds = %53, %49, %47
%57 = phi i32 [ 1, %47 ], [ 0, %53 ], [ 1, %49 ]
br label %58
58: ; preds = %56, %8
%59 = phi i32 [ 1, %8 ], [ %57, %56 ]
ret i32 %59
}