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

/* Variables and functions */
int ENODEV;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int ifname2ifindex(uint32_t *p_ifindex, char *ifname) {
  int sock;
  struct ifreq ifr;
  int ret;
  sock = socket(PF_PACKET, SOCK_DGRAM, 0);
  if (sock == -1)
    return -errno;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  ret = ioctl(sock, SIOCGIFINDEX, &ifr);
  close(sock);
  if (ret == -1) {
    if (errno == ENODEV)
      *p_ifindex = 0;
    else
      return -errno;
  } else {
    *p_ifindex = ifr.ifr_ifindex;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ifname2ifindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1199 <ifname2ifindex+0x49>
xor    %eax,%eax
mov    0x2e39(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <ifname2ifindex+0xec>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e19(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    122c <ifname2ifindex+0xdc>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%eax
jne    1214 <ifname2ifindex+0xc4>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
jmp    1227 <ifname2ifindex+0xd7>
xor    %eax,%eax
mov    0x2dab(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <ifname2ifindex+0xec>
jmp    1235 <ifname2ifindex+0xe5>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifname2ifindex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11dd <ifname2ifindex+0x8d>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%ebx
je     11e8 <ifname2ifindex+0x98>
mov    0x8(%rsp),%r12d
jmp    11fc <ifname2ifindex+0xac>
mov    0x2de4(%rip),%rax
sub    (%rax),%ebx
jmp    1205 <ifname2ifindex+0xb5>
mov    0x2dd9(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc8(%rip),%rax
cmp    (%rax),%ebx
jne    1203 <ifname2ifindex+0xb3>
mov    %r12d,(%r14)
xor    %ebx,%ebx
jmp    1205 <ifname2ifindex+0xb5>
neg    %ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFINDEX = global i32 0
@ENODEV = global i32 0
define i32 @ifname2ifindex(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%9 = load i32, i32* @PF_PACKET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
store i32 %16, i32* %3
br label %47
17: ; preds = %2
%18 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 8)
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load i8*, i8** %5
%22 = load i64, i64* @IFNAMSIZ
%23 = sub nsw i64 %22, 1
%24 = call i32 @strncpy(i32 %20, i8* %21, i64 %23)
%25 = load i32, i32* %6
%26 = load i32, i32* @SIOCGIFINDEX
%27 = call i32 @ioctl(i32 %25, i32 %26, %struct.ifreq* %7)
store i32 %27, i32* %8
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %42
32: ; preds = %17
%33 = load i32, i32* @errno
%34 = load i32, i32* @ENODEV
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32*, i32** %4
store i32 0, i32* %37
br label %41
38: ; preds = %32
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
store i32 %40, i32* %3
br label %47
41: ; preds = %36
br label %46
42: ; preds = %17
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32*, i32** %4
store i32 %44, i32* %45
br label %46
46: ; preds = %42, %41
store i32 0, i32* %3
br label %47
47: ; preds = %46, %38, %14
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFINDEX = global i32 0
@ENODEV = global i32 0
define i32 @ifname2ifindex(i32* nocapture writeonly %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @PF_PACKET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
br label %34
12: ; preds = %2
%13 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%15 = load i32, i32* %14
%16 = load i64, i64* @IFNAMSIZ
%17 = add nsw i64 %16, -1
%18 = call i32 @strncpy(i32 %15, i8* %1, i64 %17)
%19 = load i32, i32* @SIOCGIFINDEX
%20 = call i32 @ioctl(i32 %7, i32 %19, %struct.ifreq* %3)
%21 = call i32 @close(i32 %7)
%22 = icmp eq i32 %20, -1
br i1 %22, label %23, label %29
23: ; preds = %12
%24 = load i32, i32* @errno
%25 = load i32, i32* @ENODEV
%26 = icmp eq i32 %24, %25
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = sub nsw i32 0, %24
br label %34
29: ; preds = %12
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%31 = load i32, i32* %30
br label %32
32: ; preds = %23, %29
%33 = phi i32 [ %31, %29 ], [ 0, %23 ]
store i32 %33, i32* %0
br label %34
34: ; preds = %32, %27, %9
%35 = phi i32 [ %11, %9 ], [ 0, %32 ], [ %28, %27 ]
ret i32 %35
}