#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct iphdr {
  int ihl;
  int version;
  int ttl;
  int protocol;
  scalar_t__ check;
  int /*<<< orphan*/ daddr;
  int /*<<< orphan*/ saddr;
  void *frag_off;
  void *id;
  void *tot_len;
  scalar_t__ tos;
};
typedef unsigned char icmphdr;
struct ethhdr {
  unsigned char *h_dest;
  unsigned char *h_source;
  void *h_proto;
};

/* Variables and functions */
int ETHERTYPE_IP;
scalar_t__ calc_ipchksum(struct iphdr *, int);
int errno;
int /*<<< orphan*/ free(void *);
void *htons(int);
void *malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, size_t);
int sendto_l2(int, int, struct ethhdr *, int);
int sendto_icmp_ipv4_l2(int ifidx, int sock, const unsigned char dst_mac[6],
                        const unsigned char src_mac[6],
                        const struct sockaddr_in *dst_ip,
                        const struct sockaddr_in *src_ip,
                        const struct icmphdr *in_icmp, size_t icmp_len,
                        int ttl) {
  void *mem = malloc(sizeof(struct ethhdr) + sizeof(struct iphdr) + icmp_len);
  struct ethhdr *ether = mem;
  struct iphdr *ip = (struct iphdr *)(ether + 1);
  struct icmphdr *icmp = (struct icmphdr *)(ip + 1);
  int result = 0, ret_errno = 0;
  static int id_base = 0;
  memcpy(ether->h_dest, dst_mac, 6);
  memcpy(ether->h_source, src_mac, 6);
  ether->h_proto = htons(ETHERTYPE_IP);
  ip->ihl = 5;
  ip->version = 4;
  ip->tos = 0;
  ip->tot_len = htons(ip->ihl * 4 + icmp_len);
  ip->id = htons(id_base++);
  ip->frag_off = htons(0x4000);
  ip->ttl = ttl;
  ip->protocol = 1;
  ip->check = 0;
  ip->saddr = src_ip->sin_addr.s_addr;
  ip->daddr = dst_ip->sin_addr.s_addr;
  memcpy(icmp, in_icmp, icmp_len);
  ip->check = calc_ipchksum(ip, ip->ihl * 4);
  result = sendto_l2(ifidx, sock, ether,
                     sizeof(struct ethhdr) + sizeof(struct iphdr) + icmp_len);
  ret_errno = errno;
  free(mem);
  errno = ret_errno;
  return result;
}


