#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_hwaddr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int get_arptype(char *ifname, int sockfd) {
  struct ifreq ifr;
  if (!ifname)
    return 1;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
    perror("get_arptype: ioctl (SIOCGIFHWADDR):");
    return -1;
  }
  return ifr.ifr_hwaddr.sa_family;
}


x86 assembly (clang, -O0):
<get_arptype>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1166 <get_arptype+0x26>
movl   $0x1,-0x4(%rbp)
jmp    11cd <get_arptype+0x8d>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e54(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11c7 <get_arptype+0x87>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <get_arptype+0x8d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_arptype>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     119e <get_arptype+0x5e>
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11ac <get_arptype+0x6c>
mov    0x8(%rsp),%eax
jmp    11a3 <get_arptype+0x63>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a3 <get_arptype+0x63>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@IFNAMSIZ = global i64 0
@SIOCGIFHWADDR = global i32 0
@.str = [36 x i8] c"get_arptype: ioctl (SIOCGIFHWADDR):\00"
define i32 @get_arptype(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i32 1, i32* %3
br label %28
10: ; preds = %2
%11 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%13 = load i32, i32* %12
%14 = load i8*, i8** %4
%15 = load i64, i64* @IFNAMSIZ
%16 = sub nsw i64 %15, 1
%17 = call i32 @strncpy(i32 %13, i8* %14, i64 %16)
%18 = load i32, i32* %5
%19 = load i32, i32* @SIOCGIFHWADDR
%20 = call i64 @ioctl(i32 %18, i32 %19, %struct.ifreq* %6)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %28
24: ; preds = %10
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i32, i32* %26
store i32 %27, i32* %3
br label %28
28: ; preds = %24, %22, %9
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@IFNAMSIZ = global i64 0
@SIOCGIFHWADDR = global i32 0
@.str = [36 x i8] c"get_arptype: ioctl (SIOCGIFHWADDR):\00"
define i32 @get_arptype(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%9 = load i32, i32* %8
%10 = load i64, i64* @IFNAMSIZ
%11 = add nsw i64 %10, -1
%12 = call i32 @strncpy(i32 %9, i8* %0, i64 %11)
%13 = load i32, i32* @SIOCGIFHWADDR
%14 = call i64 @ioctl(i32 %1, i32 %13, %struct.ifreq* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %21
18: ; preds = %6
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%20 = load i32, i32* %19
br label %21
21: ; preds = %2, %18, %16
%22 = phi i32 [ -1, %16 ], [ %20, %18 ], [ 1, %2 ]
ret i32 %22
}