#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();
scalar_t__ init_ring_daddr(int, char *);
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);
  ;
  if (init_ring_daddr(fd, "eth0"))
    return NULL;
  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   1070 <getpagesize@plt>
shl    $0x7,%eax
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %eax,-0x18(%rbp)
movl   $0x80,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
lea    -0x20(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11d2 <init_packetsock_ring+0x82>
jmp    11b0 <init_packetsock_ring+0x60>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1267 <init_packetsock_ring+0x117>
mov    -0x20(%rbp),%esi
imul   -0x1c(%rbp),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
mov    0x2dee(%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    1239 <init_packetsock_ring+0xe9>
jmp    1217 <init_packetsock_ring+0xc7>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1267 <init_packetsock_ring+0x117>
mov    -0xc(%rbp),%edi
lea    0xdd8(%rip),%rsi
call   1050 <init_ring_daddr@plt>
cmp    $0x0,%rax
je     125f <init_packetsock_ring+0x10f>
movq   $0x0,-0x8(%rbp)
jmp    1267 <init_packetsock_ring+0x117>
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   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
xor    %eax,%eax
call   1070 <getpagesize@plt>
shl    $0x7,%eax
mov    %eax,(%rsp)
movl   $0x1,0x4(%rsp)
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %eax,0x8(%rsp)
movl   $0x80,0xc(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
mov    %ebx,%edi
mov    %ebp,%edx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11bd <init_packetsock_ring+0x6d>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
jmp    120f <init_packetsock_ring+0xbf>
mov    0x4(%rsp),%esi
imul   (%rsp),%esi
mov    0x2e2c(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e02(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     1218 <init_packetsock_ring+0xc8>
mov    %rax,%r14
lea    0xe1f(%rip),%rsi
mov    %ebx,%edi
call   1050 <init_ring_daddr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
cmove  %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
jmp    11b4 <init_packetsock_ring+0x64>

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"
@.str.2 = [5 x i8] c"eth0\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 %52
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 %52
44: ; No predecessors!
br label %45
45: ; preds = %44, %26
%46 = load i32, i32* %4
%47 = call i64 @init_ring_daddr(i32 %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
store i8* null, i8** %3
br label %52
50: ; preds = %45
%51 = load i8*, i8** %7
store i8* %51, i8** %3
br label %52
52: ; preds = %50, %49, %41, %22
%53 = load i8*, i8** %3
ret i8* %53
}

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"
@.str.2 = [5 x i8] c"eth0\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 %18, label %15
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %35
18: ; preds = %2
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = mul nsw i32 %20, %19
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %23, %22
%25 = load i32, i32* @MAP_SHARED
%26 = call i8* @mmap(i32 0, i32 %21, i32 %24, i32 %25, i32 %0, i32 0)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %18
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %35
31: ; preds = %18
%32 = call i64 @init_ring_daddr(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
%34 = select i1 %33, i8* %26, i8* null
br label %35
35: ; preds = %31, %28, %15
%36 = phi i8* [ null, %15 ], [ null, %28 ], [ %34, %31 ]
ret i8* %36
}