x86 assembly (clang, -O0):
<sendto_icmp_ipv4_l2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    0x18(%rbp),%rax
add    $0x58,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x40,%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x40(%rbp),%rax
movl   $0x5,(%rax)
mov    -0x40(%rbp),%rax
movl   $0x4,0x4(%rax)
mov    -0x40(%rbp),%rax
movq   $0x0,0x38(%rax)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
shl    $0x2,%eax
cltq
add    0x18(%rbp),%rax
mov    %eax,%edi
call   1050 <htons@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x30(%rax)
mov    0x2dfe(%rip),%edi
mov    %edi,%eax
add    $0x1,%eax
mov    %eax,0x2df3(%rip)
call   1050 <htons@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x28(%rax)
mov    $0x4000,%edi
call   1050 <htons@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    0x20(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x40(%rbp),%rax
movl   $0x1,0xc(%rax)
mov    -0x40(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x1c(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    -0x48(%rbp),%rdi
mov    0x10(%rbp),%rsi
mov    0x18(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
shl    $0x2,%esi
call   1040 <calc_ipchksum@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    0x18(%rbp),%rax
add    $0x58,%rax
mov    %eax,%ecx
call   1080 <sendto_l2@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%ecx
mov    0x2cae(%rip),%rax
mov    %ecx,(%rax)
mov    -0x4c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendto_icmp_ipv4_l2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x18(%rsp)
mov    %r8,0x20(%rsp)
mov    %rcx,%r13
mov    %rdx,%r14
mov    %esi,0xc(%rsp)
mov    %edi,0x8(%rsp)
mov    0x68(%rsp),%r15
lea    0x58(%r15),%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
add    $0x18,%rax
mov    %rax,0x10(%rsp)
lea    0x58(%rbp),%r12
mov    $0x6,%edx
mov    %r14,%rsi
call   1060 <memcpy@plt>
mov    $0x6,%edx
mov    %r13,%rsi
call   1060 <memcpy@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %rax,0x10(%rbp)
movabs $0x400000005,%rax
mov    %rax,0x18(%rbp)
movq   $0x0,0x50(%rbp)
lea    0x14(%r15),%edi
call   1050 <htons@plt>
mov    %rax,0x48(%rbp)
mov    0x2e59(%rip),%edi
lea    0x1(%rdi),%eax
mov    %eax,0x2e50(%rip)
call   1050 <htons@plt>
mov    %rax,0x40(%rbp)
mov    $0x4000,%edi
call   1050 <htons@plt>
mov    %rax,0x38(%rbp)
mov    0x70(%rsp),%eax
mov    %eax,0x20(%rbp)
movl   $0x1,0x24(%rbp)
movq   $0x0,0x28(%rbp)
mov    0x18(%rsp),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rbp)
mov    0x20(%rsp),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rbp)
mov    %r12,%rdi
mov    0x60(%rsp),%rsi
mov    %r15,%rdx
call   1060 <memcpy@plt>
mov    0x18(%rbp),%esi
shl    $0x2,%esi
mov    0x10(%rsp),%rdi
call   1040 <calc_ipchksum@plt>
mov    %rax,0x28(%rbp)
mov    0x8(%rsp),%edi
mov    0xc(%rsp),%esi
mov    %rbp,%rdx
mov    %ebx,%ecx
call   1080 <sendto_l2@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.icmphdr = type opaque
%struct.ethhdr = type { i8*, i8*, i8* }
%struct.iphdr = type { i32, i32, i32, i32, i64, i32, i32, i8*, i8*, i8*, i64 }
@sendto_icmp_ipv4_l2.id_base = internal global i32 0
@ETHERTYPE_IP = global i32 0
@errno = global i32 0
define i32 @sendto_icmp_ipv4_l2(i32 %0, i32 %1, i8* %2, i8* %3, %struct.sockaddr_in* %4, %struct.sockaddr_in* %5, %struct.icmphdr* %6, i64 %7, i32 %8) {
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca %struct.sockaddr_in*
%15 = alloca %struct.sockaddr_in*
%16 = alloca %struct.icmphdr*
%17 = alloca i64
%18 = alloca i32
%19 = alloca i8*
%20 = alloca %struct.ethhdr*
%21 = alloca %struct.iphdr*
%22 = alloca %struct.icmphdr*
%23 = alloca i32
%24 = alloca i32
store i32 %0, i32* %10
store i32 %1, i32* %11
store i8* %2, i8** %12
store i8* %3, i8** %13
store %struct.sockaddr_in* %4, %struct.sockaddr_in** %14
store %struct.sockaddr_in* %5, %struct.sockaddr_in** %15
store %struct.icmphdr* %6, %struct.icmphdr** %16
store i64 %7, i64* %17
store i32 %8, i32* %18
%25 = load i64, i64* %17
%26 = add i64 88, %25
%27 = trunc i64 %26 to i32
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %19
%29 = load i8*, i8** %19
%30 = bitcast i8* %29 to %struct.ethhdr*
store %struct.ethhdr* %30, %struct.ethhdr** %20
%31 = load %struct.ethhdr*, %struct.ethhdr** %20
%32 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %31, i64 1
%33 = bitcast %struct.ethhdr* %32 to %struct.iphdr*
store %struct.iphdr* %33, %struct.iphdr** %21
%34 = load %struct.iphdr*, %struct.iphdr** %21
%35 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %34, i64 1
%36 = bitcast %struct.iphdr* %35 to %struct.icmphdr*
store %struct.icmphdr* %36, %struct.icmphdr** %22
store i32 0, i32* %23
store i32 0, i32* %24
%37 = load %struct.ethhdr*, %struct.ethhdr** %20
%38 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = load i8*, i8** %12
%41 = call i32 @memcpy(i8* %39, i8* %40, i64 6)
%42 = load %struct.ethhdr*, %struct.ethhdr** %20
%43 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %42, i32 0, i32 1
%44 = load i8*, i8** %43
%45 = load i8*, i8** %13
%46 = call i32 @memcpy(i8* %44, i8* %45, i64 6)
%47 = load i32, i32* @ETHERTYPE_IP
%48 = call i8* @htons(i32 %47)
%49 = load %struct.ethhdr*, %struct.ethhdr** %20
%50 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %49, i32 0, i32 2
store i8* %48, i8** %50
%51 = load %struct.iphdr*, %struct.iphdr** %21
%52 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %51, i32 0, i32 0
store i32 5, i32* %52
%53 = load %struct.iphdr*, %struct.iphdr** %21
%54 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %53, i32 0, i32 1
store i32 4, i32* %54
%55 = load %struct.iphdr*, %struct.iphdr** %21
%56 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %55, i32 0, i32 10
store i64 0, i64* %56
%57 = load %struct.iphdr*, %struct.iphdr** %21
%58 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %57, i32 0, i32 0
%59 = load i32, i32* %58
%60 = mul nsw i32 %59, 4
%61 = sext i32 %60 to i64
%62 = load i64, i64* %17
%63 = add i64 %61, %62
%64 = trunc i64 %63 to i32
%65 = call i8* @htons(i32 %64)
%66 = load %struct.iphdr*, %struct.iphdr** %21
%67 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %66, i32 0, i32 9
store i8* %65, i8** %67
%68 = load i32, i32* @sendto_icmp_ipv4_l2.id_base
%69 = add nsw i32 %68, 1
store i32 %69, i32* @sendto_icmp_ipv4_l2.id_base
%70 = call i8* @htons(i32 %68)
%71 = load %struct.iphdr*, %struct.iphdr** %21
%72 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %71, i32 0, i32 8
store i8* %70, i8** %72
%73 = call i8* @htons(i32 16384)
%74 = load %struct.iphdr*, %struct.iphdr** %21
%75 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %74, i32 0, i32 7
store i8* %73, i8** %75
%76 = load i32, i32* %18
%77 = load %struct.iphdr*, %struct.iphdr** %21
%78 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %77, i32 0, i32 2
store i32 %76, i32* %78
%79 = load %struct.iphdr*, %struct.iphdr** %21
%80 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %79, i32 0, i32 3
store i32 1, i32* %80
%81 = load %struct.iphdr*, %struct.iphdr** %21
%82 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %81, i32 0, i32 4
store i64 0, i64* %82
%83 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%84 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %83, i32 0, i32 0
%85 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %84, i32 0, i32 0
%86 = load i32, i32* %85
%87 = load %struct.iphdr*, %struct.iphdr** %21
%88 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %87, i32 0, i32 6
store i32 %86, i32* %88
%89 = load %struct.sockaddr_in*, %struct.sockaddr_in** %14
%90 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %89, i32 0, i32 0
%91 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %90, i32 0, i32 0
%92 = load i32, i32* %91
%93 = load %struct.iphdr*, %struct.iphdr** %21
%94 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %93, i32 0, i32 5
store i32 %92, i32* %94
%95 = load %struct.icmphdr*, %struct.icmphdr** %22
%96 = bitcast %struct.icmphdr* %95 to i8*
%97 = load %struct.icmphdr*, %struct.icmphdr** %16
%98 = bitcast %struct.icmphdr* %97 to i8*
%99 = load i64, i64* %17
%100 = call i32 @memcpy(i8* %96, i8* %98, i64 %99)
%101 = load %struct.iphdr*, %struct.iphdr** %21
%102 = load %struct.iphdr*, %struct.iphdr** %21
%103 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %102, i32 0, i32 0
%104 = load i32, i32* %103
%105 = mul nsw i32 %104, 4
%106 = call i64 @calc_ipchksum(%struct.iphdr* %101, i32 %105)
%107 = load %struct.iphdr*, %struct.iphdr** %21
%108 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %107, i32 0, i32 4
store i64 %106, i64* %108
%109 = load i32, i32* %10
%110 = load i32, i32* %11
%111 = load %struct.ethhdr*, %struct.ethhdr** %20
%112 = load i64, i64* %17
%113 = add i64 88, %112
%114 = trunc i64 %113 to i32
%115 = call i32 @sendto_l2(i32 %109, i32 %110, %struct.ethhdr* %111, i32 %114)
store i32 %115, i32* %23
%116 = load i32, i32* @errno
store i32 %116, i32* %24
%117 = load i8*, i8** %19
%118 = call i32 @free(i8* %117)
%119 = load i32, i32* %24
store i32 %119, i32* @errno
%120 = load i32, i32* %23
ret i32 %120
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.icmphdr = type opaque
%struct.ethhdr = type { i8*, i8*, i8* }
%struct.iphdr = type { i32, i32, i32, i32, i64, i32, i32, i8*, i8*, i8*, i64 }
@sendto_icmp_ipv4_l2.id_base = internal unnamed_addr global i32 0
@ETHERTYPE_IP = global i32 0
@errno = global i32 0
define i32 @sendto_icmp_ipv4_l2(i32 %0, i32 %1, i8* %2, i8* %3, %struct.sockaddr_in* nocapture %4, %struct.sockaddr_in* nocapture %5, %struct.icmphdr* %6, i64 %7, i32 %8) {
%10 = trunc i64 %7 to i32
%11 = add i32 %10, 88
%12 = tail call i8* @malloc(i32 %11)
%13 = bitcast i8* %12 to %struct.ethhdr*
%14 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 1
%15 = bitcast %struct.ethhdr* %14 to %struct.iphdr*
%16 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 3, i32 2
%17 = tail call i32 @memcpy(i8* undef, i8* %2, i64 6)
%18 = tail call i32 @memcpy(i8* undef, i8* %3, i64 6)
%19 = load i32, i32* @ETHERTYPE_IP
%20 = tail call i8* @htons(i32 %19)
%21 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 0, i32 2
store i8* %20, i8** %21
%22 = bitcast %struct.ethhdr* %14 to i32*
store i32 5, i32* %22
%23 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %15, i64 0, i32 1
store i32 4, i32* %23
%24 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 3, i32 1
%25 = bitcast i8** %24 to i64*
store i64 0, i64* %25
%26 = add i32 %10, 20
%27 = tail call i8* @htons(i32 %26)
%28 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 3, i32 0
store i8* %27, i8** %28
%29 = load i32, i32* @sendto_icmp_ipv4_l2.id_base
%30 = add nsw i32 %29, 1
store i32 %30, i32* @sendto_icmp_ipv4_l2.id_base
%31 = tail call i8* @htons(i32 %29)
%32 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 2, i32 2
store i8* %31, i8** %32
%33 = tail call i8* @htons(i32 16384)
%34 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 2, i32 1
store i8* %33, i8** %34
%35 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 1, i32 1
%36 = bitcast i8** %35 to i32*
store i32 %8, i32* %36
%37 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %15, i64 0, i32 3
store i32 1, i32* %37
%38 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 1, i32 2
%39 = bitcast i8** %38 to i64*
store i64 0, i64* %39
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %15, i64 0, i32 6
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %13, i64 2
%46 = bitcast %struct.ethhdr* %45 to i32*
store i32 %44, i32* %46
%47 = bitcast i8** %16 to i8*
%48 = bitcast %struct.icmphdr* %6 to i8*
%49 = tail call i32 @memcpy(i8* %47, i8* %48, i64 %7)
%50 = load i32, i32* %22
%51 = shl nsw i32 %50, 2
%52 = tail call i64 @calc_ipchksum(%struct.iphdr* %15, i32 %51)
store i64 %52, i64* %39
%53 = tail call i32 @sendto_l2(i32 %0, i32 %1, %struct.ethhdr* %13, i32 %11)
%54 = tail call i32 @free(i8* %12)
ret i32 %53
}