#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_protocol;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ mysocket;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int get_iface_index(char *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_ll *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sendto(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
           int);
int send_packet(int send_socket, char *interface, unsigned char *data,
                int datalen) {
  int result;
  int ifindex;
  struct sockaddr_ll mysocket;
  ifindex = get_iface_index(interface, send_socket);
  if (ifindex < 0) {
    printf("bad interface\n");
    return -1;
  }
  memset(&mysocket, '\0', sizeof(struct sockaddr));
  mysocket.sll_family = AF_PACKET;
  mysocket.sll_protocol = htons(0x0003);
  mysocket.sll_ifindex = ifindex;
  result = sendto(send_socket, data, datalen, 0, (struct sockaddr *)&mysocket,
                  sizeof(mysocket));
  if (result != datalen) {
    perror("sendto");
  }
  return result;
}


x86 assembly (clang, -O0):
<send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1070 <get_iface_index@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11a7 <send_packet+0x47>
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <send_packet+0xb2>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x3,%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     120c <send_packet+0xac>
lea    0xe08(%rip),%rdi
call   1080 <perror@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_packet>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
mov    %r15d,%esi
call   1070 <get_iface_index@plt>
test   %eax,%eax
js     11e3 <send_packet+0x83>
mov    %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x3,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
cmp    %ebx,%eax
je     11f4 <send_packet+0x94>
mov    %eax,%ebp
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%ebx
jmp    11f4 <send_packet+0x94>
lea    0xe1d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"bad interface\0A\00"
@AF_PACKET = global i32 0
@.str.1 = [7 x i8] c"sendto\00"
define i32 @send_packet(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_ll
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i8*, i8** %7
%14 = load i32, i32* %6
%15 = call i32 @get_iface_index(i8* %13, i32 %14)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %40
20: ; preds = %4
%21 = call i32 @memset(%struct.sockaddr_ll* %12, i8 signext 0, i32 4)
%22 = load i32, i32* @AF_PACKET
%23 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %12, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 3)
%25 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %12, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* %11
%27 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %12, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = bitcast %struct.sockaddr_ll* %12 to %struct.sockaddr*
%32 = call i32 @sendto(i32 %28, i8* %29, i32 %30, i32 0, %struct.sockaddr* %31, i32 12)
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = load i32, i32* %9
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %20
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %20
%39 = load i32, i32* %10
store i32 %39, i32* %5
br label %40
40: ; preds = %38, %18
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@.str.1 = [7 x i8] c"sendto\00"
@str = [14 x i8] c"bad interface\00"
define i32 @send_packet(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_ll
%6 = bitcast %struct.sockaddr_ll* %5 to i8*
%7 = tail call i32 @get_iface_index(i8* %1, i32 %0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %23
11: ; preds = %4
%12 = call i32 @memset(%struct.sockaddr_ll* %5, i8 signext 0, i32 4)
%13 = load i32, i32* @AF_PACKET
%14 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 3)
%16 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i64 0, i32 0
store i32 %7, i32* %17
%18 = bitcast %struct.sockaddr_ll* %5 to %struct.sockaddr*
%19 = call i32 @sendto(i32 %0, i8* %2, i32 %3, i32 0, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i32 %19, %3
br i1 %20, label %23, label %21
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %11, %21, %9
%24 = phi i32 [ -1, %9 ], [ %19, %21 ], [ %3, %11 ]
ret i32 %24
}