#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;
struct TYPE_4__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_2__ sin_addr;
};
struct TYPE_3__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int get_ipaddr(const char *ifname, uint32_t *ipaddr) {
  int fd = socket(PF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  struct ifreq req_ipaddr;
  req_ipaddr.ifr_addr.sa_family = PF_INET;
  strcpy(req_ipaddr.ifr_name, ifname);
  if (ioctl(fd, SIOCGIFADDR, &req_ipaddr) < 0)
    goto err;
  *ipaddr =
      ntohl(((struct sockaddr_in *)&req_ipaddr.ifr_addr)->sin_addr.s_addr);
  close(fd);
  return 0;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<get_ipaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1192 <get_ipaddr+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <get_ipaddr+0xb1>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11ce <get_ipaddr+0x7e>
jmp    11f2 <get_ipaddr+0xa2>
mov    -0x28(%rbp),%edi
call   1060 <ntohl@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1201 <get_ipaddr+0xb1>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ipaddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e6c(%rip),%r15
mov    (%r15),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11b2 <get_ipaddr+0x62>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,(%rsp)
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11b9 <get_ipaddr+0x69>
mov    (%rsp),%edi
call   1060 <ntohl@plt>
mov    %eax,(%r14)
xor    %ebx,%ebx
jmp    11be <get_ipaddr+0x6e>
mov    $0xffffffff,%ebx
jmp    11c5 <get_ipaddr+0x75>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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