#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct sockaddr_in {
  int dummy;
};
struct TYPE_3__ {
  int /*<<< orphan*/ ifru_addr;
};
struct TYPE_4__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_ifru;
  int /*<<< orphan*/ ifr_name;
  TYPE_2__ ifr_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
int get_interface_ipaddress(const char *devicename, struct sockaddr_in *addr) {
  struct ifreq ifr;
  int s = 0;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, devicename, IFNAMSIZ - 1);
  if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
    printf("SIOCGIFADDR: fail ioctl errno=%d\n", errno);
    close(s);
    return -1;
  }
  close(s);
  memcpy(addr, &ifr.ifr_ifru.ifru_addr, sizeof(*addr));
  return 0;
}


x86 assembly (clang, -O0):
<get_interface_ipaddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e1b(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1200 <get_interface_ipaddress+0xa0>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <get_interface_ipaddress+0xc1>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_interface_ipaddress>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e67(%rip),%r12
mov    (%r12),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x2e27(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11e1 <get_interface_ipaddress+0x81>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
jmp    1203 <get_interface_ipaddress+0xa3>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { %struct.TYPE_3__, i32, %struct.TYPE_4__ }
%struct.TYPE_3__ = type { i32 }
%struct.TYPE_4__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
@.str = [34 x i8] c"SIOCGIFADDR: fail ioctl errno=%d\0A\00"
@errno = global i32 0
define i32 @get_interface_ipaddress(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
store i32 0, i32* %7
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load i8*, i8** %4
%17 = load i64, i64* @IFNAMSIZ
%18 = sub nsw i64 %17, 1
%19 = call i32 @strncpy(i32 %15, i8* %16, i64 %18)
%20 = load i32, i32* %7
%21 = load i32, i32* @SIOCGIFADDR
%22 = call i32 @ioctl(i32 %20, i32 %21, %struct.ifreq* %6)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %2
%25 = load i32, i32* @errno
%26 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %3
br label %36
29: ; preds = %2
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i32 0, i32 0
%35 = call i32 @memcpy(%struct.sockaddr_in* %32, i32* %34, i32 4)
store i32 0, i32* %3
br label %36
36: ; preds = %29, %24
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { %struct.TYPE_3__, i32, %struct.TYPE_4__ }
%struct.TYPE_3__ = type { i32 }
%struct.TYPE_4__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
@.str = [34 x i8] c"SIOCGIFADDR: fail ioctl errno=%d\0A\00"
@errno = global i32 0
define i32 @get_interface_ipaddress(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%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* @AF_INET
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 2, i32 0
store i32 %8, i32* %9
%10 = load i64, i64* @IFNAMSIZ
%11 = add nsw i64 %10, -1
%12 = tail call i32 @strncpy(i32 undef, i8* %0, i64 %11)
%13 = load i32, i32* @SIOCGIFADDR
%14 = call i32 @ioctl(i32 %7, i32 %13, %struct.ifreq* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @close(i32 %7)
br label %24
20: ; preds = %2
%21 = call i32 @close(i32 %7)
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%23 = call i32 @memcpy(%struct.sockaddr_in* %1, i32* %22, i32 4)
br label %24
24: ; preds = %20, %16
%25 = phi i32 [ -1, %16 ], [ 0, %20 ]
ret i32 %25
}