#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 scalar_t__ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ PACKET_FANOUT;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_DGRAM;
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, int num_packets) {
  int fd, val;
  fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  if (fd < 0) {
    perror("socket packet");
    exit(1);
  }
  val = ((int)typeflags) << 16;
  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    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11d5 <sock_fanout_open+0x65>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
shl    $0x10,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1241 <sock_fanout_open+0xd1>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     1235 <sock_fanout_open+0xc5>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <sock_fanout_open+0xdf>
mov    -0x18(%rbp),%edi
call   1050 <pair_udp_setfilter@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_fanout_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e66(%rip),%rax
mov    (%rax),%ebx
mov    0x2e45(%rip),%rax
mov    (%rax),%ebp
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     120a <sock_fanout_open+0x9a>
mov    %eax,%ebx
shl    $0x10,%r14d
mov    %r14d,0xc(%rsp)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11f8 <sock_fanout_open+0x88>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11ff <sock_fanout_open+0x8f>
lea    0xe18(%rip),%rdi
jmp    1211 <sock_fanout_open+0xa1>
mov    %ebx,%edi
call   1050 <pair_udp_setfilter@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PF_PACKET = global i32 0
@SOCK_DGRAM = 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(i64 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
%8 = load i32, i32* @PF_PACKET
%9 = load i32, i32* @SOCK_DGRAM
%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 i64, i64* %4
%20 = trunc i64 %19 to i32
%21 = shl i32 %20, 16
store i32 %21, i32* %7
%22 = load i32, i32* %6
%23 = load i32, i32* @SOL_PACKET
%24 = load i32, i32* @PACKET_FANOUT
%25 = call i64 @setsockopt(i32 %22, i32 %23, i32 %24, i32* %7, i32 4)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %18
%28 = load i32, i32* %6
%29 = call i64 @close(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
store i32 -1, i32* %3
br label %39
35: ; preds = %18
%36 = load i32, i32* %6
%37 = call i32 @pair_udp_setfilter(i32 %36)
%38 = load i32, i32* %6
store i32 %38, i32* %3
br label %39
39: ; preds = %35, %34
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@PF_PACKET = global i32 0
@SOCK_DGRAM = 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(i64 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @PF_PACKET
%6 = load i32, i32* @SOCK_DGRAM
%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 = trunc i64 %0 to i32
%16 = shl i32 %15, 16
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
}