#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 /*<<< orphan*/ mr_type;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ mr_ifindex;
  int /*<<< orphan*/ sll_halen;
  int /*<<< orphan*/ sll_hatype;
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
struct packet_mreq {
  int /*<<< orphan*/ mr_type;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ mr_ifindex;
  int /*<<< orphan*/ sll_halen;
  int /*<<< orphan*/ sll_hatype;
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ sll_family;
};
typedef int /*<<< orphan*/ mreq;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ARPHRD_ETHER;
int /*<<< orphan*/ ETH_ALEN;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PACKET_ADD_MEMBERSHIP;
int /*<<< orphan*/ PACKET_MR_PROMISC;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_PACKET;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ lnx_eth_get_dev_index(char *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
               struct sockaddr_ll *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int lnx_eth_init_socket(char *device) {
  struct sockaddr_ll sa;
  struct packet_mreq mreq;
  int sck;
  if ((sck = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
    fprintf(stderr, "eth_init_socket: socket: %s\n", strerror(errno));
    return (-1);
  }
  memset(&sa, 0, sizeof(struct sockaddr_ll));
  sa.sll_family = AF_PACKET;
  sa.sll_protocol = htons(ETH_P_ALL);
  sa.sll_hatype = ARPHRD_ETHER;
  sa.sll_halen = ETH_ALEN;
  sa.sll_ifindex = lnx_eth_get_dev_index(device);
  memset(&mreq, 0, sizeof(mreq));
  mreq.mr_ifindex = sa.sll_ifindex;
  mreq.mr_type = PACKET_MR_PROMISC;
  if (bind(sck, (struct sockaddr *)&sa, sizeof(struct sockaddr_ll)) == -1) {
    fprintf(stderr, "eth_init_socket: bind: %s\n", strerror(errno));
    return (-1);
  }
  if (setsockopt(sck, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) ==
      -1) {
    fprintf(stderr, "eth_init_socket: setsockopt: %s\n", strerror(errno));
    return (-1);
  }
  return (sck);
}


x86 assembly (clang, -O0):
<lnx_eth_init_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%esi
mov    %eax,%edx
call   10a0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmp    $0xffffffff,%eax
jne    1203 <lnx_eth_init_socket+0x83>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xe0e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <lnx_eth_init_socket+0x1b3>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1060 <memset@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <lnx_eth_get_dev_index@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1060 <memset@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x54(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1c,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12c8 <lnx_eth_init_socket+0x148>
mov    0x2d09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xd66(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <lnx_eth_init_socket+0x1b3>
mov    -0x54(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
mov    0x2cb5(%rip),%rax
mov    (%rax),%edx
lea    -0x50(%rbp),%rcx
mov    $0x1c,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    132d <lnx_eth_init_socket+0x1ad>
mov    0x2ca4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xd1c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <lnx_eth_init_socket+0x1b3>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lnx_eth_init_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%r14
mov    0x2e4a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e31(%rip),%rax
mov    (%rax),%ebp
mov    0x2e08(%rip),%r12
mov    (%r12),%edi
call   1050 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     129c <lnx_eth_init_socket+0x11c>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1060 <memset@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    (%r12),%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1030 <lnx_eth_get_dev_index@plt>
mov    %eax,0x4(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1060 <memset@plt>
mov    0x4(%rsp),%eax
mov    %eax,0x28(%rsp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1c,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     12bc <lnx_eth_init_socket+0x13c>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rcx
mov    %ebx,%edi
mov    $0x1c,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12e9 <lnx_eth_init_socket+0x169>
mov    0x2d1d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd9e(%rip),%rsi
jmp    12da <lnx_eth_init_socket+0x15a>
mov    0x2cfd(%rip),%rax
mov    (%rax),%ebx
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd46(%rip),%rsi
jmp    12da <lnx_eth_init_socket+0x15a>
mov    0x2cdd(%rip),%rax
mov    (%rax),%ebx
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd43(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.packet_mreq = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"eth_init_socket: socket: %s\0A\00"
@errno = global i32 0
@AF_PACKET = global i32 0
@ARPHRD_ETHER = global i32 0
@ETH_ALEN = global i32 0
@PACKET_MR_PROMISC = global i32 0
@.str.1 = [27 x i8] c"eth_init_socket: bind: %s\0A\00"
@SOL_PACKET = global i32 0
@PACKET_ADD_MEMBERSHIP = global i32 0
@.str.2 = [33 x i8] c"eth_init_socket: setsockopt: %s\0A\00"
define i32 @lnx_eth_init_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_ll
%5 = alloca %struct.packet_mreq
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @PF_PACKET
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @ETH_P_ALL
%10 = call i32 @htons(i32 %9)
%11 = call i32 @socket(i32 %7, i32 %8, i32 %10)
store i32 %11, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %2
br label %62
18: ; preds = %1
%19 = call i32 @memset(%struct.sockaddr_ll* %4, i32 0, i32 28)
%20 = load i32, i32* @AF_PACKET
%21 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 6
store i32 %20, i32* %21
%22 = load i32, i32* @ETH_P_ALL
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 5
store i32 %23, i32* %24
%25 = load i32, i32* @ARPHRD_ETHER
%26 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 4
store i32 %25, i32* %26
%27 = load i32, i32* @ETH_ALEN
%28 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 3
store i32 %27, i32* %28
%29 = load i8*, i8** %3
%30 = call i32 @lnx_eth_get_dev_index(i8* %29)
%31 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 1
store i32 %30, i32* %31
%32 = bitcast %struct.packet_mreq* %5 to %struct.sockaddr_ll*
%33 = call i32 @memset(%struct.sockaddr_ll* %32, i32 0, i32 28)
%34 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %5, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i32, i32* @PACKET_MR_PROMISC
%38 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %5, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %6
%40 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%41 = call i32 @bind(i32 %39, %struct.sockaddr* %40, i32 28)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %18
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %46)
store i32 -1, i32* %2
br label %62
48: ; preds = %18
%49 = load i32, i32* %6
%50 = load i32, i32* @SOL_PACKET
%51 = load i32, i32* @PACKET_ADD_MEMBERSHIP
%52 = bitcast %struct.packet_mreq* %5 to %struct.sockaddr_ll*
%53 = call i32 @setsockopt(i32 %49, i32 %50, i32 %51, %struct.sockaddr_ll* %52, i32 28)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %48
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %58)
store i32 -1, i32* %2
br label %62
60: ; preds = %48
%61 = load i32, i32* %6
store i32 %61, i32* %2
br label %62
62: ; preds = %60, %55, %43, %13
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.packet_mreq = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"eth_init_socket: socket: %s\0A\00"
@errno = global i32 0
@AF_PACKET = global i32 0
@ARPHRD_ETHER = global i32 0
@ETH_ALEN = global i32 0
@PACKET_MR_PROMISC = global i32 0
@.str.1 = [27 x i8] c"eth_init_socket: bind: %s\0A\00"
@SOL_PACKET = global i32 0
@PACKET_ADD_MEMBERSHIP = global i32 0
@.str.2 = [33 x i8] c"eth_init_socket: setsockopt: %s\0A\00"
define i32 @lnx_eth_init_socket(i8* %0) {
%2 = alloca %struct.sockaddr_ll
%3 = alloca %struct.packet_mreq
%4 = bitcast %struct.sockaddr_ll* %2 to i8*
%5 = bitcast %struct.packet_mreq* %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 i32 @htons(i32 %8)
%10 = tail call i32 @socket(i32 %6, i32 %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %54
17: ; preds = %1
%18 = call i32 @memset(%struct.sockaddr_ll* %2, i32 0, i32 28)
%19 = load i32, i32* @AF_PACKET
%20 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 6
store i32 %19, i32* %20
%21 = load i32, i32* @ETH_P_ALL
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 5
store i32 %22, i32* %23
%24 = load i32, i32* @ARPHRD_ETHER
%25 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 4
store i32 %24, i32* %25
%26 = load i32, i32* @ETH_ALEN
%27 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 3
store i32 %26, i32* %27
%28 = call i32 @lnx_eth_get_dev_index(i8* %0)
%29 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 1
store i32 %28, i32* %29
%30 = bitcast %struct.packet_mreq* %3 to %struct.sockaddr_ll*
%31 = call i32 @memset(%struct.sockaddr_ll* %30, i32 0, i32 28)
%32 = load i32, i32* %29
%33 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* @PACKET_MR_PROMISC
%35 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %3, i64 0, i32 0
store i32 %34, i32* %35
%36 = bitcast %struct.sockaddr_ll* %2 to %struct.sockaddr*
%37 = call i32 @bind(i32 %10, %struct.sockaddr* %36, i32 28)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %17
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %42)
br label %54
44: ; preds = %17
%45 = load i32, i32* @SOL_PACKET
%46 = load i32, i32* @PACKET_ADD_MEMBERSHIP
%47 = call i32 @setsockopt(i32 %10, i32 %45, i32 %46, %struct.sockaddr_ll* %30, i32 28)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %44, %49, %39, %12
%55 = phi i32 [ -1, %12 ], [ -1, %39 ], [ -1, %49 ], [ %10, %44 ]
ret i32 %55
}