#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 in_addr {
  int /*<<< orphan*/ s_addr;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ in_addr_t;
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ INADDR_NONE;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strnzcpy(int /*<<< orphan*/, char const *,
                            int /*<<< orphan*/);
int get_if_addr(const char *ifname, struct in_addr *addr) {
  int sock_fd;
  struct ifreq ifr;
  int ioctl_ret;
  struct sockaddr_in *sa_in = NULL;
  addr->s_addr = INADDR_NONE;
  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock_fd == -1) {
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  strnzcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ioctl_ret = ioctl(sock_fd, SIOCGIFADDR, &ifr);
  if (close(sock_fd) == -1) {
    return -1;
  }
  if (ioctl_ret == -1) {
    return -1;
  }
  sa_in = (struct sockaddr_in *)&ifr.ifr_addr;
  memcpy(&addr->s_addr, &sa_in->sin_addr, sizeof(in_addr_t));
  return 0;
}


x86 assembly (clang, -O0):
<get_if_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b9 <get_if_addr+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <get_if_addr+0xea>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
call   1030 <strnzcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1213 <get_if_addr+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <get_if_addr+0xea>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1229 <get_if_addr+0xc9>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <get_if_addr+0xea>
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_if_addr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsi)
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     1207 <get_if_addr+0xa7>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strnzcpy@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1050 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     1207 <get_if_addr+0xa7>
cmp    $0xffffffff,%ebp
je     1207 <get_if_addr+0xa7>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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