#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SIOCGIFNETMASK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int get_gateway(char const *, struct in_addr *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int ip_addr(const char *ifname, struct in_addr *addr, struct in_addr *mask,
            struct in_addr *gw) {
  int s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s < 0)
    return -1;
  struct ifreq ifr;
  strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (addr) {
    if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
      goto failed;
    *addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
  }
  if (mask) {
    if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0)
      goto failed;
    *mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
  }
  close(s);
  if (gw)
    return get_gateway(ifname, gw);
  return 0;
failed:
  close(s);
  return -1;
}


x86 assembly (clang, -O0):
<ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    119a <ip_addr+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <ip_addr+0x112>
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
call   1060 <strlcpy@plt>
cmpq   $0x0,-0x18(%rbp)
je     11e7 <ip_addr+0x97>
mov    -0x2c(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11de <ip_addr+0x8e>
jmp    1253 <ip_addr+0x103>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     121f <ip_addr+0xcf>
mov    -0x2c(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1216 <ip_addr+0xc6>
jmp    1253 <ip_addr+0x103>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x28(%rbp)
je     1247 <ip_addr+0xf7>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <get_gateway@plt>
mov    %eax,-0x4(%rbp)
jmp    1262 <ip_addr+0x112>
movl   $0x0,-0x4(%rbp)
jmp    1262 <ip_addr+0x112>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip_addr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     1203 <ip_addr+0xb3>
mov    %eax,%ebp
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rsi
call   1060 <strlcpy@plt>
test   %rbx,%rbx
je     11bd <ip_addr+0x6d>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11fc <ip_addr+0xac>
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
test   %r15,%r15
je     11e3 <ip_addr+0x93>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11fc <ip_addr+0xac>
mov    0x8(%rsp),%eax
mov    %eax,(%r15)
mov    %ebp,%edi
call   1050 <close@plt>
test   %r14,%r14
je     1215 <ip_addr+0xc5>
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <get_gateway@plt>
jmp    1208 <ip_addr+0xb8>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1208 <ip_addr+0xb8>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { %struct.in_addr }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
@SIOCGIFNETMASK = global i32 0
define i32 @ip_addr(i8* %0, %struct.in_addr* %1, %struct.in_addr* %2, %struct.in_addr* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.in_addr*
%8 = alloca %struct.in_addr*
%9 = alloca %struct.in_addr*
%10 = alloca i32
%11 = alloca %struct.ifreq
store i8* %0, i8** %6
store %struct.in_addr* %1, %struct.in_addr** %7
store %struct.in_addr* %2, %struct.in_addr** %8
store %struct.in_addr* %3, %struct.in_addr** %9
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %68
18: ; preds = %4
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load i8*, i8** %6
%22 = load i32, i32* @IFNAMSIZ
%23 = call i32 @strlcpy(i32 %20, i8* %21, i32 %22)
%24 = load %struct.in_addr*, %struct.in_addr** %7
%25 = icmp ne %struct.in_addr* %24, null
br i1 %25, label %26, label %39
26: ; preds = %18
%27 = load i32, i32* %10
%28 = load i32, i32* @SIOCGIFADDR
%29 = call i64 @ioctl(i32 %27, i32 %28, %struct.ifreq* %11)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
br label %65
32: ; preds = %26
%33 = load %struct.in_addr*, %struct.in_addr** %7
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 0
%35 = bitcast i32* %34 to %struct.sockaddr_in*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
%37 = bitcast %struct.in_addr* %33 to i8*
%38 = bitcast %struct.in_addr* %36 to i8*
br label %39
39: ; preds = %32, %18
%40 = load %struct.in_addr*, %struct.in_addr** %8
%41 = icmp ne %struct.in_addr* %40, null
br i1 %41, label %42, label %55
42: ; preds = %39
%43 = load i32, i32* %10
%44 = load i32, i32* @SIOCGIFNETMASK
%45 = call i64 @ioctl(i32 %43, i32 %44, %struct.ifreq* %11)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
br label %65
48: ; preds = %42
%49 = load %struct.in_addr*, %struct.in_addr** %8
%50 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 0
%51 = bitcast i32* %50 to %struct.sockaddr_in*
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %51, i32 0, i32 0
%53 = bitcast %struct.in_addr* %49 to i8*
%54 = bitcast %struct.in_addr* %52 to i8*
br label %55
55: ; preds = %48, %39
%56 = load i32, i32* %10
%57 = call i32 @close(i32 %56)
%58 = load %struct.in_addr*, %struct.in_addr** %9
%59 = icmp ne %struct.in_addr* %58, null
br i1 %59, label %60, label %64
60: ; preds = %55
%61 = load i8*, i8** %6
%62 = load %struct.in_addr*, %struct.in_addr** %9
%63 = call i32 @get_gateway(i8* %61, %struct.in_addr* %62)
store i32 %63, i32* %5
br label %68
64: ; preds = %55
store i32 0, i32* %5
br label %68
65: ; preds = %47, %31
%66 = load i32, i32* %10
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %5
br label %68
68: ; preds = %65, %64, %60, %17
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFADDR = global i32 0
@SIOCGIFNETMASK = global i32 0
define i32 @ip_addr(i8* %0, %struct.in_addr* writeonly %1, %struct.in_addr* writeonly %2, %struct.in_addr* %3) {
%5 = alloca %struct.ifreq
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %42, label %10
10: ; preds = %4
%11 = bitcast %struct.ifreq* %5 to i8*
%12 = load i32, i32* @IFNAMSIZ
%13 = tail call i32 @strlcpy(i32 undef, i8* %0, i32 %12)
%14 = icmp eq %struct.in_addr* %1, null
br i1 %14, label %23, label %15
15: ; preds = %10
%16 = load i32, i32* @SIOCGIFADDR
%17 = call i64 @ioctl(i32 %8, i32 %16, %struct.ifreq* %5)
%18 = icmp slt i64 %17, 0
br i1 %18, label %38, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%21 = getelementptr %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
%22 = load i32, i32* %20
store i32 %22, i32* %21
br label %23
23: ; preds = %19, %10
%24 = icmp eq %struct.in_addr* %2, null
br i1 %24, label %33, label %25
25: ; preds = %23
%26 = load i32, i32* @SIOCGIFNETMASK
%27 = call i64 @ioctl(i32 %8, i32 %26, %struct.ifreq* %5)
%28 = icmp slt i64 %27, 0
br i1 %28, label %38, label %29
29: ; preds = %25
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%31 = getelementptr %struct.in_addr, %struct.in_addr* %2, i64 0, i32 0
%32 = load i32, i32* %30
store i32 %32, i32* %31
br label %33
33: ; preds = %29, %23
%34 = call i32 @close(i32 %8)
%35 = icmp eq %struct.in_addr* %3, null
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = call i32 @get_gateway(i8* %0, %struct.in_addr* %3)
br label %40
38: ; preds = %25, %15
%39 = call i32 @close(i32 %8)
br label %40
40: ; preds = %33, %38, %36
%41 = phi i32 [ -1, %38 ], [ %37, %36 ], [ 0, %33 ]
br label %42
42: ; preds = %4, %40
%43 = phi i32 [ %41, %40 ], [ -1, %4 ]
ret i32 %43
}