#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*/ *);
unsigned short fread(unsigned char *, int, unsigned short,
                     int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned short fwrite(unsigned char *, int, unsigned short,
                      int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int extract_data(FILE *in, FILE *out) {
  long fileindex = 0L;
  unsigned char word[4];
  while ((fread(word, sizeof(char), 4, in)) == 4) {
    unsigned char ihl = (word[0] & 0x0F) * 4;
    unsigned short int packetlen = (word[2] << 8) | word[3];
    fseek(in, fileindex + ihl + 12, SEEK_SET);
    if (fread(word, sizeof(char), 1, in) != 1) {
      perror("Failed to read tcp header data from log file");
      return 2;
    }
    unsigned char tcphl = (word[0] & 0xF0) >> 2;
    fseek(in, fileindex + ihl + tcphl, SEEK_SET);
    unsigned short int datalen = packetlen - ihl - tcphl;
    unsigned char buf[datalen];
    if (fread(buf, sizeof(char), datalen, in) != datalen) {
      perror("Failed to read data from log file");
      return 3;
    }
    if (fwrite(buf, sizeof(char), datalen, out) != datalen) {
      perror("Failed to write data to output file");
      return 4;
    }
    fileindex += packetlen;
    fseek(in, fileindex, SEEK_SET);
  }
  if (!feof(in)) {
    perror("Failed to read ip header data from log file");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<extract_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
movzwl %ax,%eax
cmp    $0x4,%eax
jne    1341 <extract_data+0x1f1>
movzbl -0x24(%rbp),%eax
and    $0xf,%eax
shl    $0x2,%eax
mov    %al,-0x25(%rbp)
movzbl -0x22(%rbp),%eax
shl    $0x8,%eax
movzbl -0x21(%rbp),%ecx
or     %ecx,%eax
mov    %ax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movzbl -0x25(%rbp),%eax
add    %rax,%rsi
add    $0xc,%rsi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
lea    -0x24(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
movzwl %ax,%eax
cmp    $0x1,%eax
je     1202 <extract_data+0xb2>
lea    0xe0f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1372 <extract_data+0x222>
movzbl -0x24(%rbp),%eax
shr    $0x2,%eax
and    $0xfffffffc,%eax
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movzbl -0x25(%rbp),%eax
add    %rax,%rsi
movzbl -0x29(%rbp),%eax
add    %rax,%rsi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
movzwl -0x28(%rbp),%eax
movzbl -0x25(%rbp),%ecx
sub    %ecx,%eax
movzbl -0x29(%rbp),%ecx
sub    %ecx,%eax
mov    %ax,-0x2c(%rbp)
movzwl -0x2c(%rbp),%edx
mov    %edx,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
add    $0xf,%edx
mov    %edx,%ecx
and    $0xfffffff0,%ecx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%ax
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
movzwl %ax,%edx
call   1030 <fread@plt>
movzwl %ax,%eax
movzwl -0x2c(%rbp),%ecx
cmp    %ecx,%eax
je     12b0 <extract_data+0x160>
lea    0xd95(%rip),%rdi
call   1060 <perror@plt>
movl   $0x3,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1320 <extract_data+0x1d0>
mov    -0x50(%rbp),%rdi
mov    -0x2c(%rbp),%ax
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
movzwl %ax,%edx
call   1070 <fwrite@plt>
movzwl %ax,%eax
movzwl -0x2c(%rbp),%ecx
cmp    %ecx,%eax
je     12f7 <extract_data+0x1a7>
lea    0xd70(%rip),%rdi
call   1060 <perror@plt>
movl   $0x4,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1320 <extract_data+0x1d0>
movzwl -0x28(%rbp),%eax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2cce(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     133c <extract_data+0x1ec>
jmp    1337 <extract_data+0x1e7>
jmp    1372 <extract_data+0x222>
jmp    1168 <extract_data+0x18>
mov    -0x10(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
jne    136b <extract_data+0x21b>
lea    0xd19(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1372 <extract_data+0x222>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_data>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x40(%rbp)
mov    %rdi,%r14
lea    -0x2c(%rbp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x4,%ax
jne    12af <extract_data+0x15f>
xor    %r12d,%r12d
lea    -0x2c(%rbp),%rbx
nop
mov    %rbx,%r15
movzbl -0x2c(%rbp),%ebx
shl    $0x2,%ebx
and    $0x3c,%ebx
movzwl -0x2a(%rbp),%r13d
lea    (%r12,%rbx,1),%rsi
add    $0xc,%rsi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1050 <fseek@plt>
mov    %r15,%rdi
mov    %r14,%r15
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x1,%ax
jne    12e1 <extract_data+0x191>
shl    $0x30,%r13
bswap  %r13
mov    %rbx,%rsi
add    %r12,%rsi
movzbl -0x2c(%rbp),%r14d
shr    $0x2,%r14
and    $0x3c,%r14d
add    %r14,%rsi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
call   1050 <fseek@plt>
mov    %rsp,-0x38(%rbp)
lea    (%r14,%rbx,1),%eax
movzbl %al,%eax
mov    %r13d,%edx
sub    %eax,%edx
mov    %r15,%rcx
movzwl %dx,%r15d
lea    0xf(%r15),%eax
and    $0xfffffff0,%eax
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rcx,%rbx
call   1030 <fread@plt>
cmp    %r15w,%ax
jne    12f4 <extract_data+0x1a4>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    -0x40(%rbp),%rcx
call   1070 <fwrite@plt>
cmp    %r15w,%ax
jne    1302 <extract_data+0x1b2>
add    %r13,%r12
mov    0x2d69(%rip),%r14
mov    (%r14),%edx
mov    %rbx,%r14
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <fseek@plt>
mov    -0x38(%rbp),%rsp
lea    -0x2c(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x4,%ax
je     1190 <extract_data+0x40>
mov    %r14,%rdi
call   1040 <feof@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     12ce <extract_data+0x17e>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebx
jmp    12bd <extract_data+0x16d>
lea    0xd18(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%ebx
jmp    12bd <extract_data+0x16d>
mov    $0x3,%ebx
lea    0xd2d(%rip),%rdi
jmp    130e <extract_data+0x1be>
mov    $0x4,%ebx
lea    0xd41(%rip),%rdi
call   1060 <perror@plt>
mov    -0x38(%rbp),%rsp
jmp    12bd <extract_data+0x16d>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [45 x i8] c"Failed to read tcp header data from log file\00"
@.str.1 = [34 x i8] c"Failed to read data from log file\00"
@.str.2 = [36 x i8] c"Failed to write data to output file\00"
@.str.3 = [44 x i8] c"Failed to read ip header data from log file\00"
define i32 @extract_data(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca [4 x i8]
%8 = alloca i8
%9 = alloca i16
%10 = alloca i8
%11 = alloca i16
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 0, i64* %6
br label %15
15: ; preds = %113, %2
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call zeroext i16 @fread(i8* %16, i32 1, i16 zeroext 4, i32* %17)
%19 = zext i16 %18 to i32
%20 = icmp eq i32 %19, 4
br i1 %20, label %21, label %114
21: ; preds = %15
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = and i32 %24, 15
%26 = mul nsw i32 %25, 4
%27 = trunc i32 %26 to i8
store i8 %27, i8* %8
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 2
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = shl i32 %30, 8
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 3
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = or i32 %31, %34
%36 = trunc i32 %35 to i16
store i16 %36, i16* %9
%37 = load i32*, i32** %4
%38 = load i64, i64* %6
%39 = load i8, i8* %8
%40 = zext i8 %39 to i64
%41 = add nsw i64 %38, %40
%42 = add nsw i64 %41, 12
%43 = load i32, i32* @SEEK_SET
%44 = call i32 @fseek(i32* %37, i64 %42, i32 %43)
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%46 = load i32*, i32** %4
%47 = call zeroext i16 @fread(i8* %45, i32 1, i16 zeroext 1, i32* %46)
%48 = zext i16 %47 to i32
%49 = icmp ne i32 %48, 1
br i1 %49, label %50, label %52
50: ; preds = %21
%51 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %3
br label %121
52: ; preds = %21
%53 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = and i32 %55, 240
%57 = ashr i32 %56, 2
%58 = trunc i32 %57 to i8
store i8 %58, i8* %10
%59 = load i32*, i32** %4
%60 = load i64, i64* %6
%61 = load i8, i8* %8
%62 = zext i8 %61 to i64
%63 = add nsw i64 %60, %62
%64 = load i8, i8* %10
%65 = zext i8 %64 to i64
%66 = add nsw i64 %63, %65
%67 = load i32, i32* @SEEK_SET
%68 = call i32 @fseek(i32* %59, i64 %66, i32 %67)
%69 = load i16, i16* %9
%70 = zext i16 %69 to i32
%71 = load i8, i8* %8
%72 = zext i8 %71 to i32
%73 = sub nsw i32 %70, %72
%74 = load i8, i8* %10
%75 = zext i8 %74 to i32
%76 = sub nsw i32 %73, %75
%77 = trunc i32 %76 to i16
store i16 %77, i16* %11
%78 = load i16, i16* %11
%79 = zext i16 %78 to i64
%80 = call i8* @llvm.stacksave()
store i8* %80, i8** %12
%81 = alloca i8, i64 %79
store i64 %79, i64* %13
%82 = load i16, i16* %11
%83 = load i32*, i32** %4
%84 = call zeroext i16 @fread(i8* %81, i32 1, i16 zeroext %82, i32* %83)
%85 = zext i16 %84 to i32
%86 = load i16, i16* %11
%87 = zext i16 %86 to i32
%88 = icmp ne i32 %85, %87
br i1 %88, label %89, label %91
89: ; preds = %52
%90 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %3
store i32 1, i32* %14
br label %110
91: ; preds = %52
%92 = load i16, i16* %11
%93 = load i32*, i32** %5
%94 = call zeroext i16 @fwrite(i8* %81, i32 1, i16 zeroext %92, i32* %93)
%95 = zext i16 %94 to i32
%96 = load i16, i16* %11
%97 = zext i16 %96 to i32
%98 = icmp ne i32 %95, %97
br i1 %98, label %99, label %101
99: ; preds = %91
%100 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 4, i32* %3
store i32 1, i32* %14
br label %110
101: ; preds = %91
%102 = load i16, i16* %9
%103 = zext i16 %102 to i64
%104 = load i64, i64* %6
%105 = add nsw i64 %104, %103
store i64 %105, i64* %6
%106 = load i32*, i32** %4
%107 = load i64, i64* %6
%108 = load i32, i32* @SEEK_SET
%109 = call i32 @fseek(i32* %106, i64 %107, i32 %108)
store i32 0, i32* %14
br label %110
110: ; preds = %101, %99, %89
%111 = load i8*, i8** %12
%112 = load i32, i32* %14
switch i32 %112, label %123 [
i32 0, label %113
i32 1, label %121
]
113: ; preds = %110
br label %15
114: ; preds = %15
%115 = load i32*, i32** %4
%116 = call i32 @feof(i32* %115)
%117 = icmp ne i32 %116, 0
br i1 %117, label %120, label %118
118: ; preds = %114
%119 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %121
120: ; preds = %114
store i32 0, i32* %3
br label %121
121: ; preds = %120, %118, %110, %50
%122 = load i32, i32* %3
ret i32 %122
123: ; preds = %110
unreachable
}

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