#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*/ ETH_P_ALL;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setPromisc(char const *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int initSniffer(const char *device) {
  int sd;
  sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sd < 0) {
    printf("socket err: %s");
    exit(1);
  }
  setPromisc(device, sd, 1);
  return sd;
}


x86 assembly (clang, -O0):
<initSniffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b2 <initSniffer+0x62>
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x1,%edx
call   1050 <setPromisc@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initSniffer>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e92(%rip),%rax
mov    (%rax),%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%ebx
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     119e <initSniffer+0x4e>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x1,%edx
call   1050 <setPromisc@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [15 x i8] c"socket err: %s\00"
define i32 @initSniffer(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i32, i32* @PF_PACKET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @ETH_P_ALL
%7 = call i32 @htons(i32 %6)
%8 = call i32 @socket(i32 %4, i32 %5, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = load i32, i32* %3
%17 = call i32 @setPromisc(i8* %15, i32 %16, i32 1)
%18 = load i32, i32* %3
ret i32 %18
}

LLVM IR (-O2):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [15 x i8] c"socket err: %s\00"
define i32 @initSniffer(i8* %0) {
%2 = load i32, i32* @PF_PACKET
%3 = load i32, i32* @SOCK_RAW
%4 = load i32, i32* @ETH_P_ALL
%5 = tail call i32 @htons(i32 %4)
%6 = tail call i32 @socket(i32 %2, i32 %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = tail call i32 @setPromisc(i8* %0, i32 %6, i32 1)
ret i32 %6
}