#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 iphdr {
  int checksum;
  int /*<<< orphan*/ type;
};
typedef struct iphdr u_char;
struct icmphdr {
  int checksum;
  int /*<<< orphan*/ type;
};
struct icmp {
  int dummy;
};
typedef int /*<<< orphan*/ packet;
typedef int /*<<< orphan*/ ip;
typedef int /*<<< orphan*/ icmp;

/* Variables and functions */
int /*<<< orphan*/ CompileIPhdr(struct iphdr *, char *, char *,
                                int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ICMP_ECHO;
int /*<<< orphan*/ IPPROTO_ICMP;
int /*<<< orphan*/ free(struct iphdr *);
int /*<<< orphan*/ getpid();
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(struct iphdr *, struct iphdr *, int);
int /*<<< orphan*/ memset(struct iphdr *, int /*<<< orphan*/, int);
int CompileICMPacket(char *soAddr, char *dAddr, u_char *rPckt) {
  struct iphdr ip;
  struct icmphdr icmp;
  u_char *packet;
  packet = (u_char *)malloc(sizeof(struct iphdr) + sizeof(struct icmp));
  memset(packet, 0, sizeof(packet));
  memset(&icmp, 0, sizeof(icmp));
  CompileIPhdr(&ip, soAddr, dAddr, IPPROTO_ICMP, getpid());
  memcpy(packet, &ip, sizeof(ip));
  memset(&icmp, 0, sizeof(icmp));
  icmp.type = ICMP_ECHO;
  icmp.checksum = 0xfff7;
  memcpy(packet + sizeof(struct iphdr), &icmp, sizeof(icmp));
  memcpy(rPckt, packet, sizeof(struct iphdr) + sizeof(struct icmp));
  free(packet);
  return 1;
}


x86 assembly (clang, -O0):
<CompileICMPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0xc,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x34(%rbp),%ecx
mov    %eax,%r8d
lea    -0x20(%rbp),%rdi
call   1050 <CompileIPhdr@plt>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0xfff7,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x40,%rdi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CompileICMPacket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%r12
mov    %rdi,%r13
mov    $0xc,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1050 <CompileIPhdr@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
movl   $0xfff7,(%rsp)
lea    0x40(%rbx),%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32 }
%struct.icmphdr = type { i32, i32 }
@IPPROTO_ICMP = global i32 0
@ICMP_ECHO = global i32 0
define i32 @CompileICMPacket(i8* %0, i8* %1, %struct.iphdr* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.iphdr*
%7 = alloca %struct.iphdr
%8 = alloca %struct.icmphdr
%9 = alloca %struct.iphdr*
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.iphdr* %2, %struct.iphdr** %6
%10 = call i64 @malloc(i32 12)
%11 = inttoptr i64 %10 to %struct.iphdr*
store %struct.iphdr* %11, %struct.iphdr** %9
%12 = load %struct.iphdr*, %struct.iphdr** %9
%13 = call i32 @memset(%struct.iphdr* %12, i32 0, i32 8)
%14 = bitcast %struct.icmphdr* %8 to %struct.iphdr*
%15 = call i32 @memset(%struct.iphdr* %14, i32 0, i32 8)
%16 = load i8*, i8** %4
%17 = load i8*, i8** %5
%18 = load i32, i32* @IPPROTO_ICMP
%19 = call i32 (...) @getpid()
%20 = call i32 @CompileIPhdr(%struct.iphdr* %7, i8* %16, i8* %17, i32 %18, i32 %19)
%21 = load %struct.iphdr*, %struct.iphdr** %9
%22 = call i32 @memcpy(%struct.iphdr* %21, %struct.iphdr* %7, i32 8)
%23 = bitcast %struct.icmphdr* %8 to %struct.iphdr*
%24 = call i32 @memset(%struct.iphdr* %23, i32 0, i32 8)
%25 = load i32, i32* @ICMP_ECHO
%26 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %8, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %8, i32 0, i32 0
store i32 65527, i32* %27
%28 = load %struct.iphdr*, %struct.iphdr** %9
%29 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %28, i64 8
%30 = bitcast %struct.icmphdr* %8 to %struct.iphdr*
%31 = call i32 @memcpy(%struct.iphdr* %29, %struct.iphdr* %30, i32 8)
%32 = load %struct.iphdr*, %struct.iphdr** %6
%33 = load %struct.iphdr*, %struct.iphdr** %9
%34 = call i32 @memcpy(%struct.iphdr* %32, %struct.iphdr* %33, i32 12)
%35 = load %struct.iphdr*, %struct.iphdr** %9
%36 = call i32 @free(%struct.iphdr* %35)
ret i32 1
}

LLVM IR (-O2):
%struct.iphdr = type { i32, i32 }
%struct.icmphdr = type { i32, i32 }
@IPPROTO_ICMP = global i32 0
@ICMP_ECHO = global i32 0
define i32 @CompileICMPacket(i8* %0, i8* %1, %struct.iphdr* %2) {
%4 = alloca %struct.iphdr
%5 = alloca %struct.icmphdr
%6 = bitcast %struct.iphdr* %4 to i8*
%7 = bitcast %struct.icmphdr* %5 to i8*
%8 = tail call i64 @malloc(i32 12)
%9 = inttoptr i64 %8 to %struct.iphdr*
%10 = tail call i32 @memset(%struct.iphdr* %9, i32 0, i32 8)
%11 = bitcast %struct.icmphdr* %5 to %struct.iphdr*
%12 = call i32 @memset(%struct.iphdr* %11, i32 0, i32 8)
%13 = load i32, i32* @IPPROTO_ICMP
%14 = call i32 (...) @getpid()
%15 = call i32 @CompileIPhdr(%struct.iphdr* %4, i8* %0, i8* %1, i32 %13, i32 %14)
%16 = call i32 @memcpy(%struct.iphdr* %9, %struct.iphdr* %4, i32 8)
%17 = call i32 @memset(%struct.iphdr* %11, i32 0, i32 8)
%18 = load i32, i32* @ICMP_ECHO
%19 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %5, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %5, i64 0, i32 0
store i32 65527, i32* %20
%21 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %9, i64 8
%22 = call i32 @memcpy(%struct.iphdr* %21, %struct.iphdr* %11, i32 8)
%23 = call i32 @memcpy(%struct.iphdr* %2, %struct.iphdr* %9, i32 12)
%24 = call i32 @free(%struct.iphdr* %9)
ret i32 1
}