#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*/ sll;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int l2_socket(int ifidx) {
  int s;
  struct sockaddr_ll sll;
  s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  if (s == -1) {
    return -1;
  }
  memset(&sll, 0xff, sizeof(sll));
  sll.sll_family = AF_PACKET;
  sll.sll_protocol = htons(ETH_P_ALL);
  sll.sll_ifindex = ifidx;
  if (bind(s, (struct sockaddr *)&sll, sizeof sll) == -1) {
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<l2_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a7 <l2_socket+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <l2_socket+0xc1>
lea    -0x18(%rbp),%rdi
mov    $0xff,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    120b <l2_socket+0xbb>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <l2_socket+0xc1>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<l2_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e8c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e73(%rip),%rax
mov    (%rax),%ebp
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11ee <l2_socket+0x9e>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xff,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11e7 <l2_socket+0x97>
mov    %ebx,%ebp
jmp    11ee <l2_socket+0x9e>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@AF_PACKET = global i32 0
@ETH_P_ALL = global i32 0
define i32 @l2_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_ll
store i32 %0, i32* %3
%6 = load i32, i32* @PF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @ETH_P_IP
%9 = call i32 @htons(i32 %8)
%10 = call i32 @socket(i32 %6, i32 %7, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %32
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_ll* %5, i32 255, i32 12)
%16 = load i32, i32* @AF_PACKET
%17 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @ETH_P_ALL
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* %3
%22 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %4
%24 = bitcast %struct.sockaddr_ll* %5 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %14
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %2
br label %32
30: ; preds = %14
%31 = load i32, i32* %4
store i32 %31, i32* %2
br label %32
32: ; preds = %30, %27, %13
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@AF_PACKET = global i32 0
@ETH_P_ALL = global i32 0
define i32 @l2_socket(i32 %0) {
%2 = alloca %struct.sockaddr_ll
%3 = bitcast %struct.sockaddr_ll* %2 to i8*
%4 = load i32, i32* @PF_PACKET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @ETH_P_IP
%7 = tail call i32 @htons(i32 %6)
%8 = tail call i32 @socket(i32 %4, i32 %5, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %23, label %10
10: ; preds = %1
%11 = call i32 @memset(%struct.sockaddr_ll* %2, i32 255, i32 12)
%12 = load i32, i32* @AF_PACKET
%13 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @ETH_P_ALL
%15 = call i32 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 0
store i32 %0, i32* %17
%18 = bitcast %struct.sockaddr_ll* %2 to %struct.sockaddr*
%19 = call i32 @bind(i32 %8, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = call i32 @close(i32 %8)
br label %23
23: ; preds = %10, %1, %21
%24 = phi i32 [ -1, %21 ], [ -1, %1 ], [ %8, %10 ]
ret i32 %24
}