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

/* Variables and functions */
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int errno;
int int_ip4(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int get_if_ip4(int fd, const char *ifname, uint32_t *ip) {
  struct ifreq ifrs;
  memset(&ifrs, 0, sizeof(struct ifreq));
  if (strlen(ifname) > (IFNAMSIZ - 1)) {
    errno = -5;
    return -1;
  }
  strcpy(ifrs.ifr_name, ifname);
  if (ioctl(fd, SIOCGIFADDR, &ifrs) == -1)
    return -1;
  return int_ip4(&ifrs.ifr_addr, ip);
}


x86 assembly (clang, -O0):
<get_if_ip4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    0x2e55(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11a9 <get_if_ip4+0x59>
mov    0x2e39(%rip),%rax
movl   $0xfffffffb,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <get_if_ip4+0x9f>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11df <get_if_ip4+0x8f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <get_if_ip4+0x9f>
lea    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <int_ip4@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_if_ip4>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
jge    11b9 <get_if_ip4+0x69>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11c6 <get_if_ip4+0x76>
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1070 <int_ip4@plt>
jmp    11cb <get_if_ip4+0x7b>
mov    0x2e10(%rip),%rax
movl   $0xfffffffb,(%rax)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@errno = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @get_if_ip4(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.ifreq
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%9 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 8)
%10 = load i8*, i8** %6
%11 = call i32 @strlen(i8* %10)
%12 = load i32, i32* @IFNAMSIZ
%13 = sub nsw i32 %12, 1
%14 = icmp sgt i32 %11, %13
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -5, i32* @errno
store i32 -1, i32* %4
br label %30
16: ; preds = %3
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load i8*, i8** %6
%20 = call i32 @strcpy(i32 %18, i8* %19)
%21 = load i32, i32* %5
%22 = load i32, i32* @SIOCGIFADDR
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %16
store i32 -1, i32* %4
br label %30
26: ; preds = %16
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%28 = load i32*, i32** %7
%29 = call i32 @int_ip4(i32* %27, i32* %28)
store i32 %29, i32* %4
br label %30
30: ; preds = %26, %25, %15
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@errno = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @get_if_ip4(i32 %0, i8* %1, i32* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%7 = call i32 @strlen(i8* %1)
%8 = load i32, i32* @IFNAMSIZ
%9 = icmp slt i32 %7, %8
br i1 %9, label %11, label %10
10: ; preds = %3
store i32 -5, i32* @errno
br label %21
11: ; preds = %3
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i32 @strcpy(i32 %13, i8* %1)
%15 = load i32, i32* @SIOCGIFADDR
%16 = call i32 @ioctl(i32 %0, i32 %15, %struct.ifreq* %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %21, label %18
18: ; preds = %11
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%20 = call i32 @int_ip4(i32* %19, i32* %2)
br label %21
21: ; preds = %11, %18, %10
%22 = phi i32 [ -1, %10 ], [ %20, %18 ], [ -1, %11 ]
ret i32 %22
}