#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 dummy;
};
struct ethhdr {
  int /*<<< orphan*/ h_proto;
  int /*<<< orphan*/ h_source;
  int /*<<< orphan*/ h_dest;
};

/* Variables and functions */
int /*<<< orphan*/ ETHERTYPE_IP;
int errno;
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
void *malloc(int);
int /*<<< orphan*/ memcpy(struct iphdr *, void const *, size_t);
int sendto_l2(int, int, struct ethhdr *, int);
int sendto_ip_l2(int ifidx, int sock, const unsigned char dst_mac[6],
                 const unsigned char src_mac[6], const void *data, size_t len) {
  void *mem = malloc(sizeof(struct ethhdr) + len);
  struct ethhdr *ether = mem;
  struct iphdr *ip = (struct iphdr *)(ether + 1);
  int result = 0, ret_errno = 0;
  memcpy(ether->h_dest, dst_mac, 6);
  memcpy(ether->h_source, src_mac, 6);
  ether->h_proto = htons(ETHERTYPE_IP);
  memcpy(ip, data, len);
  result = sendto_l2(ifidx, sock, ether, sizeof(struct ethhdr) + len);
  ret_errno = errno;
  free(mem);
  errno = ret_errno;
  return result;
}


x86 assembly (clang, -O0):
<sendto_ip_l2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
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    -0x28(%rbp),%rax
add    $0xc,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x38(%rbp),%rax
movslq 0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rax
movslq 0x4(%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rax
add    $0xc,%rax
mov    %eax,%ecx
call   1070 <sendto_l2@plt>
mov    %eax,-0x44(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%ecx
mov    0x2da7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendto_ip_l2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r12
mov    %r8,0x10(%rsp)
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,0xc(%rsp)
mov    %edi,0x8(%rsp)
lea    0xc(%r9),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xc(%rax),%r13
mov    $0x6,%edx
xor    %edi,%edi
mov    %r15,%rsi
call   1050 <memcpy@plt>
mov    $0x6,%edx
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <memcpy@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,(%rbx)
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
mov    %r12,%rdx
call   1050 <memcpy@plt>
mov    0x8(%rsp),%edi
mov    0xc(%rsp),%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1070 <sendto_l2@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ethhdr = type { i32, i32, i32 }
%struct.iphdr = type { i32 }
@ETHERTYPE_IP = global i32 0
@errno = global i32 0
define i32 @sendto_ip_l2(i32 %0, i32 %1, i8* %2, i8* %3, i8* %4, i64 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca %struct.ethhdr*
%15 = alloca %struct.iphdr*
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i64 %5, i64* %12
%18 = load i64, i64* %12
%19 = add i64 12, %18
%20 = trunc i64 %19 to i32
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %13
%22 = load i8*, i8** %13
%23 = bitcast i8* %22 to %struct.ethhdr*
store %struct.ethhdr* %23, %struct.ethhdr** %14
%24 = load %struct.ethhdr*, %struct.ethhdr** %14
%25 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %24, i64 1
%26 = bitcast %struct.ethhdr* %25 to %struct.iphdr*
store %struct.iphdr* %26, %struct.iphdr** %15
store i32 0, i32* %16
store i32 0, i32* %17
%27 = load %struct.ethhdr*, %struct.ethhdr** %14
%28 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to %struct.iphdr*
%32 = load i8*, i8** %9
%33 = call i32 @memcpy(%struct.iphdr* %31, i8* %32, i64 6)
%34 = load %struct.ethhdr*, %struct.ethhdr** %14
%35 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = sext i32 %36 to i64
%38 = inttoptr i64 %37 to %struct.iphdr*
%39 = load i8*, i8** %10
%40 = call i32 @memcpy(%struct.iphdr* %38, i8* %39, i64 6)
%41 = load i32, i32* @ETHERTYPE_IP
%42 = call i32 @htons(i32 %41)
%43 = load %struct.ethhdr*, %struct.ethhdr** %14
%44 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %43, i32 0, i32 0
store i32 %42, i32* %44
%45 = load %struct.iphdr*, %struct.iphdr** %15
%46 = load i8*, i8** %11
%47 = load i64, i64* %12
%48 = call i32 @memcpy(%struct.iphdr* %45, i8* %46, i64 %47)
%49 = load i32, i32* %7
%50 = load i32, i32* %8
%51 = load %struct.ethhdr*, %struct.ethhdr** %14
%52 = load i64, i64* %12
%53 = add i64 12, %52
%54 = trunc i64 %53 to i32
%55 = call i32 @sendto_l2(i32 %49, i32 %50, %struct.ethhdr* %51, i32 %54)
store i32 %55, i32* %16
%56 = load i32, i32* @errno
store i32 %56, i32* %17
%57 = load i8*, i8** %13
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %17
store i32 %59, i32* @errno
%60 = load i32, i32* %16
ret i32 %60
}

LLVM IR (-O2):
%struct.ethhdr = type { i32, i32, i32 }
%struct.iphdr = type { i32 }
@ETHERTYPE_IP = global i32 0
@errno = global i32 0
define i32 @sendto_ip_l2(i32 %0, i32 %1, i8* %2, i8* %3, i8* %4, i64 %5) {
%7 = trunc i64 %5 to i32
%8 = add i32 %7, 12
%9 = tail call i8* @malloc(i32 %8)
%10 = bitcast i8* %9 to %struct.ethhdr*
%11 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %10, i64 1
%12 = bitcast %struct.ethhdr* %11 to %struct.iphdr*
%13 = tail call i32 @memcpy(%struct.iphdr* null, i8* %2, i64 6)
%14 = tail call i32 @memcpy(%struct.iphdr* null, i8* %3, i64 6)
%15 = load i32, i32* @ETHERTYPE_IP
%16 = tail call i32 @htons(i32 %15)
%17 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %10, i64 0, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @memcpy(%struct.iphdr* %12, i8* %4, i64 %5)
%19 = tail call i32 @sendto_l2(i32 %0, i32 %1, %struct.ethhdr* %10, i32 %8)
%20 = tail call i32 @free(i8* %9)
ret i32 %19
}