#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 /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int count_packets(FILE *fp) {
  int count = 0;
  long fileindex = 2L;
  unsigned char bytes[2];
  fseek(fp, fileindex, SEEK_SET);
  while (fread(bytes, sizeof(char), 2, fp) == 2) {
    count++;
    fileindex += (bytes[0] << 8) | bytes[1];
    fseek(fp, fileindex, SEEK_SET);
  }
  if (!feof(fp)) {
    perror("Failed to reach end of log file");
    return -1;
  }
  return count;
}


x86 assembly (clang, -O0):
<count_packets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x2,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
lea    -0x22(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
cmp    $0x2,%eax
jne    11cc <count_packets+0x8c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movzbl -0x22(%rbp),%eax
shl    $0x8,%eax
movzbl -0x21(%rbp),%ecx
or     %ecx,%eax
cltq
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
jmp    1171 <count_packets+0x31>
mov    -0x10(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
jne    11f6 <count_packets+0xb6>
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <count_packets+0xbc>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_packets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e8a(%rip),%r12
mov    (%r12),%edx
mov    $0x2,%ebx
mov    $0x2,%esi
call   1050 <fseek@plt>
lea    0xe(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
xor    %ebp,%ebp
cmp    $0x2,%eax
jne    11cb <count_packets+0x8b>
xor    %ebp,%ebp
lea    0xe(%rsp),%r15
xchg   %ax,%ax
add    $0x1,%ebp
movzwl 0xe(%rsp),%eax
shl    $0x30,%rax
bswap  %rax
add    %rax,%rbx
mov    (%r12),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x2,%eax
je     1190 <count_packets+0x50>
mov    %r14,%rdi
call   1040 <feof@plt>
test   %eax,%eax
je     11e6 <count_packets+0xa6>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11d7 <count_packets+0x97>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [32 x i8] c"Failed to reach end of log file\00"
define i32 @count_packets(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca [2 x i8]
store i32* %0, i32** %3
store i32 0, i32* %4
store i64 2, i64* %5
%7 = load i32*, i32** %3
%8 = load i64, i64* %5
%9 = load i32, i32* @SEEK_SET
%10 = call i32 @fseek(i32* %7, i64 %8, i32 %9)
br label %11
11: ; preds = %16, %1
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%13 = load i32*, i32** %3
%14 = call i32 @fread(i8* %12, i32 1, i32 2, i32* %13)
%15 = icmp eq i32 %14, 2
br i1 %15, label %16, label %34
16: ; preds = %11
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 1
store i32 %18, i32* %4
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = shl i32 %21, 8
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 1
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = or i32 %22, %25
%27 = sext i32 %26 to i64
%28 = load i64, i64* %5
%29 = add nsw i64 %28, %27
store i64 %29, i64* %5
%30 = load i32*, i32** %3
%31 = load i64, i64* %5
%32 = load i32, i32* @SEEK_SET
%33 = call i32 @fseek(i32* %30, i64 %31, i32 %32)
br label %11
34: ; preds = %11
%35 = load i32*, i32** %3
%36 = call i32 @feof(i32* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %42
40: ; preds = %34
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %38
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [32 x i8] c"Failed to reach end of log file\00"
define i32 @count_packets(i32* nocapture %0) {
%2 = alloca [2 x i8]
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @fseek(i32* %0, i64 2, i32 %4)
%6 = call i32 @fread(i8* %3, i32 1, i32 2, i32* %0)
%7 = icmp eq i32 %6, 2
br i1 %7, label %8, label %25
8: ; preds = %1
%9 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 2, %8 ], [ %20, %10 ]
%12 = phi i32 [ 0, %8 ], [ %13, %10 ]
%13 = add nuw nsw i32 %12, 1
%14 = load i8, i8* %3
%15 = zext i8 %14 to i64
%16 = shl nuw nsw i64 %15, 8
%17 = load i8, i8* %9
%18 = zext i8 %17 to i64
%19 = or i64 %16, %18
%20 = add nuw nsw i64 %19, %11
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @fseek(i32* %0, i64 %20, i32 %21)
%23 = call i32 @fread(i8* %3, i32 1, i32 2, i32* %0)
%24 = icmp eq i32 %23, 2
br i1 %24, label %10, label %25
25: ; preds = %10, %1
%26 = phi i32 [ 0, %1 ], [ %13, %10 ]
%27 = tail call i32 @feof(i32* %0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %25, %29
%32 = phi i32 [ -1, %29 ], [ %26, %25 ]
ret i32 %32
}