#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 sockaddr_ll {
  int sll_protocol;
  void *sll_ifindex;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
void *if_nametoindex(char const *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
int sniff_create_socket(char const *iface) {
  int raw_socket = socket(PF_PACKET, SOCK_RAW, 0x0003);
  if (raw_socket >= 0) {
    if (strcmp(iface, "any") != 0) {
      int ifindex = if_nametoindex(iface);
      if (ifindex != 0) {
        struct sockaddr_ll saddr_ll;
        saddr_ll.sll_family = AF_PACKET;
        saddr_ll.sll_protocol = 0x0003;
        saddr_ll.sll_ifindex = if_nametoindex(iface);
        if (bind(raw_socket, (struct sockaddr *)&saddr_ll,
                 sizeof(struct sockaddr_ll)) != 0)
          perror("bind");
      } else {
        fprintf(stderr, "if_nametoindex: No such interface found, perhaps you "
                        "want 'any' (all)?\n");
        close(raw_socket);
        return -1;
      }
    }
  } else {
    perror("socket");
    fprintf(stderr, "socket: Are you root? You need CAP_NET_RAW powers.\n");
  }
  return raw_socket;
}


x86 assembly (clang, -O0):
<sniff_create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    $0x3,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1254 <sniff_create_socket+0xe4>
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     124f <sniff_create_socket+0xdf>
mov    -0x10(%rbp),%rdi
call   1060 <if_nametoindex@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1221 <sniff_create_socket+0xb1>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x3,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <if_nametoindex@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     121c <sniff_create_socket+0xac>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
jmp    124a <sniff_create_socket+0xda>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <sniff_create_socket+0x10b>
jmp    124f <sniff_create_socket+0xdf>
jmp    1275 <sniff_create_socket+0x105>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sniff_create_socket>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    $0x3,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1229 <sniff_create_socket+0xb9>
lea    0xe5a(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     124a <sniff_create_socket+0xda>
mov    %rbx,%rdi
call   1060 <if_nametoindex@plt>
test   %eax,%eax
je     1206 <sniff_create_socket+0x96>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movl   $0x3,(%rsp)
mov    %rbx,%rdi
call   1060 <if_nametoindex@plt>
mov    %rax,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     124a <sniff_create_socket+0xda>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
jmp    124a <sniff_create_socket+0xda>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebp
jmp    124a <sniff_create_socket+0xda>
lea    0xe21(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str = [4 x i8] c"any\00"
@AF_PACKET = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@stderr = global i32 0
@.str.2 = [72 x i8] c"if_nametoindex: No such interface found, perhaps you want 'any' (all)?\0A\00"
@.str.3 = [7 x i8] c"socket\00"
@.str.4 = [52 x i8] c"socket: Are you root? You need CAP_NET_RAW powers.\0A\00"
define i32 @sniff_create_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_ll
store i8* %0, i8** %3
%7 = load i32, i32* @PF_PACKET
%8 = load i32, i32* @SOCK_RAW
%9 = call i32 @socket(i32 %7, i32 %8, i32 3)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %42
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = call i8* @if_nametoindex(i8* %17)
%19 = ptrtoint i8* %18 to i32
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %16
%23 = load i32, i32* @AF_PACKET
%24 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i32 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i32 0, i32 0
store i32 3, i32* %25
%26 = load i8*, i8** %3
%27 = call i8* @if_nametoindex(i8* %26)
%28 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i32 0, i32 1
store i8* %27, i8** %28
%29 = load i32, i32* %4
%30 = bitcast %struct.sockaddr_ll* %6 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 24)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %22
br label %41
36: ; preds = %16
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %2
br label %49
41: ; preds = %35
br label %42
42: ; preds = %41, %12
br label %47
43: ; preds = %1
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %43, %42
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %36
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str = [4 x i8] c"any\00"
@AF_PACKET = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@stderr = global i32 0
@.str.2 = [72 x i8] c"if_nametoindex: No such interface found, perhaps you want 'any' (all)?\0A\00"
@.str.3 = [7 x i8] c"socket\00"
@.str.4 = [52 x i8] c"socket: Are you root? You need CAP_NET_RAW powers.\0A\00"
define i32 @sniff_create_socket(i8* %0) {
%2 = alloca %struct.sockaddr_ll
%3 = load i32, i32* @PF_PACKET
%4 = load i32, i32* @SOCK_RAW
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 3)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %32
7: ; preds = %1
%8 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %36, label %10
10: ; preds = %7
%11 = tail call i8* @if_nametoindex(i8* %0)
%12 = ptrtoint i8* %11 to i64
%13 = trunc i64 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %27, label %15
15: ; preds = %10
%16 = bitcast %struct.sockaddr_ll* %2 to i8*
%17 = load i32, i32* @AF_PACKET
%18 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 0
store i32 3, i32* %19
%20 = tail call i8* @if_nametoindex(i8* %0)
%21 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 1
store i8* %20, i8** %21
%22 = bitcast %struct.sockaddr_ll* %2 to %struct.sockaddr*
%23 = call i64 @bind(i32 %5, %struct.sockaddr* %22, i32 24)
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %31
27: ; preds = %10
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @close(i32 %5)
br label %36
31: ; preds = %15, %25
br label %36
32: ; preds = %1
%33 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %32, %7, %31, %27
%37 = phi i32 [ -1, %27 ], [ %5, %31 ], [ %5, %7 ], [ %5, %32 ]
ret i32 %37
}