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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SOL_PACKET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpagesize();
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int, void *, int);
int /*<<< orphan*/ stderr;
char *init_packetsock_ring(int fd, int ringtype) {
  struct tpacket_req tp;
  char *ring;
  tp.tp_block_size = 128 * getpagesize();
  tp.tp_block_nr = 1;
  tp.tp_frame_size = getpagesize();
  tp.tp_frame_nr = 128;
  if (setsockopt(fd, SOL_PACKET, ringtype, (void *)&tp, sizeof(tp)))
    do {
      fprintf(stderr, "setsockopt() ring\n");
      return NULL;
    } while (0);
  ;
  ring = mmap(0, tp.tp_block_size * tp.tp_block_nr, PROT_READ | PROT_WRITE,
              MAP_SHARED, fd, 0);
  if (!ring)
    do {
      fprintf(stderr, "mmap()\n");
      return NULL;
    } while (0);
  ;
  return ring;
}


x86 assembly (clang, -O0):
<init_packetsock_ring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
shl    $0x7,%eax
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %eax,-0x18(%rbp)
movl   $0x80,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
lea    -0x20(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11c2 <init_packetsock_ring+0x82>
jmp    11a0 <init_packetsock_ring+0x60>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1231 <init_packetsock_ring+0xf1>
mov    -0x20(%rbp),%esi
imul   -0x1c(%rbp),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
mov    0x2de7(%rip),%rax
or     (%rax),%edx
mov    0x2dfe(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1229 <init_packetsock_ring+0xe9>
jmp    1207 <init_packetsock_ring+0xc7>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1231 <init_packetsock_ring+0xf1>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_packetsock_ring>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
xor    %eax,%eax
call   1060 <getpagesize@plt>
shl    $0x7,%eax
mov    %eax,0x8(%rsp)
movl   $0x1,0xc(%rsp)
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %eax,0x10(%rsp)
movl   $0x80,0x14(%rsp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    %ebp,%edx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11ae <init_packetsock_ring+0x6e>
lea    0xe64(%rip),%rsi
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1050 <fprintf@plt>
xor    %eax,%eax
jmp    11e4 <init_packetsock_ring+0xa4>
mov    0xc(%rsp),%esi
imul   0x8(%rsp),%esi
mov    0x2e3a(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e10(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     11eb <init_packetsock_ring+0xab>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe21(%rip),%rsi
jmp    119c <init_packetsock_ring+0x5c>

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

LLVM IR (-O2):
%struct.tpacket_req = type { i32, i32, i32, i32 }
@SOL_PACKET = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"setsockopt() ring\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [8 x i8] c"mmap()\0A\00"
define i8* @init_packetsock_ring(i32 %0, i32 %1) {
%3 = alloca %struct.tpacket_req
%4 = bitcast %struct.tpacket_req* %3 to i8*
%5 = tail call i32 (...) @getpagesize()
%6 = shl nsw i32 %5, 7
%7 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i64 0, i32 0
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i64 0, i32 1
store i32 1, i32* %8
%9 = tail call i32 (...) @getpagesize()
%10 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i64 0, i32 3
store i32 128, i32* %11
%12 = load i32, i32* @SOL_PACKET
%13 = call i64 @setsockopt(i32 %0, i32 %12, i32 %1, i8* %4, i32 16)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %2
%16 = load i32, i32* %7
%17 = load i32, i32* %8
%18 = mul nsw i32 %17, %16
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_SHARED
%23 = call i8* @mmap(i32 0, i32 %18, i32 %21, i32 %22, i32 %0, i32 0)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %15, %2
%26 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %15 ]
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* %26)
br label %29
29: ; preds = %25, %15
%30 = phi i8* [ %23, %15 ], [ null, %25 ]
ret i8* %30
}