#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*/ PACKET_VERSION;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_PACKET;
int /*<<< orphan*/ exit(int);
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, 0);
  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    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1187 <pfsocket+0x47>
lea    0xe88(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%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    11ce <pfsocket+0x8e>
lea    0xe48(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pfsocket>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,0xc(%rsp)
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     119a <pfsocket+0x5a>
mov    %eax,%ebx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11a3 <pfsocket+0x63>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe5f(%rip),%rdi
jmp    11aa <pfsocket+0x6a>
lea    0xe5d(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@PF_PACKET = global i32 0
@SOCK_RAW = 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 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i32, i32* @SOL_PACKET
%16 = load i32, i32* @PACKET_VERSION
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %2, i32 4)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %13
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@PF_PACKET = global i32 0
@SOCK_RAW = 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 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = load i32, i32* @SOL_PACKET
%12 = load i32, i32* @PACKET_VERSION
%13 = call i32 @setsockopt(i32 %5, i32 %11, i32 %12, i32* %2, i32 4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
ret i32 %5
}