#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 mr_ifindex;
  int /*<<< orphan*/ mr_type;
  int /*<<< orphan*/ sll_halen;
  int /*<<< orphan*/ sll_hatype;
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
struct packet_mreq {
  int sll_ifindex;
  int mr_ifindex;
  int /*<<< orphan*/ mr_type;
  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*/ 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 raw_init_socket(int dev_id) {
  struct sockaddr_ll sa;
  struct packet_mreq mreq;
  int sock;
  if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
    fprintf(stderr, "raw_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 = dev_id;
  memset(&mreq, 0, sizeof(mreq));
  mreq.mr_ifindex = sa.sll_ifindex;
  mreq.mr_type = PACKET_MR_PROMISC;
  if (bind(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_ll)) == -1) {
    fprintf(stderr, "raw_init_socket: bind: %s\n", strerror(errno));
    return (-1);
  }
  if (setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
                 sizeof(mreq)) == -1) {
    fprintf(stderr, "raw_init_socket: setsockopt: %s\n", strerror(errno));
    return (-1);
  }
  return (sock);
}


x86 assembly (clang, -O0):
<raw_init_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0xffffffff,%eax
jne    11f2 <raw_init_socket+0x82>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xe1f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <raw_init_socket+0x1ac>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x4c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1c,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    12b1 <raw_init_socket+0x141>
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xd7d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <raw_init_socket+0x1ac>
mov    -0x4c(%rbp),%edi
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
mov    0x2ccc(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x1c,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1316 <raw_init_socket+0x1a6>
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xd33(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <raw_init_socket+0x1ac>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raw_init_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edi,%r15d
mov    0x2e5a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e41(%rip),%rax
mov    (%rax),%ebp
mov    0x2e18(%rip),%r12
mov    (%r12),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1283 <raw_init_socket+0x113>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    (%r12),%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r15d,(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    (%rsp),%eax
mov    %eax,0x24(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1c,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     12a3 <raw_init_socket+0x133>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rcx
mov    %ebx,%edi
mov    $0x1c,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12d0 <raw_init_socket+0x160>
mov    0x2d36(%rip),%rax
mov    (%rax),%ebx
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdb7(%rip),%rsi
jmp    12c1 <raw_init_socket+0x151>
mov    0x2d16(%rip),%rax
mov    (%rax),%ebx
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd5f(%rip),%rsi
jmp    12c1 <raw_init_socket+0x151>
mov    0x2cf6(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd5c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <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"raw_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"raw_init_socket: bind: %s\0A\00"
@SOL_PACKET = global i32 0
@PACKET_ADD_MEMBERSHIP = global i32 0
@.str.2 = [33 x i8] c"raw_init_socket: setsockopt: %s\0A\00"
define i32 @raw_init_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_ll
%5 = alloca %struct.packet_mreq
%6 = alloca i32
store i32 %0, i32* %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 %61
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 i32, i32* %3
%30 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 0
store i32 %29, i32* %30
%31 = bitcast %struct.packet_mreq* %5 to %struct.sockaddr_ll*
%32 = call i32 @memset(%struct.sockaddr_ll* %31, i32 0, i32 28)
%33 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %5, i32 0, i32 1
store i32 %34, i32* %35
%36 = load i32, i32* @PACKET_MR_PROMISC
%37 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %5, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%40 = call i32 @bind(i32 %38, %struct.sockaddr* %39, i32 28)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %18
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %45)
store i32 -1, i32* %2
br label %61
47: ; preds = %18
%48 = load i32, i32* %6
%49 = load i32, i32* @SOL_PACKET
%50 = load i32, i32* @PACKET_ADD_MEMBERSHIP
%51 = bitcast %struct.packet_mreq* %5 to %struct.sockaddr_ll*
%52 = call i32 @setsockopt(i32 %48, i32 %49, i32 %50, %struct.sockaddr_ll* %51, i32 28)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %59
54: ; preds = %47
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %57)
store i32 -1, i32* %2
br label %61
59: ; preds = %47
%60 = load i32, i32* %6
store i32 %60, i32* %2
br label %61
61: ; preds = %59, %54, %42, %13
%62 = load i32, i32* %2
ret i32 %62
}

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"raw_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"raw_init_socket: bind: %s\0A\00"
@SOL_PACKET = global i32 0
@PACKET_ADD_MEMBERSHIP = global i32 0
@.str.2 = [33 x i8] c"raw_init_socket: setsockopt: %s\0A\00"
define i32 @raw_init_socket(i32 %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 %53
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 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 0
store i32 %0, i32* %28
%29 = bitcast %struct.packet_mreq* %3 to %struct.sockaddr_ll*
%30 = call i32 @memset(%struct.sockaddr_ll* %29, i32 0, i32 28)
%31 = load i32, i32* %28
%32 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %3, i64 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @PACKET_MR_PROMISC
%34 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %3, i64 0, i32 2
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_ll* %2 to %struct.sockaddr*
%36 = call i32 @bind(i32 %10, %struct.sockaddr* %35, i32 28)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %43
38: ; preds = %17
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %53
43: ; preds = %17
%44 = load i32, i32* @SOL_PACKET
%45 = load i32, i32* @PACKET_ADD_MEMBERSHIP
%46 = call i32 @setsockopt(i32 %10, i32 %44, i32 %45, %struct.sockaddr_ll* %29, i32 28)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %43, %48, %38, %12
%54 = phi i32 [ -1, %12 ], [ -1, %38 ], [ -1, %48 ], [ %10, %43 ]
ret i32 %54
}