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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                              int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
scalar_t__ pig_newseg(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
char *get_iface_ip(const char *iface) {
  int sfd;
  char *ip = NULL;
  struct ifreq req;
  struct sockaddr *addr;
  sfd = socket(PF_INET, SOCK_DGRAM, 0);
  if (sfd == -1)
    return NULL;
  strncpy(req.ifr_name, iface, sizeof(req.ifr_name));
  if (ioctl(sfd, SIOCGIFADDR, &req) == -1) {
    close(sfd);
    return NULL;
  }
  ip = (char *)pig_newseg(20);
  addr = &req.ifr_addr;
  if (inet_ntop(AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), ip, 20) ==
      NULL) {
    close(sfd);
    return NULL;
  }
  close(sfd);
  return ip;
}


x86 assembly (clang, -O0):
<get_iface_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a7 <get_iface_ip+0x47>
movq   $0x0,-0x8(%rbp)
jmp    124b <get_iface_ip+0xeb>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11eb <get_iface_ip+0x8b>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    124b <get_iface_ip+0xeb>
mov    $0x14,%edi
call   1070 <pig_newseg@plt>
mov    %rax,-0x20(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x14,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    123b <get_iface_ip+0xdb>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    124b <get_iface_ip+0xeb>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_iface_ip>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11f8 <get_iface_ip+0x98>
mov    %eax,%ebp
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11ef <get_iface_ip+0x8f>
mov    $0x14,%edi
call   1070 <pig_newseg@plt>
mov    %rax,%rbx
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
mov    $0x14,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1060 <close@plt>
test   %r14,%r14
cmove  %r14,%rbx
jmp    11f8 <get_iface_ip+0x98>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.sockaddr, i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
@AF_INET = global i32 0
define i8* @get_iface_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca %struct.sockaddr*
store i8* %0, i8** %3
store i8* null, i8** %5
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %44
14: ; preds = %1
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = call i32 @strncpy(i32 %16, i8* %17, i32 4)
%19 = load i32, i32* %4
%20 = load i32, i32* @SIOCGIFADDR
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.ifreq* %6)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
store i8* null, i8** %2
br label %44
26: ; preds = %14
%27 = call i64 @pig_newseg(i32 20)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %5
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store %struct.sockaddr* %29, %struct.sockaddr** %7
%30 = load i32, i32* @AF_INET
%31 = load %struct.sockaddr*, %struct.sockaddr** %7
%32 = bitcast %struct.sockaddr* %31 to %struct.sockaddr_in*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 0
%34 = load i8*, i8** %5
%35 = call i32* @inet_ntop(i32 %30, i32* %33, i8* %34, i32 20)
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i8* null, i8** %2
br label %44
40: ; preds = %26
%41 = load i32, i32* %4
%42 = call i32 @close(i32 %41)
%43 = load i8*, i8** %5
store i8* %43, i8** %2
br label %44
44: ; preds = %40, %37, %23, %13
%45 = load i8*, i8** %2
ret i8* %45
}

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