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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
char *strerror(int /*<<< orphan*/);
int raw_get_dev_index(char *name) {
  struct ifreq if_req;
  int fd;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "raw_get_dev_index: socket: %s\n", strerror(errno));
    return (-1);
  }
  memset((void *)&if_req, 0, sizeof(if_req));
  strcpy(if_req.ifr_name, name);
  if (ioctl(fd, SIOCGIFINDEX, &if_req) < 0) {
    fprintf(stderr, "raw_get_dev_index: SIOCGIFINDEX: %s\n", strerror(errno));
    close(fd);
    return (-1);
  }
  close(fd);
  return (if_req.ifr_ifindex);
}


x86 assembly (clang, -O0):
<raw_get_dev_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11d9 <raw_get_dev_index+0x69>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe38(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <raw_get_dev_index+0xf2>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1254 <raw_get_dev_index+0xe4>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xde4(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <raw_get_dev_index+0xf2>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raw_get_dev_index>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11db <raw_get_dev_index+0x6b>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1205 <raw_get_dev_index+0x95>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%rsp),%eax
jmp    1239 <raw_get_dev_index+0xc9>
mov    0x2df6(%rip),%rax
mov    (%rax),%ebx
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe07(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
jmp    1234 <raw_get_dev_index+0xc4>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ebp
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdfc(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"raw_get_dev_index: socket: %s\0A\00"
@errno = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [37 x i8] c"raw_get_dev_index: SIOCGIFINDEX: %s\0A\00"
define i32 @raw_get_dev_index(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %2
br label %38
15: ; preds = %1
%16 = bitcast %struct.ifreq* %4 to i8*
%17 = call i32 @memset(i8* %16, i32 0, i32 8)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i32 %19, i8* %20)
%22 = load i32, i32* %5
%23 = load i32, i32* @SIOCGIFINDEX
%24 = call i64 @ioctl(i32 %22, i32 %23, %struct.ifreq* %4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %15
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %2
br label %38
33: ; preds = %15
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%37 = load i32, i32* %36
store i32 %37, i32* %2
br label %38
38: ; preds = %33, %26, %10
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"raw_get_dev_index: socket: %s\0A\00"
@errno = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [37 x i8] c"raw_get_dev_index: SIOCGIFINDEX: %s\0A\00"
define i32 @raw_get_dev_index(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %31
13: ; preds = %1
%14 = call i32 @memset(i8* %3, i32 0, i32 8)
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i32 %16, i8* %0)
%18 = load i32, i32* @SIOCGIFINDEX
%19 = call i64 @ioctl(i32 %6, i32 %18, %struct.ifreq* %2)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %13
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = call i32 @close(i32 %6)
br label %31
27: ; preds = %13
%28 = call i32 @close(i32 %6)
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%30 = load i32, i32* %29
br label %31
31: ; preds = %27, %21, %8
%32 = phi i32 [ -1, %8 ], [ -1, %21 ], [ %30, %27 ]
ret i32 %32
}