#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 sockaddr_storage {
  int dummy;
};
struct TYPE_2__ {
  int sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int af_socket(int);
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, TYPE_1__ *, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char *, char const *, int /*<<< orphan*/);
int device_address(const char *ifname, int af, struct sockaddr_storage *ss) {
  int ret, sock;
  struct ifreq ifr;
  if (!ss)
    return -EINVAL;
  if (!strncmp("any", ifname, strlen("any")))
    return -EINVAL;
  sock = af_socket(af);
  memset(&ifr, 0, sizeof(ifr));
  strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ifr.ifr_addr.sa_family = af;
  ret = ioctl(sock, SIOCGIFADDR, &ifr);
  if (!ret)
    memcpy(ss, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
  close(sock);
  return ret;
}


x86 assembly (clang, -O0):
<device_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <device_address+0x31>
xor    %eax,%eax
mov    0x2e29(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    126b <device_address+0xeb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe40(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe2e(%rip),%rdi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11f3 <device_address+0x73>
xor    %eax,%eax
mov    0x2de7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    126b <device_address+0xeb>
mov    -0x14(%rbp),%edi
call   1040 <af_socket@plt>
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
call   10a0 <strlcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    125d <device_address+0xdd>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_address>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdx,%rdx
je     122d <device_address+0xad>
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbx
lea    0xe5b(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     122d <device_address+0xad>
mov    %r12d,%edi
call   1040 <af_socket@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   10a0 <strlcpy@plt>
mov    %r12d,0x8(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbp,%rdx
call   1070 <ioctl@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1223 <device_address+0xa3>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    %r14d,%edi
call   1080 <close@plt>
jmp    1238 <device_address+0xb8>
mov    0x2d9c(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@EINVAL = global i32 0
@.str = [4 x i8] c"any\00"
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @device_address(i8* %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.ifreq
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
%11 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%12 = icmp ne %struct.sockaddr_storage* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
store i32 %15, i32* %4
br label %49
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @EINVAL
%23 = sub nsw i32 0, %22
store i32 %23, i32* %4
br label %49
24: ; preds = %16
%25 = load i32, i32* %6
%26 = call i32 @af_socket(i32 %25)
store i32 %26, i32* %9
%27 = call i32 @memset(%struct.ifreq* %10, i32 0, i32 8)
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 1
%29 = load i32, i32* %28
%30 = load i8*, i8** %5
%31 = load i32, i32* @IFNAMSIZ
%32 = call i32 @strlcpy(i32 %29, i8* %30, i32 %31)
%33 = load i32, i32* %6
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i32, i32* %9
%37 = load i32, i32* @SIOCGIFADDR
%38 = call i32 @ioctl(i32 %36, i32 %37, %struct.ifreq* %10)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %24
%42 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 0
%44 = call i32 @memcpy(%struct.sockaddr_storage* %42, %struct.TYPE_2__* %43, i32 4)
br label %45
45: ; preds = %41, %24
%46 = load i32, i32* %9
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %49
49: ; preds = %45, %21, %13
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@EINVAL = global i32 0
@.str = [4 x i8] c"any\00"
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @device_address(i8* %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = icmp eq %struct.sockaddr_storage* %2, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %33
10: ; preds = %3
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @EINVAL
%16 = sub nsw i32 0, %15
br label %33
17: ; preds = %10
%18 = tail call i32 @af_socket(i32 %1)
%19 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%21 = load i32, i32* %20
%22 = load i32, i32* @IFNAMSIZ
%23 = call i32 @strlcpy(i32 %21, i8* %0, i32 %22)
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0, i32 0
store i32 %1, i32* %24
%25 = load i32, i32* @SIOCGIFADDR
%26 = call i32 @ioctl(i32 %18, i32 %25, %struct.ifreq* %4)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%30 = call i32 @memcpy(%struct.sockaddr_storage* %2, %struct.TYPE_2__* %29, i32 4)
br label %31
31: ; preds = %28, %17
%32 = call i32 @close(i32 %18)
br label %33
33: ; preds = %31, %14, %7
%34 = phi i32 [ %26, %31 ], [ %16, %14 ], [ %9, %7 ]
ret i32 %34
}