#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_halen;
  int /*<<< orphan*/ sll_ifindex;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ if_idx;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_ALEN;
int /*<<< orphan*/ IPPROTO_RAW;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bzero(struct ifreq *, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int open_forward_socket(const char *ifname, struct sockaddr_ll *laddr) {
  struct ifreq if_idx;
  int sock;
  if ((sock = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
    return -1;
  }
  bzero(&if_idx, sizeof(if_idx));
  strncpy(if_idx.ifr_name, ifname, strlen(ifname));
  if (ioctl(sock, SIOCGIFINDEX, &if_idx) < 0) {
    return -1;
  }
  memset(laddr, 0, sizeof(struct sockaddr_ll));
  laddr->sll_ifindex = if_idx.ifr_ifindex;
  laddr->sll_halen = ETH_ALEN;
  return sock;
}


x86 assembly (clang, -O0):
<open_forward_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11a8 <open_forward_socket+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <open_forward_socket+0xd5>
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1206 <open_forward_socket+0xa6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <open_forward_socket+0xd5>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2db9(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_forward_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r13
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     1203 <open_forward_socket+0xa3>
mov    %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    0x4(%rsp),%ebx
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     1203 <open_forward_socket+0xa3>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%rsp),%eax
mov    %eax,0x4(%r14)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    %ebp,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32 }
%struct.ifreq = type { i32, i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@SIOCGIFINDEX = global i32 0
@ETH_ALEN = global i32 0
define i32 @open_forward_socket(i8* %0, %struct.sockaddr_ll* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_ll*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store %struct.sockaddr_ll* %1, %struct.sockaddr_ll** %5
%8 = load i32, i32* @AF_PACKET
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @IPPROTO_RAW
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %38
14: ; preds = %2
%15 = call i32 @bzero(%struct.ifreq* %6, i32 8)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %4
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = call i32 @strncpy(i32 %17, i8* %18, i32 %20)
%22 = load i32, i32* %7
%23 = load i32, i32* @SIOCGIFINDEX
%24 = call i64 @ioctl(i32 %22, i32 %23, %struct.ifreq* %6)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %14
store i32 -1, i32* %3
br label %38
27: ; preds = %14
%28 = load %struct.sockaddr_ll*, %struct.sockaddr_ll** %5
%29 = call i32 @memset(%struct.sockaddr_ll* %28, i32 0, i32 8)
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load %struct.sockaddr_ll*, %struct.sockaddr_ll** %5
%33 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %32, i32 0, i32 1
store i32 %31, i32* %33
%34 = load i32, i32* @ETH_ALEN
%35 = load %struct.sockaddr_ll*, %struct.sockaddr_ll** %5
%36 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %7
store i32 %37, i32* %3
br label %38
38: ; preds = %27, %26, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32 }
%struct.ifreq = type { i32, i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@SIOCGIFINDEX = global i32 0
@ETH_ALEN = global i32 0
define i32 @open_forward_socket(i8* %0, %struct.sockaddr_ll* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_PACKET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @IPPROTO_RAW
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %26, label %10
10: ; preds = %2
%11 = call i32 @bzero(%struct.ifreq* %3, i32 8)
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%13 = load i32, i32* %12
%14 = tail call i32 @strlen(i8* %0)
%15 = tail call i32 @strncpy(i32 %13, i8* %0, i32 %14)
%16 = load i32, i32* @SIOCGIFINDEX
%17 = call i64 @ioctl(i32 %8, i32 %16, %struct.ifreq* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %10
%20 = call i32 @memset(%struct.sockaddr_ll* %1, i32 0, i32 8)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %1, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @ETH_ALEN
%25 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %1, i64 0, i32 0
store i32 %24, i32* %25
br label %26
26: ; preds = %10, %2, %19
%27 = phi i32 [ %8, %19 ], [ -1, %2 ], [ -1, %10 ]
ret i32 %27
}