#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;
scalar_t__ int_ip4(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int get_if_ip4(int fd, const char *ifname, uint32_t *ip) {
  int err = -1;
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(struct ifreq));
  if (strlen(ifname) > (IFNAMSIZ - 1)) {
    printf("Too long interface name");
    printf("\n");
    ;
    goto out;
  }
  strcpy(ifr.ifr_name, ifname);
  if (ioctl(fd, SIOCGIFADDR, &ifr) == -1) {
    perror("SIOCGIFADDR");
    goto out;
  }
  if (int_ip4(&ifr.ifr_addr, ip)) {
    goto out;
  }
  err = 0;
out:
  return err;
}


x86 assembly (clang, -O0):
<get_if_ip4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11d4 <get_if_ip4+0x64>
lea    0xe42(%rip),%rdi
call   1050 <printf@plt>
lea    0xe4e(%rip),%rdi
call   1050 <printf@plt>
jmp    1232 <get_if_ip4+0xc2>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x4(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    120f <get_if_ip4+0x9f>
lea    0xe15(%rip),%rdi
call   1090 <perror@plt>
jmp    1232 <get_if_ip4+0xc2>
lea    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <int_ip4@plt>
cmp    $0x0,%rax
je     122b <get_if_ip4+0xbb>
jmp    1232 <get_if_ip4+0xc2>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x30,%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   1070 <memset@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%eax
jge    11ee <get_if_ip4+0x6e>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1080 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1212 <get_if_ip4+0x92>
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1090 <int_ip4@plt>
neg    %rax
sbb    %eax,%eax
jmp    1209 <get_if_ip4+0x89>
lea    0xe0b(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdff(%rip),%rdi
call   10a0 <perror@plt>
jmp    1204 <get_if_ip4+0x84>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@.str = [24 x i8] c"Too long interface name\00"
@.str.1 = [2 x i8] c"\0A\00"
@SIOCGIFADDR = global i32 0
@.str.2 = [12 x i8] c"SIOCGIFADDR\00"
define i32 @get_if_ip4(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.ifreq
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i32 -1, i32* %7
%9 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 8)
%10 = load i8*, i8** %5
%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 %18
15: ; preds = %3
%16 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %36
18: ; preds = %3
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load i8*, i8** %5
%22 = call i32 @strcpy(i32 %20, i8* %21)
%23 = load i32, i32* %4
%24 = load i32, i32* @SIOCGIFADDR
%25 = call i32 @ioctl(i32 %23, i32 %24, %struct.ifreq* %8)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %36
29: ; preds = %18
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%31 = load i32*, i32** %6
%32 = call i64 @int_ip4(i32* %30, i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
br label %36
35: ; preds = %29
store i32 0, i32* %7
br label %36
36: ; preds = %35, %34, %27, %15
%37 = load i32, i32* %7
ret i32 %37
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@.str = [24 x i8] c"Too long interface name\00"
@SIOCGIFADDR = global i32 0
@.str.2 = [12 x i8] c"SIOCGIFADDR\00"
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 %13, label %10
10: ; preds = %3
%11 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @putchar(i32 10)
br label %27
13: ; preds = %3
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* %1)
%17 = load i32, i32* @SIOCGIFADDR
%18 = call i32 @ioctl(i32 %0, i32 %17, %struct.ifreq* %4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %27
22: ; preds = %13
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%24 = call i64 @int_ip4(i32* %23, i32* %2)
%25 = icmp ne i64 %24, 0
%26 = sext i1 %25 to i32
br label %27
27: ; preds = %22, %20, %10
%28 = phi i32 [ -1, %10 ], [ -1, %20 ], [ %26, %22 ]
ret i32 %28
}