#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;
struct tpacket_req {
  int tp_block_size;
  int tp_frame_size;
  int tp_block_nr;
  int tp_frame_nr;
};
typedef int /*<<< orphan*/ req;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PACKET_RX_RING;
int /*<<< orphan*/ PACKET_VERSION;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SOL_PACKET;
int TPACKET_V2;
int /*<<< orphan*/ exit(int);
int getpagesize();
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
char *sock_fanout_open_ring(int fd) {
  struct tpacket_req req = {
      .tp_block_size = getpagesize(),
      .tp_frame_size = getpagesize(),
      .tp_block_nr = 20,
      .tp_frame_nr = 20,
  };
  char *ring;
  int val = TPACKET_V2;
  if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *)&val, sizeof(val))) {
    perror("packetsock ring setsockopt version");
    exit(1);
  }
  if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *)&req, sizeof(req))) {
    perror("packetsock ring setsockopt");
    exit(1);
  }
  ring = mmap(0, req.tp_block_size * req.tp_block_nr, PROT_READ | PROT_WRITE,
              MAP_SHARED, fd, 0);
  if (ring == MAP_FAILED) {
    perror("packetsock ring mmap");
    exit(1);
  }
  return ring;
}


x86 assembly (clang, -O0):
<sock_fanout_open_ring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %eax,-0x14(%rbp)
movl   $0x14,-0x10(%rbp)
movl   $0x14,-0xc(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11cd <sock_fanout_open_ring+0x7d>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1211 <sock_fanout_open_ring+0xc1>
lea    0xe21(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%esi
imul   -0x10(%rbp),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    0x2d88(%rip),%rax
or     (%rax),%edx
mov    0x2daf(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d57(%rip),%rcx
cmp    (%rcx),%rax
jne    1270 <sock_fanout_open_ring+0x120>
lea    0xddd(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_fanout_open_ring>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %eax,0xc(%rsp)
movabs $0x1400000014,%rax
mov    %rax,0x10(%rsp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2e56(%rip),%r14
mov    (%r14),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    121d <sock_fanout_open_ring+0xcd>
mov    (%r14),%esi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    1226 <sock_fanout_open_ring+0xd6>
mov    0x10(%rsp),%esi
imul   0x8(%rsp),%esi
mov    0x2e10(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de6(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
je     122f <sock_fanout_open_ring+0xdf>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xddc(%rip),%rdi
jmp    1236 <sock_fanout_open_ring+0xe6>
lea    0xdf6(%rip),%rdi
jmp    1236 <sock_fanout_open_ring+0xe6>
lea    0xe08(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.tpacket_req = type { i32, i32, i32, i32 }
@TPACKET_V2 = global i32 0
@SOL_PACKET = global i32 0
@PACKET_VERSION = global i32 0
@.str = [35 x i8] c"packetsock ring setsockopt version\00"
@PACKET_RX_RING = global i32 0
@.str.1 = [27 x i8] c"packetsock ring setsockopt\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [21 x i8] c"packetsock ring mmap\00"
define i8* @sock_fanout_open_ring(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.tpacket_req
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i32 0, i32 0
%7 = call i32 (...) @getpagesize()
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i32 0, i32 1
%9 = call i32 (...) @getpagesize()
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i32 0, i32 2
store i32 20, i32* %10
%11 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i32 0, i32 3
store i32 20, i32* %11
%12 = load i32, i32* @TPACKET_V2
store i32 %12, i32* %5
%13 = load i32, i32* %2
%14 = load i32, i32* @SOL_PACKET
%15 = load i32, i32* @PACKET_VERSION
%16 = bitcast i32* %5 to i8*
%17 = call i64 @setsockopt(i32 %13, i32 %14, i32 %15, i8* %16, i32 4)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %1
%23 = load i32, i32* %2
%24 = load i32, i32* @SOL_PACKET
%25 = load i32, i32* @PACKET_RX_RING
%26 = bitcast %struct.tpacket_req* %3 to i8*
%27 = call i64 @setsockopt(i32 %23, i32 %24, i32 %25, i8* %26, i32 16)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %22
%33 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i32 0, i32 2
%36 = load i32, i32* %35
%37 = mul nsw i32 %34, %36
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @PROT_WRITE
%40 = or i32 %38, %39
%41 = load i32, i32* @MAP_SHARED
%42 = load i32, i32* %2
%43 = call i8* @mmap(i32 0, i32 %37, i32 %40, i32 %41, i32 %42, i32 0)
store i8* %43, i8** %4
%44 = load i8*, i8** %4
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %44, %45
br i1 %46, label %47, label %50
47: ; preds = %32
%48 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %32
%51 = load i8*, i8** %4
ret i8* %51
}

LLVM IR (-O2):
%struct.tpacket_req = type { i32, i32, i32, i32 }
@TPACKET_V2 = global i32 0
@SOL_PACKET = global i32 0
@PACKET_VERSION = global i32 0
@.str = [35 x i8] c"packetsock ring setsockopt version\00"
@PACKET_RX_RING = global i32 0
@.str.1 = [27 x i8] c"packetsock ring setsockopt\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [21 x i8] c"packetsock ring mmap\00"
define i8* @sock_fanout_open_ring(i32 %0) {
%2 = alloca %struct.tpacket_req
%3 = alloca i32
%4 = bitcast %struct.tpacket_req* %2 to i8*
%5 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %2, i64 0, i32 0
%6 = tail call i32 (...) @getpagesize()
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %2, i64 0, i32 1
%8 = tail call i32 (...) @getpagesize()
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %2, i64 0, i32 2
store i32 20, i32* %9
%10 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %2, i64 0, i32 3
store i32 20, i32* %10
%11 = bitcast i32* %3 to i8*
%12 = load i32, i32* @TPACKET_V2
store i32 %12, i32* %3
%13 = load i32, i32* @SOL_PACKET
%14 = load i32, i32* @PACKET_VERSION
%15 = call i64 @setsockopt(i32 %0, i32 %13, i32 %14, i8* %11, i32 4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = load i32, i32* @SOL_PACKET
%22 = load i32, i32* @PACKET_RX_RING
%23 = call i64 @setsockopt(i32 %0, i32 %21, i32 %22, i8* %4, i32 16)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = load i32, i32* %5
%30 = load i32, i32* %9
%31 = mul nsw i32 %30, %29
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %33, %32
%35 = load i32, i32* @MAP_SHARED
%36 = call i8* @mmap(i32 0, i32 %31, i32 %34, i32 %35, i32 %0, i32 0)
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %42
39: ; preds = %28
%40 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %28
ret i8* %36
}