#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 */
typedef int /*<<< orphan*/ uint8_t;
struct sockaddr_ll {
  int sll_halen;
  int *sll_addr;
  int /*<<< orphan*/ sll_pkttype;
  void *sll_hatype;
  int /*<<< orphan*/ sll_ifindex;
  void *sll_protocol;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int ETH_P_ALL;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PACKET_BROADCAST;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
void *htons(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sendto(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, void *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int sendframe(uint8_t *buf, int buflen, char *intname) {
  int sd;
  struct sockaddr_ll sa;
  struct ifreq ifr;
  sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sd < 0) {
    perror("can't open socket\n");
    exit(1);
  }
  printf("%s\n", intname);
  strncpy(ifr.ifr_name, intname, IFNAMSIZ);
  if (ioctl(sd, SIOCGIFINDEX, &ifr) == -1) {
    perror("can't retrieve etherenet index\n");
    exit(1);
  }
  memset(&sa, 0x00, sizeof(sa));
  sa.sll_family = PF_PACKET;
  sa.sll_protocol = htons(ETH_P_ALL);
  sa.sll_ifindex = ifr.ifr_ifindex;
  sa.sll_hatype = htons(0x0001);
  sa.sll_pkttype = (PACKET_BROADCAST);
  sa.sll_halen = 6;
  sa.sll_addr[6] = 0x00;
  sa.sll_addr[7] = 0x00;
  sendto(sd, buf, buflen, 0, (struct sockaddr *)&sa, sizeof(sa));
  close(sd);
  return (0);
}


x86 assembly (clang, -O0):
<sendframe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x68(%rbp),%edi
mov    -0x64(%rbp),%esi
mov    %rax,%rdx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    120a <sendframe+0x6a>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
mov    -0x5c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1263 <sendframe+0xc3>
lea    0xdc3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1060 <memset@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x30(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x1,%edi
call   1040 <htons@plt>
mov    %rax,-0x40(%rbp)
mov    0x2d14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
movl   $0x6,-0x58(%rbp)
mov    -0x50(%rbp),%rax
movl   $0x0,0x18(%rax)
mov    -0x50(%rbp),%rax
movl   $0x0,0x1c(%rax)
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x58(%rbp),%r8
xor    %ecx,%ecx
mov    $0x38,%r9d
call   1080 <sendto@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendframe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r12
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e32(%rip),%rax
mov    (%rax),%ebp
mov    0x2e19(%rip),%rax
mov    (%rax),%ebx
mov    0x2e00(%rip),%r13
mov    0x0(%r13),%edi
call   1040 <htons@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %rax,%rdx
call   10c0 <socket@plt>
test   %eax,%eax
js     12c5 <sendframe+0x125>
mov    %eax,%ebp
lea    0xe1d(%rip),%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rsi
call   1030 <strncpy@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12ce <sendframe+0x12e>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1060 <memset@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
mov    0x0(%r13),%edi
call   1040 <htons@plt>
mov    %rax,0x38(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x30(%rsp)
mov    $0x1,%edi
call   1040 <htons@plt>
mov    %rax,0x28(%rsp)
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movl   $0x6,0x10(%rsp)
mov    0x18(%rsp),%rax
movq   $0x0,0x18(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    $0x38,%r9d
call   1080 <sendto@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd34(%rip),%rdi
jmp    12d5 <sendframe+0x135>
lea    0xd42(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32*, i32, i8*, i32, i8*, i32 }
%struct.ifreq = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [19 x i8] c"can't open socket\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.2 = [32 x i8] c"can't retrieve etherenet index\0A\00"
@PACKET_BROADCAST = global i32 0
define i32 @sendframe(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_ll
%9 = alloca %struct.ifreq
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i32, i32* @PF_PACKET
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @ETH_P_ALL
%13 = call i8* @htons(i32 %12)
%14 = call i32 @socket(i32 %10, i32 %11, i8* %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i8*, i8** %6
%26 = load i32, i32* @IFNAMSIZ
%27 = call i32 @strncpy(i32 %24, i8* %25, i32 %26)
%28 = load i32, i32* %7
%29 = load i32, i32* @SIOCGIFINDEX
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.ifreq* %9)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %20
%36 = call i32 @memset(%struct.sockaddr_ll* %8, i32 0, i32 56)
%37 = load i32, i32* @PF_PACKET
%38 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 6
store i32 %37, i32* %38
%39 = load i32, i32* @ETH_P_ALL
%40 = call i8* @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 5
store i8* %40, i8** %41
%42 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 4
store i32 %43, i32* %44
%45 = call i8* @htons(i32 1)
%46 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 3
store i8* %45, i8** %46
%47 = load i32, i32* @PACKET_BROADCAST
%48 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 2
store i32 %47, i32* %48
%49 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 0
store i32 6, i32* %49
%50 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 1
%51 = load i32*, i32** %50
%52 = getelementptr inbounds i32, i32* %51, i64 6
store i32 0, i32* %52
%53 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 1
%54 = load i32*, i32** %53
%55 = getelementptr inbounds i32, i32* %54, i64 7
store i32 0, i32* %55
%56 = load i32, i32* %7
%57 = load i32*, i32** %4
%58 = load i32, i32* %5
%59 = bitcast %struct.sockaddr_ll* %8 to %struct.sockaddr*
%60 = call i32 @sendto(i32 %56, i32* %57, i32 %58, i32 0, %struct.sockaddr* %59, i32 56)
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32*, i32, i8*, i32, i8*, i32 }
%struct.ifreq = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [19 x i8] c"can't open socket\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.2 = [32 x i8] c"can't retrieve etherenet index\0A\00"
@PACKET_BROADCAST = global i32 0
define i32 @sendframe(i32* %0, i32 %1, i8* %2) {
%4 = alloca %struct.sockaddr_ll
%5 = alloca %struct.ifreq
%6 = bitcast %struct.sockaddr_ll* %4 to i8*
%7 = bitcast %struct.ifreq* %5 to i8*
%8 = load i32, i32* @PF_PACKET
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @ETH_P_ALL
%11 = tail call i8* @htons(i32 %10)
%12 = tail call i32 @socket(i32 %8, i32 %9, i8* %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%19 = load i32, i32* @IFNAMSIZ
%20 = tail call i32 @strncpy(i32 undef, i8* %2, i32 %19)
%21 = load i32, i32* @SIOCGIFINDEX
%22 = call i32 @ioctl(i32 %12, i32 %21, %struct.ifreq* %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = call i32 @memset(%struct.sockaddr_ll* %4, i32 0, i32 56)
%29 = load i32, i32* @PF_PACKET
%30 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 6
store i32 %29, i32* %30
%31 = load i32, i32* @ETH_P_ALL
%32 = call i8* @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 5
store i8* %32, i8** %33
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 4
store i32 %35, i32* %36
%37 = call i8* @htons(i32 1)
%38 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 3
store i8* %37, i8** %38
%39 = load i32, i32* @PACKET_BROADCAST
%40 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 2
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 0
store i32 6, i32* %41
%42 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 1
%43 = load i32*, i32** %42
%44 = getelementptr inbounds i32, i32* %43, i64 6
store i32 0, i32* %44
%45 = getelementptr inbounds i32, i32* %43, i64 7
store i32 0, i32* %45
%46 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%47 = call i32 @sendto(i32 %12, i32* %0, i32 %1, i32 0, %struct.sockaddr* %46, i32 56)
%48 = call i32 @close(i32 %12)
ret i32 0
}