#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_in {
  int /*<<< orphan*/ sin_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ ifrn_name;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  TYPE_1__ ifr_ifrn;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int get_ip_addr(const char *ifname, char *ip) {
  int err = 0;
  int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  struct ifreq req;
  if (sockfd == -1) {
    err = -1;
    goto exit;
  }
  strncpy(req.ifr_ifrn.ifrn_name, ifname, sizeof(req.ifr_ifrn.ifrn_name));
  err = ioctl(sockfd, SIOCGIFADDR, &req);
  if (err == 0) {
    memcpy(ip, inet_ntoa(((struct sockaddr_in *)(&req.ifr_addr))->sin_addr),
           36);
  }
exit:
  if (sockfd)
    close(sockfd);
  return err;
}


x86 assembly (clang, -O0):
<get_ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11a9 <get_ip_addr+0x49>
movl   $0xffffffff,-0x14(%rbp)
jmp    1201 <get_ip_addr+0xa1>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11fc <get_ip_addr+0x9c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
mov    $0x24,%edx
call   1070 <memcpy@plt>
jmp    1201 <get_ip_addr+0xa1>
cmpl   $0x0,-0x18(%rbp)
je     1213 <get_ip_addr+0xb3>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_addr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11bc <get_ip_addr+0x5c>
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11c3 <get_ip_addr+0x63>
test   %ebx,%ebx
jne    11df <get_ip_addr+0x7f>
jmp    11e6 <get_ip_addr+0x86>
mov    $0xffffffff,%ebp
jmp    11df <get_ip_addr+0x7f>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x24,%edx
call   1070 <memcpy@plt>
test   %ebx,%ebx
je     11e6 <get_ip_addr+0x86>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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