#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 in_addr {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  struct in_addr sin_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
struct in_addr get_ipaddr(char *ifname) {
  int fd;
  struct ifreq ifr;
  struct in_addr pa;
  pa.s_addr = 0;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return pa;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_addr.sa_family = AF_INET;
  strcpy(ifr.ifr_name, ifname);
  if (ioctl(fd, SIOCGIFADDR, &ifr) == 0)
    pa = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
  close(fd);
  return pa;
}


x86 assembly (clang, -O0):
<get_ipaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118f <get_ipaddr+0x3f>
jmp    11e6 <get_ipaddr+0x96>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    11de <get_ipaddr+0x8e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ipaddr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e7a(%rip),%r12
mov    (%r12),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11d0 <get_ipaddr+0x80>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    11c9 <get_ipaddr+0x79>
mov    0x8(%rsp),%rbx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i64 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define i64 @get_ipaddr(i8* %0) {
%2 = alloca %struct.in_addr
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.ifreq
store i8* %0, i8** %3
%6 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i32 0, i32 0
store i64 0, i64* %6
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
br label %35
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %5, i32 0, i32 8)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i32 %19, i8* %20)
%22 = load i32, i32* %4
%23 = load i32, i32* @SIOCGIFADDR
%24 = call i64 @ioctl(i32 %22, i32 %23, %struct.ifreq* %5)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %13
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%28 = bitcast %struct.TYPE_2__* %27 to %struct.sockaddr_in*
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 0
%30 = bitcast %struct.in_addr* %2 to i8*
%31 = bitcast %struct.in_addr* %29 to i8*
br label %32
32: ; preds = %26, %13
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32, %12
%36 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i32 0, i32 0
%37 = load i64, i64* %36
ret i64 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr }
%struct.in_addr = type { i64 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define i64 @get_ipaddr(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to %struct.ifreq*
%4 = bitcast %struct.sockaddr_in* %2 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 = icmp slt i32 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %1
%10 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%11 = load i32, i32* @AF_INET
%12 = bitcast %struct.sockaddr_in* %2 to i32*
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @strcpy(i32 %14, i8* %0)
%16 = load i32, i32* @SIOCGIFADDR
%17 = call i64 @ioctl(i32 %7, i32 %16, %struct.ifreq* %3)
%18 = icmp eq i64 %17, 0
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
%20 = load i64, i64* %19
%21 = select i1 %18, i64 %20, i64 0
%22 = call i32 @close(i32 %7)
br label %23
23: ; preds = %1, %9
%24 = phi i64 [ %21, %9 ], [ 0, %1 ]
ret i64 %24
}