#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*/ val;
typedef int uint16_t;

/* Variables and functions */
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ PACKET_FANOUT;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_PACKET;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ pair_udp_setfilter(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sock_fanout_open(uint16_t typeflags, uint16_t group_id) {
  int fd, val;
  fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  if (fd < 0) {
    perror("socket packet");
    exit(1);
  }
  val = (((int)typeflags) << 16) | group_id;
  if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
    if (close(fd)) {
      perror("close packet");
      exit(1);
    }
    return -1;
  }
  pair_udp_setfilter(fd);
  return fd;
}


x86 assembly (clang, -O0):
<sock_fanout_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11d4 <sock_fanout_open+0x64>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
shl    $0x10,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1242 <sock_fanout_open+0xd2>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     1236 <sock_fanout_open+0xc6>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <sock_fanout_open+0xe0>
mov    -0x10(%rbp),%edi
call   1050 <pair_udp_setfilter@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_fanout_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e65(%rip),%rax
mov    (%rax),%r15d
mov    0x2e4b(%rip),%rax
mov    (%rax),%ebx
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %r15d,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1210 <sock_fanout_open+0xa0>
mov    %eax,%ebx
shl    $0x10,%ebp
or     %r14d,%ebp
mov    %ebp,0x4(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11fc <sock_fanout_open+0x8c>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1203 <sock_fanout_open+0x93>
lea    0xe14(%rip),%rdi
jmp    1217 <sock_fanout_open+0xa7>
mov    %ebx,%edi
call   1050 <pair_udp_setfilter@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@.str = [14 x i8] c"socket packet\00"
@SOL_PACKET = global i32 0
@PACKET_FANOUT = global i32 0
@.str.1 = [13 x i8] c"close packet\00"
define i32 @sock_fanout_open(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* @PF_PACKET
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @ETH_P_IP
%11 = call i32 @htons(i32 %10)
%12 = call i32 @socket(i32 %8, i32 %9, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %4
%20 = shl i32 %19, 16
%21 = load i32, i32* %5
%22 = or i32 %20, %21
store i32 %22, i32* %7
%23 = load i32, i32* %6
%24 = load i32, i32* @SOL_PACKET
%25 = load i32, i32* @PACKET_FANOUT
%26 = call i64 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %7, i32 4)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %18
%29 = load i32, i32* %6
%30 = call i64 @close(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
store i32 -1, i32* %3
br label %40
36: ; preds = %18
%37 = load i32, i32* %6
%38 = call i32 @pair_udp_setfilter(i32 %37)
%39 = load i32, i32* %6
store i32 %39, i32* %3
br label %40
40: ; preds = %36, %35
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@.str = [14 x i8] c"socket packet\00"
@SOL_PACKET = global i32 0
@PACKET_FANOUT = global i32 0
@.str.1 = [13 x i8] c"close packet\00"
define i32 @sock_fanout_open(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @PF_PACKET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @ETH_P_IP
%8 = tail call i32 @htons(i32 %7)
%9 = tail call i32 @socket(i32 %5, i32 %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = shl i32 %0, 16
%16 = or i32 %15, %1
store i32 %16, i32* %3
%17 = load i32, i32* @SOL_PACKET
%18 = load i32, i32* @PACKET_FANOUT
%19 = call i64 @setsockopt(i32 %9, i32 %17, i32 %18, i32* %3, i32 4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %14
%22 = call i64 @close(i32 %9)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %14
%28 = call i32 @pair_udp_setfilter(i32 %9)
br label %29
29: ; preds = %21, %27
%30 = phi i32 [ %9, %27 ], [ -1, %21 ]
ret i32 %30
}