#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 */

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ sniffer_trace(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_data_sniffer_socket() {
  if (0) {
    sniffer_trace("ENTERING: setup_data_sniffer_socket");
  }
  int sockfd, flags;
  sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  flags = fcntl(sockfd, F_GETFL);
  flags |= O_NONBLOCK;
  fcntl(sockfd, F_SETFL, flags);
  if (sockfd < 0) {
    if (0) {
      sniffer_trace("    ERROR: socket failed!");
    }
    exit(0);
  }
  if (0) {
    sniffer_trace("LEAVING: setup_data_sniffer_socket");
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<setup_data_sniffer_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    %eax,%edx
call   1060 <socket@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmpl   $0x0,-0x4(%rbp)
jge    11ca <setup_data_sniffer_socket+0x8a>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_data_sniffer_socket>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rax
mov    (%rax),%ebx
mov    0x2e8d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e5f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %ebx,%ebx
js     11ac <setup_data_sniffer_socket+0x6c>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @setup_data_sniffer_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @AF_PACKET
%4 = load i32, i32* @SOCK_RAW
%5 = load i32, i32* @ETH_P_ALL
%6 = call i32 @htons(i32 %5)
%7 = call i32 @socket(i32 %3, i32 %4, i32 %6)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = load i32, i32* @F_GETFL
%10 = call i32 (i32, i32, ...) @fcntl(i32 %8, i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* @O_NONBLOCK
%12 = load i32, i32* %2
%13 = or i32 %12, %11
store i32 %13, i32* %2
%14 = load i32, i32* %1
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* %2
%17 = call i32 (i32, i32, ...) @fcntl(i32 %14, i32 %15, i32 %16)
%18 = load i32, i32* %1
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %0
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @setup_data_sniffer_socket() {
%1 = load i32, i32* @AF_PACKET
%2 = load i32, i32* @SOCK_RAW
%3 = load i32, i32* @ETH_P_ALL
%4 = tail call i32 @htons(i32 %3)
%5 = tail call i32 @socket(i32 %1, i32 %2, i32 %4)
%6 = load i32, i32* @F_GETFL
%7 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %8, %7
%10 = load i32, i32* @F_SETFL
%11 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %10, i32 %9)
%12 = icmp slt i32 %5, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %0
ret i32 %5
}