#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 */
struct timeval {
  int dummy;
};
struct icmp {
  scalar_t__ icmp_cksum;
  scalar_t__ icmp_data;
  int /*<<< orphan*/ icmp_id;
  scalar_t__ icmp_seq;
  scalar_t__ icmp_code;
  int /*<<< orphan*/ icmp_type;
};

/* Variables and functions */
int /*<<< orphan*/ ICMP_ECHO;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ nc_cal_chksum(unsigned short *, int);
int nc_pack(void *_buf) {
  int packsize;
  int datalen = 56;
  struct icmp *icmp;
  struct timeval *tval;
  icmp = (struct icmp *)_buf;
  icmp->icmp_type = ICMP_ECHO;
  icmp->icmp_code = 0;
  icmp->icmp_cksum = 0;
  icmp->icmp_seq = 0;
  icmp->icmp_id = getpid();
  packsize = 8 + datalen;
  tval = (struct timeval *)icmp->icmp_data;
  gettimeofday(tval, NULL);
  icmp->icmp_cksum = nc_cal_chksum((unsigned short *)icmp, packsize);
  return packsize;
}


x86 assembly (clang, -O0):
<nc_pack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x38,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x28(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,0x20(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x10(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <nc_cal_chksum@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nc_pack>:
push   %rbx
mov    %rdi,%rbx
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rdi)
movq   $0x0,(%rdi)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rdi)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x10(%rbx)
mov    0x8(%rbx),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %rbx,%rdi
mov    $0x40,%esi
call   1050 <nc_cal_chksum@plt>
mov    %rax,(%rbx)
mov    $0x40,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.icmp = type { i64, i64, i32, i64, i64, i32 }
%struct.timeval = type { i32 }
@ICMP_ECHO = global i32 0
define i32 @nc_pack(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.icmp*
%6 = alloca %struct.timeval*
store i8* %0, i8** %2
store i32 56, i32* %4
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to %struct.icmp*
store %struct.icmp* %8, %struct.icmp** %5
%9 = load i32, i32* @ICMP_ECHO
%10 = load %struct.icmp*, %struct.icmp** %5
%11 = getelementptr inbounds %struct.icmp, %struct.icmp* %10, i32 0, i32 5
store i32 %9, i32* %11
%12 = load %struct.icmp*, %struct.icmp** %5
%13 = getelementptr inbounds %struct.icmp, %struct.icmp* %12, i32 0, i32 4
store i64 0, i64* %13
%14 = load %struct.icmp*, %struct.icmp** %5
%15 = getelementptr inbounds %struct.icmp, %struct.icmp* %14, i32 0, i32 0
store i64 0, i64* %15
%16 = load %struct.icmp*, %struct.icmp** %5
%17 = getelementptr inbounds %struct.icmp, %struct.icmp* %16, i32 0, i32 3
store i64 0, i64* %17
%18 = call i32 (...) @getpid()
%19 = load %struct.icmp*, %struct.icmp** %5
%20 = getelementptr inbounds %struct.icmp, %struct.icmp* %19, i32 0, i32 2
store i32 %18, i32* %20
%21 = load i32, i32* %4
%22 = add nsw i32 8, %21
store i32 %22, i32* %3
%23 = load %struct.icmp*, %struct.icmp** %5
%24 = getelementptr inbounds %struct.icmp, %struct.icmp* %23, i32 0, i32 1
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to %struct.timeval*
store %struct.timeval* %26, %struct.timeval** %6
%27 = load %struct.timeval*, %struct.timeval** %6
%28 = call i32 @gettimeofday(%struct.timeval* %27, i32* null)
%29 = load %struct.icmp*, %struct.icmp** %5
%30 = bitcast %struct.icmp* %29 to i16*
%31 = load i32, i32* %3
%32 = call i64 @nc_cal_chksum(i16* %30, i32 %31)
%33 = load %struct.icmp*, %struct.icmp** %5
%34 = getelementptr inbounds %struct.icmp, %struct.icmp* %33, i32 0, i32 0
store i64 %32, i64* %34
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@ICMP_ECHO = global i32 0
define i32 @nc_pack(i8* %0) {
%2 = load i32, i32* @ICMP_ECHO
%3 = getelementptr inbounds i8, i8* %0, i64 40
%4 = bitcast i8* %3 to i32*
store i32 %2, i32* %4
%5 = bitcast i8* %0 to i64*
store i64 0, i64* %5
%6 = getelementptr inbounds i8, i8* %0, i64 24
%7 = tail call i32 (...) @getpid()
%8 = getelementptr inbounds i8, i8* %0, i64 16
%9 = bitcast i8* %8 to i32*
store i32 %7, i32* %9
%10 = getelementptr inbounds i8, i8* %0, i64 8
%11 = bitcast i8* %10 to i64*
%12 = load i64, i64* %11
%13 = inttoptr i64 %12 to %struct.timeval*
%14 = tail call i32 @gettimeofday(%struct.timeval* %13, i32* null)
%15 = bitcast i8* %0 to i16*
%16 = tail call i64 @nc_cal_chksum(i16* %15, i32 64)
store i64 %16, i64* %5
ret i32 64
}