#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 */
typedef int /*<<< orphan*/ ver;

/* Variables and functions */
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PACKET_VERSION;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_PACKET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int pfsocket(int ver) {
  int ret, sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sock == -1) {
    perror("socket");
    exit(1);
  }
  ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver));
  if (ret == -1) {
    perror("setsockopt");
    exit(1);
  }
  return sock;
}


x86 assembly (clang, -O0):
<pfsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11b1 <pfsocket+0x61>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
lea    -0x4(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f8 <pfsocket+0xa8>
lea    0xe1e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pfsocket>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,0x4(%rsp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e69(%rip),%rax
mov    (%rax),%ebp
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11bb <pfsocket+0x6b>
mov    %eax,%ebx
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11c4 <pfsocket+0x74>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe3e(%rip),%rdi
jmp    11cb <pfsocket+0x7b>
lea    0xe3c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_PACKET = global i32 0
@PACKET_VERSION = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
define i32 @pfsocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @PF_PACKET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @ETH_P_ALL
%8 = call i32 @htons(i32 %7)
%9 = call i32 @socket(i32 %5, i32 %6, i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i32, i32* @SOL_PACKET
%18 = load i32, i32* @PACKET_VERSION
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %2, i32 4)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_PACKET = global i32 0
@PACKET_VERSION = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
define i32 @pfsocket(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* @PF_PACKET
%4 = load i32, i32* @SOCK_RAW
%5 = load i32, i32* @ETH_P_ALL
%6 = tail call i32 @htons(i32 %5)
%7 = tail call i32 @socket(i32 %3, i32 %4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i32, i32* @SOL_PACKET
%14 = load i32, i32* @PACKET_VERSION
%15 = call i32 @setsockopt(i32 %7, i32 %13, i32 %14, i32* %2, i32 4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %12
ret i32 %7
}