#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_ifindex;
  int /*<<< orphan*/ sll_addr;
  int /*<<< orphan*/ sll_halen;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ETH_ALEN;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ sendto(int, void const *, size_t, int /*<<< orphan*/,
                  struct sockaddr *, int);
int send_to_socket(int sockfd, const void *buf, size_t len, int if_idx,
                   unsigned char *mac) {
  struct sockaddr_ll socket_address;
  socket_address.sll_ifindex = if_idx;
  socket_address.sll_halen = ETH_ALEN;
  memcpy(socket_address.sll_addr, mac, 6);
  if (sendto(sockfd, buf, len, 0, (struct sockaddr *)&socket_address,
             sizeof(struct sockaddr_ll)) < 0)
    perror("Send failed");
}


x86 assembly (clang, -O0):
<send_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x6,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1030 <sendto@plt>
cmp    $0x0,%rax
jge    119f <send_to_socket+0x6f>
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_to_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %ecx,(%rsp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r8,%rsi
mov    $0x6,%edx
call   1040 <memcpy@plt>
mov    %rsp,%r8
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1030 <sendto@plt>
test   %rax,%rax
js     1183 <send_to_socket+0x53>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe76(%rip),%rdi
call   1050 <perror@plt>
jmp    117a <send_to_socket+0x4a>

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@ETH_ALEN = global i32 0
@.str = [12 x i8] c"Send failed\00"
define i32 @send_to_socket(i32 %0, i8* %1, i64 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.sockaddr_ll
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%13 = load i32, i32* %10
%14 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %12, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @ETH_ALEN
%16 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %12, i32 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %12, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load i8*, i8** %11
%20 = call i32 @memcpy(i32 %18, i8* %19, i32 6)
%21 = load i32, i32* %7
%22 = load i8*, i8** %8
%23 = load i64, i64* %9
%24 = bitcast %struct.sockaddr_ll* %12 to %struct.sockaddr*
%25 = call i64 @sendto(i32 %21, i8* %22, i64 %23, i32 0, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %5
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %5
%30 = load i32, i32* %6
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@ETH_ALEN = global i32 0
@.str = [12 x i8] c"Send failed\00"
define i32 @send_to_socket(i32 %0, i8* %1, i64 %2, i32 %3, i8* %4) {
%6 = alloca %struct.sockaddr_ll
%7 = bitcast %struct.sockaddr_ll* %6 to i8*
%8 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 0
store i32 %3, i32* %8
%9 = load i32, i32* @ETH_ALEN
%10 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 2
store i32 %9, i32* %10
%11 = tail call i32 @memcpy(i32 undef, i8* %4, i32 6)
%12 = bitcast %struct.sockaddr_ll* %6 to %struct.sockaddr*
%13 = call i64 @sendto(i32 %0, i8* %1, i64 %2, i32 0, %struct.sockaddr* %12, i32 12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %5
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %5
ret i32 undef
}