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

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PACKET_TX_RING;
int /*<<< orphan*/ PF_PACKET;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_PACKET;
scalar_t__ bind(int, void *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
void *getpagesize();
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ if_nametoindex(char *);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_tx(char **ring) {
  struct sockaddr_ll laddr = {};
  struct tpacket_req req = {};
  int fdt;
  fdt = socket(PF_PACKET, SOCK_RAW, 0);
  if (fdt == -1)
    error(1, errno, "socket t");
  laddr.sll_family = AF_PACKET;
  laddr.sll_protocol = htons(0);
  laddr.sll_ifindex = if_nametoindex("lo");
  if (!laddr.sll_ifindex)
    error(1, errno, "if_nametoindex");
  if (bind(fdt, (void *)&laddr, sizeof(laddr)))
    error(1, errno, "bind fdt");
  req.tp_block_size = getpagesize();
  req.tp_block_nr = 1;
  req.tp_frame_size = getpagesize();
  req.tp_frame_nr = 1;
  if (setsockopt(fdt, SOL_PACKET, PACKET_TX_RING, (void *)&req, sizeof(req)))
    error(1, errno, "setsockopt ring");
  *ring = mmap(0, req.tp_block_size * req.tp_block_nr, PROT_READ | PROT_WRITE,
               MAP_SHARED, fdt, 0);
  if (*ring == MAP_FAILED)
    error(1, errno, "mmap");
  return fdt;
}


x86 assembly (clang, -O0):
<setup_tx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11fc <setup_tx+0x6c>
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe09(%rip),%rdx
call   1090 <error@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    0xdf0(%rip),%rdi
call   1070 <if_nametoindex@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1245 <setup_tx+0xb5>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdcc(%rip),%rdx
call   1090 <error@plt>
mov    -0x34(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     127a <setup_tx+0xea>
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xda6(%rip),%rdx
call   1090 <error@plt>
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %eax,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
mov    0x2d00(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x18,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12e5 <setup_tx+0x155>
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xd44(%rip),%rdx
call   1090 <error@plt>
mov    -0x30(%rbp),%esi
imul   -0x2c(%rbp),%esi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
mov    0x2ca4(%rip),%rax
or     (%rax),%edx
mov    0x2cd3(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2c6a(%rip),%rcx
cmp    (%rcx),%rax
jne    1351 <setup_tx+0x1c1>
mov    0x2c6a(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xce8(%rip),%rdx
call   1090 <error@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_tx>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11d4 <setup_tx+0x54>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xor    %edi,%edi
call   1050 <htons@plt>
mov    %eax,0x1c(%rsp)
lea    0xe16(%rip),%rdi
call   1060 <if_nametoindex@plt>
mov    %eax,0x18(%rsp)
test   %eax,%eax
jne    121a <setup_tx+0x9a>
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    0xdfc(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     124a <setup_tx+0xca>
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
lea    0xddb(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %eax,(%rsp)
movl   $0x1,0x4(%rsp)
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %rax,0x10(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x18,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     12b1 <setup_tx+0x131>
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
lea    0xd7d(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
mov    0x4(%rsp),%esi
imul   (%rsp),%esi
mov    0x2d38(%rip),%rax
mov    0x2cd9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d06(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,(%r14)
mov    0x2cad(%rip),%rcx
cmp    (%rcx),%rax
jne    130a <setup_tx+0x18a>
mov    0x2cb1(%rip),%rax
mov    (%rax),%esi
lea    0xd34(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.tpacket_req = type { i32, i32, i32, i8* }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"socket t\00"
@AF_PACKET = global i32 0
@.str.1 = [3 x i8] c"lo\00"
@.str.2 = [15 x i8] c"if_nametoindex\00"
@.str.3 = [9 x i8] c"bind fdt\00"
@SOL_PACKET = global i32 0
@PACKET_TX_RING = global i32 0
@.str.4 = [16 x i8] c"setsockopt ring\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [5 x i8] c"mmap\00"
define i32 @setup_tx(i8** %0) {
%2 = alloca i8**
%3 = alloca %struct.sockaddr_ll
%4 = alloca %struct.tpacket_req
%5 = alloca i32
store i8** %0, i8*** %2
%6 = bitcast %struct.sockaddr_ll* %3 to i8*
%7 = bitcast %struct.tpacket_req* %4 to i8*
%8 = load i32, i32* @PF_PACKET
%9 = load i32, i32* @SOCK_RAW
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = call i32 @error(i32 1, i32 %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %1
%17 = load i32, i32* @AF_PACKET
%18 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 0)
%20 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i32 0, i32 1
store i32 %19, i32* %20
%21 = call i32 @if_nametoindex(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i32 0, i32 0
%24 = load i32, i32* %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %16
%27 = load i32, i32* @errno
%28 = call i32 @error(i32 1, i32 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %26, %16
%30 = load i32, i32* %5
%31 = bitcast %struct.sockaddr_ll* %3 to i8*
%32 = call i64 @bind(i32 %30, i8* %31, i32 12)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = call i32 @error(i32 1, i32 %35, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %34, %29
%38 = call i8* (...) @getpagesize()
%39 = ptrtoint i8* %38 to i32
%40 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %4, i32 0, i32 0
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %4, i32 0, i32 1
store i32 1, i32* %41
%42 = call i8* (...) @getpagesize()
%43 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %4, i32 0, i32 3
store i8* %42, i8** %43
%44 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %4, i32 0, i32 2
store i32 1, i32* %44
%45 = load i32, i32* %5
%46 = load i32, i32* @SOL_PACKET
%47 = load i32, i32* @PACKET_TX_RING
%48 = bitcast %struct.tpacket_req* %4 to i8*
%49 = call i64 @setsockopt(i32 %45, i32 %46, i32 %47, i8* %48, i32 24)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %37
%52 = load i32, i32* @errno
%53 = call i32 @error(i32 1, i32 %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %51, %37
%55 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %4, i32 0, i32 0
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %4, i32 0, i32 1
%58 = load i32, i32* %57
%59 = mul nsw i32 %56, %58
%60 = load i32, i32* @PROT_READ
%61 = load i32, i32* @PROT_WRITE
%62 = or i32 %60, %61
%63 = load i32, i32* @MAP_SHARED
%64 = load i32, i32* %5
%65 = call i8* @mmap(i32 0, i32 %59, i32 %62, i32 %63, i32 %64, i32 0)
%66 = load i8**, i8*** %2
store i8* %65, i8** %66
%67 = load i8**, i8*** %2
%68 = load i8*, i8** %67
%69 = load i8*, i8** @MAP_FAILED
%70 = icmp eq i8* %68, %69
br i1 %70, label %71, label %74
71: ; preds = %54
%72 = load i32, i32* @errno
%73 = call i32 @error(i32 1, i32 %72, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %74
74: ; preds = %71, %54
%75 = load i32, i32* %5
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.tpacket_req = type { i32, i32, i32, i8* }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"socket t\00"
@AF_PACKET = global i32 0
@.str.1 = [3 x i8] c"lo\00"
@.str.2 = [15 x i8] c"if_nametoindex\00"
@.str.3 = [9 x i8] c"bind fdt\00"
@SOL_PACKET = global i32 0
@PACKET_TX_RING = global i32 0
@.str.4 = [16 x i8] c"setsockopt ring\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [5 x i8] c"mmap\00"
define i32 @setup_tx(i8** nocapture writeonly %0) {
%2 = alloca %struct.sockaddr_ll
%3 = alloca %struct.tpacket_req
%4 = bitcast %struct.sockaddr_ll* %2 to i8*
%5 = bitcast %struct.tpacket_req* %3 to i8*
%6 = load i32, i32* @PF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = tail call i32 @error(i32 1, i32 %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %1
%14 = load i32, i32* @AF_PACKET
%15 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 0)
%17 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @if_nametoindex(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 0
store i32 %18, i32* %19
%20 = icmp eq i32 %18, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = load i32, i32* @errno
%23 = tail call i32 @error(i32 1, i32 %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %21, %13
%25 = call i64 @bind(i32 %8, i8* %4, i32 12)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = call i32 @error(i32 1, i32 %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %27, %24
%31 = call i8* (...) @getpagesize()
%32 = ptrtoint i8* %31 to i64
%33 = trunc i64 %32 to i32
%34 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i64 0, i32 0
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i64 0, i32 1
store i32 1, i32* %35
%36 = call i8* (...) @getpagesize()
%37 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i64 0, i32 3
store i8* %36, i8** %37
%38 = getelementptr inbounds %struct.tpacket_req, %struct.tpacket_req* %3, i64 0, i32 2
store i32 1, i32* %38
%39 = load i32, i32* @SOL_PACKET
%40 = load i32, i32* @PACKET_TX_RING
%41 = call i64 @setsockopt(i32 %8, i32 %39, i32 %40, i8* %5, i32 24)
%42 = icmp eq i64 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %30
%44 = load i32, i32* @errno
%45 = call i32 @error(i32 1, i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %43, %30
%47 = load i32, i32* %34
%48 = load i32, i32* %35
%49 = mul nsw i32 %48, %47
%50 = load i32, i32* @PROT_READ
%51 = load i32, i32* @PROT_WRITE
%52 = or i32 %51, %50
%53 = load i32, i32* @MAP_SHARED
%54 = call i8* @mmap(i32 0, i32 %49, i32 %52, i32 %53, i32 %8, i32 0)
store i8* %54, i8** %0
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %60
57: ; preds = %46
%58 = load i32, i32* @errno
%59 = call i32 @error(i32 1, i32 %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %60
60: ; preds = %57, %46
ret i32 %8
}