#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 /*<<< orphan*/ sll_pkttype;
  void *sll_hatype;
  int /*<<< orphan*/ sll_ifindex;
  void *sll_protocol;
  int /*<<< orphan*/ sll_family;
};
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*/ 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 socket(int /*<<< orphan*/, int /*<<< orphan*/, void *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int getmonsock(char *intname) {
  int sock;
  struct sockaddr_ll sa;
  struct ifreq ifr;
  sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sock < 0) {
    perror("can't open socket\n");
    exit(1);
  }
  strncpy(ifr.ifr_name, intname, IFNAMSIZ);
  if (ioctl(sock, 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;
  return (sock);
}


x86 assembly (clang, -O0):
<getmonsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    %rax,%rdx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d3 <getmonsock+0x63>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x3c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0xc(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    121c <getmonsock+0xac>
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x20(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x1,%edi
call   1040 <htons@plt>
mov    %rax,-0x30(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movl   $0x6,-0x38(%rbp)
mov    -0xc(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getmonsock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%r14
mov    0x2e6a(%rip),%r15
mov    (%r15),%ebx
mov    0x2e50(%rip),%rax
mov    (%rax),%ebp
mov    0x2e37(%rip),%r12
mov    (%r12),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %rax,%rdx
call   1090 <socket@plt>
test   %eax,%eax
js     1223 <getmonsock+0xb3>
mov    %eax,%ebx
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     122c <getmonsock+0xbc>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x28(%rsp)
mov    (%r12),%edi
call   1040 <htons@plt>
mov    %rax,0x20(%rsp)
mov    (%rsp),%eax
mov    %eax,0x18(%rsp)
mov    $0x1,%edi
call   1040 <htons@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd6(%rip),%rdi
jmp    1233 <getmonsock+0xc3>
lea    0xde0(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i8*, i32, i8*, i32 }
%struct.ifreq = type { i32, 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"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [32 x i8] c"can't retrieve etherenet index\0A\00"
@PACKET_BROADCAST = global i32 0
define i32 @getmonsock(i8* %0) {
%2 = alloca %struct.sockaddr_ll
%3 = alloca %struct.ifreq
%4 = bitcast %struct.sockaddr_ll* %2 to i8*
%5 = bitcast %struct.ifreq* %3 to i8*
%6 = load i32, i32* @PF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @ETH_P_ALL
%9 = tail call i8* @htons(i32 %8)
%10 = tail call i32 @socket(i32 %6, i32 %7, i8* %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i32, i32* @IFNAMSIZ
%17 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %16)
%18 = load i32, i32* @SIOCGIFINDEX
%19 = call i32 @ioctl(i32 %10, i32 %18, %struct.ifreq* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = call i32 @memset(%struct.sockaddr_ll* %2, i32 0, i32 40)
%26 = load i32, i32* @PF_PACKET
%27 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 5
store i32 %26, i32* %27
%28 = load i32, i32* @ETH_P_ALL
%29 = call i8* @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 4
store i8* %29, i8** %30
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 3
store i32 %32, i32* %33
%34 = call i8* @htons(i32 1)
ret i32 %10
}