#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 sockaddr_ll {
  int sll_halen;
  int sll_ifindex;
  int /*<<< orphan*/ sll_addr;
  void *sll_protocol;
  void *sll_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PF_PACKET;
void *htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned char *, int);
int sendto(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
           int);
int send_packet(int sock_fd, unsigned char *dest_mac, unsigned char *buffer,
                int interface_index, int packet_size) {
  struct sockaddr_ll dest_addr;
  dest_addr.sll_family = htons(PF_PACKET);
  dest_addr.sll_protocol = htons(ETH_P_ALL);
  dest_addr.sll_halen = 6;
  dest_addr.sll_ifindex = interface_index;
  memcpy(&(dest_addr.sll_addr), dest_mac, 6);
  return sendto(sock_fd, buffer, packet_size, 0,
                (struct sockaddr *)&(dest_addr), sizeof(struct sockaddr_ll));
}


x86 assembly (clang, -O0):
<send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x30(%rbp)
movl   $0x6,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x40(%rbp),%rdi
add    $0x8,%rdi
mov    -0x10(%rbp),%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    -0x40(%rbp),%r8
xor    %ecx,%ecx
mov    $0x20,%r9d
call   1040 <sendto@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_packet>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8d,%r14d
mov    %ecx,%ebx
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%r12d
mov    0x2e9f(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,0x18(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
movl   $0x6,(%rsp)
mov    %ebx,0x4(%rsp)
lea    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
mov    %rsp,%r8
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    $0x20,%r9d
call   1040 <sendto@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32, i8*, i8* }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@ETH_P_ALL = global i32 0
define i32 @send_packet(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_ll
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%12 = load i32, i32* @PF_PACKET
%13 = call i8* @htons(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 4
store i8* %13, i8** %14
%15 = load i32, i32* @ETH_P_ALL
%16 = call i8* @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 3
store i8* %16, i8** %17
%18 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 0
store i32 6, i32* %18
%19 = load i32, i32* %9
%20 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 2
%22 = load i8*, i8** %7
%23 = call i32 @memcpy(i32* %21, i8* %22, i32 6)
%24 = load i32, i32* %6
%25 = load i8*, i8** %8
%26 = load i32, i32* %10
%27 = bitcast %struct.sockaddr_ll* %11 to %struct.sockaddr*
%28 = call i32 @sendto(i32 %24, i8* %25, i32 %26, i32 0, %struct.sockaddr* %27, i32 32)
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32, i8*, i8* }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@ETH_P_ALL = global i32 0
define i32 @send_packet(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca %struct.sockaddr_ll
%7 = bitcast %struct.sockaddr_ll* %6 to i8*
%8 = load i32, i32* @PF_PACKET
%9 = tail call i8* @htons(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 4
store i8* %9, i8** %10
%11 = load i32, i32* @ETH_P_ALL
%12 = tail call i8* @htons(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 3
store i8* %12, i8** %13
%14 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 0
store i32 6, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 1
store i32 %3, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 2
%17 = call i32 @memcpy(i32* %16, i8* %1, i32 6)
%18 = bitcast %struct.sockaddr_ll* %6 to %struct.sockaddr*
%19 = call i32 @sendto(i32 %0, i8* %2, i32 %4, i32 0, %struct.sockaddr* %18, i32 32)
ret i32 %19
}