#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*/ valint;
typedef void uint8_t;
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned short crc16_data(unsigned char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(void const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(void *, unsigned short *, int);
void pcap_write(void *handle, const uint8_t *buf, int buflen) {
  struct timeval tv;
  int valint;
  unsigned short crc;
  if (!handle)
    return;
  crc = crc16_data((const unsigned char *)buf, buflen, 0);
  memcpy((void *)&buf[buflen], &crc, 2);
  buflen += 2;
  gettimeofday(&tv, NULL);
  valint = (int)tv.tv_sec;
  fwrite((void *)&valint, sizeof(valint), 1, (FILE *)handle);
  ;
  valint = (int)tv.tv_usec;
  fwrite((void *)&valint, sizeof(valint), 1, (FILE *)handle);
  ;
  valint = (int)buflen;
  fwrite((void *)&valint, sizeof(valint), 1, (FILE *)handle);
  ;
  valint = (int)buflen;
  fwrite((void *)&valint, sizeof(valint), 1, (FILE *)handle);
  ;
  fwrite(buf, buflen, 1, (FILE *)handle);
  fflush((FILE *)handle);
}


x86 assembly (clang, -O0):
<pcap_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1173 <pcap_write+0x23>
jmp    1248 <pcap_write+0xf8>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1040 <crc16_data@plt>
mov    %ax,-0x2e(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
lea    -0x2e(%rbp),%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x28(%rbp),%rax
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fflush@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pcap_write>:
test   %rdi,%rdi
je     1234 <pcap_write+0xe4>
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    %edx,%esi
xor    %edx,%edx
call   1040 <crc16_data@plt>
mov    %ax,0x6(%rsp)
movslq %ebp,%r15
lea    (%r14,%r15,1),%rdi
lea    0x6(%rsp),%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
add    $0x2,%r15d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x10(%rsp),%eax
mov    %eax,(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%rsp)
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %r15d,(%rsp)
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %r15d,(%rsp)
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %r14,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fflush@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define void @pcap_write(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i16
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
br label %57
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = call zeroext i16 @crc16_data(i8* %14, i32 %15, i32 0)
store i16 %16, i16* %9
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @memcpy(i8* %20, i16* %9, i32 2)
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 2
store i32 %23, i32* %6
%24 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i32
store i32 %27, i32* %8
%28 = bitcast i32* %8 to i8*
%29 = load i8*, i8** %4
%30 = bitcast i8* %29 to i32*
%31 = call i32 @fwrite(i8* %28, i32 4, i32 1, i32* %30)
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%33 = load i64, i64* %32
%34 = trunc i64 %33 to i32
store i32 %34, i32* %8
%35 = bitcast i32* %8 to i8*
%36 = load i8*, i8** %4
%37 = bitcast i8* %36 to i32*
%38 = call i32 @fwrite(i8* %35, i32 4, i32 1, i32* %37)
%39 = load i32, i32* %6
store i32 %39, i32* %8
%40 = bitcast i32* %8 to i8*
%41 = load i8*, i8** %4
%42 = bitcast i8* %41 to i32*
%43 = call i32 @fwrite(i8* %40, i32 4, i32 1, i32* %42)
%44 = load i32, i32* %6
store i32 %44, i32* %8
%45 = bitcast i32* %8 to i8*
%46 = load i8*, i8** %4
%47 = bitcast i8* %46 to i32*
%48 = call i32 @fwrite(i8* %45, i32 4, i32 1, i32* %47)
%49 = load i8*, i8** %5
%50 = load i32, i32* %6
%51 = load i8*, i8** %4
%52 = bitcast i8* %51 to i32*
%53 = call i32 @fwrite(i8* %49, i32 %50, i32 1, i32* %52)
%54 = load i8*, i8** %4
%55 = bitcast i8* %54 to i32*
%56 = call i32 @fflush(i32* %55)
br label %57
57: ; preds = %13, %12
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define void @pcap_write(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i16
%7 = bitcast %struct.timeval* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i16* %6 to i8*
%10 = icmp eq i8* %0, null
br i1 %10, label %31, label %11
11: ; preds = %3
%12 = tail call zeroext i16 @crc16_data(i8* %1, i32 %2, i32 0)
store i16 %12, i16* %6
%13 = sext i32 %2 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = call i32 @memcpy(i8* %14, i16* %6, i32 2)
%16 = add nsw i32 %2, 2
%17 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
store i32 %20, i32* %5
%21 = bitcast i8* %0 to i32*
%22 = call i32 @fwrite(i8* %8, i32 4, i32 1, i32* %21)
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i32
store i32 %25, i32* %5
%26 = call i32 @fwrite(i8* %8, i32 4, i32 1, i32* %21)
store i32 %16, i32* %5
%27 = call i32 @fwrite(i8* %8, i32 4, i32 1, i32* %21)
store i32 %16, i32* %5
%28 = call i32 @fwrite(i8* %8, i32 4, i32 1, i32* %21)
%29 = call i32 @fwrite(i8* %1, i32 %16, i32 1, i32* %21)
%30 = call i32 @fflush(i32* %21)
br label %31
31: ; preds = %3, %11
ret void
}