#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct ifreq {
  char *ifr_name;
  int /*<<< orphan*/ ifr_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
char const *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int get_ip_address(char *ip, int iplen, const char *ifname) {
  int s;
  struct ifreq ifr;
  struct sockaddr_in *sin;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s < 0)
    return -1;
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
    close(s);
    return -1;
  }
  sin = (struct sockaddr_in *)&ifr.ifr_addr;
  strncpy(ip, inet_ntoa(sin->sin_addr), iplen);
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<get_ip_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1195 <get_ip_address+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <get_ip_address+0xcb>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11de <get_ip_address+0x8e>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <get_ip_address+0xcb>
lea    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_address>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11c8 <get_ip_address+0x78>
mov    %eax,%ebp
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     11cf <get_ip_address+0x7f>
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
xor    %ebx,%ebx
jmp    11d4 <get_ip_address+0x84>
mov    $0xffffffff,%ebx
jmp    11db <get_ip_address+0x8b>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @get_ip_address(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.ifreq
%10 = alloca %struct.sockaddr_in*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%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* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %42
17: ; preds = %3
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = load i8*, i8** %7
%21 = load i32, i32* @IFNAMSIZ
%22 = call i32 @strncpy(i8* %19, i8* %20, i32 %21)
%23 = load i32, i32* %8
%24 = load i32, i32* @SIOCGIFADDR
%25 = call i64 @ioctl(i32 %23, i32 %24, %struct.ifreq* %9)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %4
br label %42
30: ; preds = %17
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%32 = bitcast i32* %31 to %struct.sockaddr_in*
store %struct.sockaddr_in* %32, %struct.sockaddr_in** %10
%33 = load i8*, i8** %5
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i8* @inet_ntoa(i32 %36)
%38 = load i32, i32* %6
%39 = call i32 @strncpy(i8* %33, i8* %37, i32 %38)
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
store i32 0, i32* %4
br label %42
42: ; preds = %30, %27, %16
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @get_ip_address(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %3
%11 = load i32, i32* @IFNAMSIZ
%12 = tail call i32 @strncpy(i8* undef, i8* %2, i32 %11)
%13 = load i32, i32* @SIOCGIFADDR
%14 = call i64 @ioctl(i32 %8, i32 %13, %struct.ifreq* %4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %10
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i8* @inet_ntoa(i32 %18)
%20 = call i32 @strncpy(i8* %0, i8* %19, i32 %1)
br label %21
21: ; preds = %10, %16
%22 = phi i32 [ 0, %16 ], [ -1, %10 ]
%23 = call i32 @close(i32 %8)
br label %24
24: ; preds = %21, %3
%25 = phi i32 [ -1, %3 ], [ %22, %21 ]
ret i32 %25
}