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

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sendraw_socket() {
  int s;
  struct sockaddr_ll mysocket;
  s = socket(AF_PACKET, SOCK_RAW, htons(0x0003));
  if (s < 0) {
    perror("socket");
    return -1;
  }
  memset(&mysocket, 0, sizeof(struct sockaddr));
  mysocket.sll_family = AF_PACKET;
  mysocket.sll_protocol = htons(0x0003);
  mysocket.sll_ifindex = 0;
  if (bind(s, (struct sockaddr *)&mysocket, sizeof(mysocket)) < 0) {
    perror("bind");
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<sendraw_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x3,%edi
call   1030 <htons@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11bc <sendraw_socket+0x5c>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <sendraw_socket+0xce>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x3,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1228 <sendraw_socket+0xc8>
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <sendraw_socket+0xce>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendraw_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e69(%rip),%r14
mov    (%r14),%ebx
mov    0x2e6f(%rip),%rax
mov    (%rax),%ebp
mov    $0x3,%edi
call   1030 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11e4 <sendraw_socket+0x84>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    $0x3,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11f2 <sendraw_socket+0x92>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe15(%rip),%rdi
call   1070 <perror@plt>
jmp    1205 <sendraw_socket+0xa5>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    11d9 <sendraw_socket+0x79>

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

LLVM IR (-O2):
%struct.sockaddr_ll = type { i64, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
define i32 @sendraw_socket() {
%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 @htons(i32 3)
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %0
%11 = call i32 @memset(%struct.sockaddr_ll* %1, i32 0, i32 4)
%12 = load i32, i32* @AF_PACKET
%13 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %1, i64 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 3)
%15 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %1, i64 0, i32 0
store i64 0, i64* %16
%17 = bitcast %struct.sockaddr_ll* %1 to %struct.sockaddr*
%18 = call i64 @bind(i32 %6, %struct.sockaddr* %17, i32 16)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @close(i32 %6)
br label %23
23: ; preds = %10, %20, %8
%24 = phi i32 [ -1, %8 ], [ -1, %20 ], [ %6, %10 ]
ret i32 %24
}