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

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int raw_socket(int ifindex) {
  int fd;
  struct sockaddr_ll sock;
  if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
    return -1;
  }
  sock.sll_family = AF_PACKET;
  sock.sll_protocol = htons(ETH_P_IP);
  sock.sll_ifindex = ifindex;
  if (bind(fd, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<raw_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1196 <raw_socket+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <raw_socket+0xae>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e1f(%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   1050 <bind@plt>
cmp    $0x0,%rax
jge    11e8 <raw_socket+0xa8>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <raw_socket+0xae>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raw_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e9c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e83(%rip),%rax
mov    (%rax),%ebp
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
call   1030 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11c3 <raw_socket+0x83>
mov    %eax,%ebx
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%r15),%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11bc <raw_socket+0x7c>
mov    %ebx,%ebp
jmp    11c3 <raw_socket+0x83>
mov    %ebx,%edi
call   1040 <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_DGRAM = global i32 0
@ETH_P_IP = global i32 0
@AF_PACKET = global i32 0
define i32 @raw_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_DGRAM
%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 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %30
13: ; preds = %1
%14 = load i32, i32* @AF_PACKET
%15 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @ETH_P_IP
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* %3
%20 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %4
%22 = bitcast %struct.sockaddr_ll* %5 to %struct.sockaddr*
%23 = call i64 @bind(i32 %21, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %13
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %2
br label %30
28: ; preds = %13
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %30
30: ; preds = %28, %25, %12
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_DGRAM = global i32 0
@ETH_P_IP = global i32 0
@AF_PACKET = global i32 0
define i32 @raw_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_DGRAM
%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 slt i32 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %1
%11 = load i32, i32* @AF_PACKET
%12 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @ETH_P_IP
%14 = tail call i32 @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 0
store i32 %0, i32* %16
%17 = bitcast %struct.sockaddr_ll* %2 to %struct.sockaddr*
%18 = call i64 @bind(i32 %8, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @close(i32 %8)
br label %22
22: ; preds = %10, %1, %20
%23 = phi i32 [ -1, %20 ], [ -1, %1 ], [ %8, %10 ]
ret i32 %23
}