#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
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_iftype(char *dev) {
  struct ifreq ifr;
  int fd;
  int err;
  strcpy(ifr.ifr_name, dev);
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  err = ioctl(fd, SIOCGIFHWADDR, &ifr);
  if (err) {
    perror("ioctl");
    return -1;
  }
  close(fd);
  return ifr.ifr_addr.sa_family;
}


x86 assembly (clang, -O0):
<do_ioctl_get_iftype>:
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    0x2e42(%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_iftype+0x6e>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <do_ioctl_get_iftype+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_iftype>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rsi
call   1030 <strcpy@plt>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
jne    11a2 <do_ioctl_get_iftype+0x52>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    119c <do_ioctl_get_iftype+0x4c>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [6 x i8] c"ioctl\00"
define i32 @do_ioctl_get_iftype(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* @SIOCGIFHWADDR
%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 -1, i32* %2
br label %27
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 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %2
br label %27
27: ; preds = %21, %19
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [6 x i8] c"ioctl\00"
define i32 @do_ioctl_get_iftype(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* @SIOCGIFHWADDR
%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, i32 0
%16 = load i32, i32* %15
br label %17
17: ; preds = %13, %11
%18 = phi i32 [ -1, %11 ], [ %16, %13 ]
ret i32 %18
}