#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int do_ioctl_get_ifindex(char *dev) {
  struct ifreq ifr;
  int fd;
  int err;
  strcpy(ifr.ifr_name, dev);
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  err = ioctl(fd, SIOCGIFINDEX, &ifr);
  if (err) {
    perror("ioctl");
    return 0;
  }
  close(fd);
  return ifr.ifr_ifindex;
}


x86 assembly (clang, -O0):
<do_ioctl_get_ifindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11be <do_ioctl_get_ifindex+0x6e>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cc <do_ioctl_get_ifindex+0x7c>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_ioctl_get_ifindex>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rsi
call   1030 <strcpy@plt>
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
jne    11a2 <do_ioctl_get_ifindex+0x52>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rsp),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
jmp    1199 <do_ioctl_get_ifindex+0x49>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [6 x i8] c"ioctl\00"
define i32 @do_ioctl_get_ifindex(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%8 = load i32, i32* %7
%9 = load i8*, i8** %3
%10 = call i32 @strcpy(i32 %8, i8* %9)
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = load i32, i32* @SIOCGIFINDEX
%16 = call i32 @ioctl(i32 %14, i32 %15, %struct.ifreq* %4)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %26
21: ; preds = %1
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %2
br label %26
26: ; preds = %21, %19
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [6 x i8] c"ioctl\00"
define i32 @do_ioctl_get_ifindex(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = tail call i32 @strcpy(i32 undef, i8* %0)
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = load i32, i32* @SIOCGIFINDEX
%9 = call i32 @ioctl(i32 %7, i32 %8, %struct.ifreq* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %17
13: ; preds = %1
%14 = call i32 @close(i32 %7)
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%16 = load i32, i32* %15
br label %17
17: ; preds = %13, %11
%18 = phi i32 [ 0, %11 ], [ %16, %13 ]
ret i32 %18
}