#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 /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  int /*<<< orphan*/ ifr_name;
};
struct sockaddr {
  int dummy;
};
struct packet_mreq {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ sll;
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
scalar_t__ EINTR;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
scalar_t__ IFNAMSIZ;
int O_NONBLOCK;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct sockaddr_ll *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int create_capture_socket(char *ifname) {
  int sock_raw;
  struct ifreq ifr;
  struct sockaddr_ll sll;
  struct packet_mreq mrq;
  int fdflags;
  sock_raw = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sock_raw < 0) {
    perror("create_capture_socket: socket : ");
    return -1;
  }
  if (!ifname)
    return sock_raw;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  if (ioctl(sock_raw, SIOCGIFINDEX, &ifr) < 0) {
    perror("create_capture_socket: ioctl(SIOCGIFINDEX) :");
    goto err1;
  }
  memset(&sll, 0, sizeof(sll));
  sll.sll_family = AF_PACKET;
  sll.sll_ifindex = ifr.ifr_ifindex;
  sll.sll_protocol = htons(ETH_P_ALL);
  if (bind(sock_raw, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
    perror("create_capture_socket: bind()");
    goto err1;
  }
  fdflags = fcntl(sock_raw, F_GETFL);
  if (fdflags < 0) {
    perror("create_capture_socket: fcntl(F_GETFL):");
    goto err1;
  }
  if (fcntl(sock_raw, F_SETFL, fdflags | O_NONBLOCK) < 0) {
    perror("create_capture_socket: fcntl(F_SETFL)");
  }
  return sock_raw;
err1:
  while ((close(sock_raw) < 1) && (errno == EINTR))
    ;
  return -1;
}


x86 assembly (clang, -O0):
<create_capture_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%esi
mov    %eax,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f4 <create_capture_socket+0x64>
lea    0xe1d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <create_capture_socket+0x1f6>
cmpq   $0x0,-0x10(%rbp)
jne    120a <create_capture_socket+0x7a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1386 <create_capture_socket+0x1f6>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d90(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    1264 <create_capture_socket+0xd4>
lea    0xdc7(%rip),%rdi
call   10a0 <perror@plt>
jmp    1333 <create_capture_socket+0x1a3>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12c3 <create_capture_socket+0x133>
lea    0xd95(%rip),%rdi
call   10a0 <perror@plt>
jmp    1333 <create_capture_socket+0x1a3>
mov    -0x14(%rbp),%edi
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    12f4 <create_capture_socket+0x164>
lea    0xd82(%rip),%rdi
call   10a0 <perror@plt>
jmp    1333 <create_capture_socket+0x1a3>
mov    -0x14(%rbp),%edi
mov    0x2cb2(%rip),%rax
mov    (%rax),%esi
mov    -0x4c(%rbp),%edx
mov    0x2cd6(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    1328 <create_capture_socket+0x198>
lea    0xd70(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1386 <create_capture_socket+0x1f6>
jmp    1338 <create_capture_socket+0x1a8>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x55(%rbp)
jge    136a <create_capture_socket+0x1da>
mov    0x2c51(%rip),%rax
mov    (%rax),%rax
mov    0x2c37(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    137a <create_capture_socket+0x1ea>
jmp    137f <create_capture_socket+0x1ef>
jmp    1338 <create_capture_socket+0x1a8>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_capture_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r14
mov    0x2e4c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e2b(%rip),%rax
mov    (%rax),%ebp
mov    0x2e12(%rip),%r15
mov    (%r15),%edi
call   1050 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12b3 <create_capture_socket+0x123>
mov    %eax,%ebx
test   %r14,%r14
je     12a6 <create_capture_socket+0x116>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x30(%rsp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
js     12c6 <create_capture_socket+0x136>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x24(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    (%r15),%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x14,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     12cf <create_capture_socket+0x13f>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     12d8 <create_capture_socket+0x148>
mov    0x2d24(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d4b(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     131d <create_capture_socket+0x18d>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd46(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    12a8 <create_capture_socket+0x118>
lea    0xd54(%rip),%rdi
jmp    12df <create_capture_socket+0x14f>
lea    0xd78(%rip),%rdi
jmp    12df <create_capture_socket+0x14f>
lea    0xd8d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cbd(%rip),%r14
mov    0x2ca6(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
call   1080 <close@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jg     12a8 <create_capture_socket+0x118>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r14)
je     1300 <create_capture_socket+0x170>
jmp    12a8 <create_capture_socket+0x118>
lea    0xd6f(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a6 <create_capture_socket+0x116>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_ll = type { i32, i32, i32, i32, i32 }
%struct.packet_mreq = type { i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [33 x i8] c"create_capture_socket: socket : \00"
@IFNAMSIZ = global i64 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [45 x i8] c"create_capture_socket: ioctl(SIOCGIFINDEX) :\00"
@AF_PACKET = global i32 0
@.str.2 = [30 x i8] c"create_capture_socket: bind()\00"
@F_GETFL = global i32 0
@.str.3 = [39 x i8] c"create_capture_socket: fcntl(F_GETFL):\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [38 x i8] c"create_capture_socket: fcntl(F_SETFL)\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @create_capture_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.ifreq
%6 = alloca %struct.sockaddr_ll
%7 = alloca %struct.packet_mreq
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @PF_PACKET
%10 = load i32, i32* @SOCK_RAW
%11 = load i32, i32* @ETH_P_ALL
%12 = call i32 @htons(i32 %11)
%13 = call i32 @socket(i32 %9, i32 %10, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %88
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = load i32, i32* %4
store i32 %22, i32* %2
br label %88
23: ; preds = %18
%24 = bitcast %struct.ifreq* %5 to %struct.sockaddr_ll*
%25 = call i32 @memset(%struct.sockaddr_ll* %24, i32 0, i32 20)
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 4
%27 = load i32, i32* %26
%28 = load i8*, i8** %3
%29 = load i64, i64* @IFNAMSIZ
%30 = sub nsw i64 %29, 1
%31 = call i32 @strncpy(i32 %27, i8* %28, i64 %30)
%32 = load i32, i32* %4
%33 = load i32, i32* @SIOCGIFINDEX
%34 = bitcast %struct.ifreq* %5 to %struct.sockaddr_ll*
%35 = call i64 @ioctl(i32 %32, i32 %33, %struct.sockaddr_ll* %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %23
%38 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %75
39: ; preds = %23
%40 = call i32 @memset(%struct.sockaddr_ll* %6, i32 0, i32 20)
%41 = load i32, i32* @AF_PACKET
%42 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i32 0, i32 3
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i32 0, i32 2
store i32 %44, i32* %45
%46 = load i32, i32* @ETH_P_ALL
%47 = call i32 @htons(i32 %46)
%48 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i32 0, i32 0
store i32 %47, i32* %48
%49 = load i32, i32* %4
%50 = bitcast %struct.sockaddr_ll* %6 to %struct.sockaddr*
%51 = call i64 @bind(i32 %49, %struct.sockaddr* %50, i32 20)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %39
%54 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %75
55: ; preds = %39
%56 = load i32, i32* %4
%57 = load i32, i32* @F_GETFL
%58 = call i32 (i32, i32, ...) @fcntl(i32 %56, i32 %57)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %75
63: ; preds = %55
%64 = load i32, i32* %4
%65 = load i32, i32* @F_SETFL
%66 = load i32, i32* %8
%67 = load i32, i32* @O_NONBLOCK
%68 = or i32 %66, %67
%69 = call i32 (i32, i32, ...) @fcntl(i32 %64, i32 %65, i32 %68)
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %63
%72 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %73
73: ; preds = %71, %63
%74 = load i32, i32* %4
store i32 %74, i32* %2
br label %88
75: ; preds = %61, %53, %37
br label %76
76: ; preds = %86, %75
%77 = load i32, i32* %4
%78 = call i32 @close(i32 %77)
%79 = icmp slt i32 %78, 1
br i1 %79, label %80, label %84
80: ; preds = %76
%81 = load i64, i64* @errno
%82 = load i64, i64* @EINTR
%83 = icmp eq i64 %81, %82
br label %84
84: ; preds = %80, %76
%85 = phi i1 [ false, %76 ], [ %83, %80 ]
br i1 %85, label %86, label %87
86: ; preds = %84
br label %76
87: ; preds = %84
store i32 -1, i32* %2
br label %88
88: ; preds = %87, %73, %21, %16
%89 = load i32, i32* %2
ret i32 %89
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_ll = type { i32, i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [33 x i8] c"create_capture_socket: socket : \00"
@IFNAMSIZ = global i64 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [45 x i8] c"create_capture_socket: ioctl(SIOCGIFINDEX) :\00"
@AF_PACKET = global i32 0
@.str.2 = [30 x i8] c"create_capture_socket: bind()\00"
@F_GETFL = global i32 0
@.str.3 = [39 x i8] c"create_capture_socket: fcntl(F_GETFL):\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [38 x i8] c"create_capture_socket: fcntl(F_SETFL)\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @create_capture_socket(i8* %0) {
%2 = alloca %struct.ifreq
%3 = alloca %struct.sockaddr_ll
%4 = bitcast %struct.ifreq* %2 to i8*
%5 = bitcast %struct.sockaddr_ll* %3 to i8*
%6 = load i32, i32* @PF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @ETH_P_ALL
%9 = tail call i32 @htons(i32 %8)
%10 = tail call i32 @socket(i32 %6, i32 %7, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %62
14: ; preds = %1
%15 = icmp eq i8* %0, null
br i1 %15, label %62, label %16
16: ; preds = %14
%17 = bitcast %struct.ifreq* %2 to %struct.sockaddr_ll*
%18 = call i32 @memset(%struct.sockaddr_ll* %17, i32 0, i32 20)
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 4
%20 = load i32, i32* %19
%21 = load i64, i64* @IFNAMSIZ
%22 = add nsw i64 %21, -1
%23 = call i32 @strncpy(i32 %20, i8* %0, i64 %22)
%24 = load i32, i32* @SIOCGIFINDEX
%25 = call i64 @ioctl(i32 %10, i32 %24, %struct.sockaddr_ll* %17)
%26 = icmp slt i64 %25, 0
br i1 %26, label %52, label %27
27: ; preds = %16
%28 = call i32 @memset(%struct.sockaddr_ll* %3, i32 0, i32 20)
%29 = load i32, i32* @AF_PACKET
%30 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 3
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* @ETH_P_ALL
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 0
store i32 %35, i32* %36
%37 = bitcast %struct.sockaddr_ll* %3 to %struct.sockaddr*
%38 = call i64 @bind(i32 %10, %struct.sockaddr* %37, i32 20)
%39 = icmp slt i64 %38, 0
br i1 %39, label %52, label %40
40: ; preds = %27
%41 = load i32, i32* @F_GETFL
%42 = call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %52, label %44
44: ; preds = %40
%45 = load i32, i32* @F_SETFL
%46 = load i32, i32* @O_NONBLOCK
%47 = or i32 %46, %42
%48 = call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %45, i32 %47)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %62
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %62
52: ; preds = %40, %27, %16
%53 = phi i8* [ getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), %16 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), %27 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), %40 ]
%54 = call i32 @perror(i8* %53)
br label %55
55: ; preds = %55, %52
%56 = call i32 @close(i32 %10)
%57 = icmp slt i32 %56, 1
%58 = load i64, i64* @errno
%59 = load i64, i64* @EINTR
%60 = icmp eq i64 %58, %59
%61 = select i1 %57, i1 %60, i1 false
br i1 %61, label %55, label %62
62: ; preds = %55, %44, %50, %14, %12
%63 = phi i32 [ -1, %12 ], [ %10, %14 ], [ %10, %50 ], [ %10, %44 ], [ -1, %55 ]
ret i32 %63
}