#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 {
  scalar_t__ sll_ifindex;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int psock_create(void) {
  int sd;
  struct sockaddr_ll addr;
  int addrlen = sizeof(addr);
  sd = socket(AF_PACKET, SOCK_RAW, 0);
  if (sd < 0) {
    perror("ERROR: failed to create packet socket");
    return -1;
  }
  addr.sll_family = AF_PACKET;
  addr.sll_ifindex = 0;
  if (bind(sd, (const struct sockaddr *)&addr, addrlen) < 0) {
    perror("ERROR: binding socket failed");
    close(sd);
    return -1;
  }
  return sd;
}


x86 assembly (clang, -O0):
<psock_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x10,-0x1c(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118d <psock_create+0x4d>
lea    0xe84(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <psock_create+0xa0>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
jge    11da <psock_create+0x9a>
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <psock_create+0xa0>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psock_create>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e8a(%rip),%r14
mov    (%r14),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     1197 <psock_create+0x57>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1040 <bind@plt>
test   %rax,%rax
js     11a5 <psock_create+0x65>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe62(%rip),%rdi
call   1050 <perror@plt>
jmp    11b8 <psock_create+0x78>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
jmp    118d <psock_create+0x4d>

LLVM IR (-O0):
%struct.sockaddr_ll = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str = [38 x i8] c"ERROR: failed to create packet socket\00"
@.str.1 = [29 x i8] c"ERROR: binding socket failed\00"
define i32 @psock_create() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_ll
%4 = alloca i32
store i32 16, i32* %4
%5 = load i32, i32* @AF_PACKET
%6 = load i32, i32* @SOCK_RAW
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
12: ; preds = %0
%13 = load i32, i32* @AF_PACKET
%14 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i32 0, i32 0
store i64 0, i64* %15
%16 = load i32, i32* %2
%17 = bitcast %struct.sockaddr_ll* %3 to %struct.sockaddr*
%18 = load i32, i32* %4
%19 = call i64 @bind(i32 %16, %struct.sockaddr* %17, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %1
br label %27
25: ; preds = %12
%26 = load i32, i32* %2
store i32 %26, i32* %1
br label %27
27: ; preds = %25, %21, %10
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str = [38 x i8] c"ERROR: failed to create packet socket\00"
@.str.1 = [29 x i8] c"ERROR: binding socket failed\00"
define i32 @psock_create() {
%1 = alloca %struct.sockaddr_ll
%2 = bitcast %struct.sockaddr_ll* %1 to i8*
%3 = load i32, i32* @AF_PACKET
%4 = load i32, i32* @SOCK_RAW
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %19
9: ; preds = %0
%10 = load i32, i32* @AF_PACKET
%11 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %1, i64 0, i32 0
store i64 0, i64* %12
%13 = bitcast %struct.sockaddr_ll* %1 to %struct.sockaddr*
%14 = call i64 @bind(i32 %5, %struct.sockaddr* %13, i32 16)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @close(i32 %5)
br label %19
19: ; preds = %9, %16, %7
%20 = phi i32 [ -1, %7 ], [ -1, %16 ], [ %5, %9 ]
ret i32 %20
}