#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 ifr_flags;
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFF_UP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCGIFNETMASK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, struct in_addr *, char *, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *, int *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int getifaddr(const char *ifname, char *buf, int len, struct in_addr *addr,
              struct in_addr *mask) {
  int s;
  struct ifreq ifr;
  int ifrlen;
  struct sockaddr_in *ifaddr;
  ifrlen = sizeof(ifr);
  if (!ifname || ifname[0] == '\0')
    return -1;
  s = socket(PF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    do {
      printf("socket(PF_INET, SOCK_DGRAM): %m");
      printf("\n");
    } while (0);
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(s, SIOCGIFFLAGS, &ifr, &ifrlen) < 0) {
    do {
      printf("ioctl(s, SIOCGIFFLAGS, ...): %m");
      printf("\n");
    } while (0);
    close(s);
    return -1;
  }
  if ((ifr.ifr_flags & IFF_UP) == 0) {
    do {
      printf("network interface %s is down", ifname);
      printf("\n");
    } while (0);
    close(s);
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(s, SIOCGIFADDR, &ifr, &ifrlen) < 0) {
    do {
      printf("ioctl(s, SIOCGIFADDR, ...): %m");
      printf("\n");
    } while (0);
    close(s);
    return -1;
  }
  ifaddr = (struct sockaddr_in *)&ifr.ifr_addr;
  if (addr)
    *addr = ifaddr->sin_addr;
  if (buf) {
    if (!inet_ntop(AF_INET, &ifaddr->sin_addr, buf, len)) {
      do {
        printf("inet_ntop(): %m");
        printf("\n");
      } while (0);
      close(s);
      return -1;
    }
  }
  if (mask) {
    strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
    if (ioctl(s, SIOCGIFNETMASK, &ifr, &ifrlen) < 0) {
      do {
        printf("ioctl(s, SIOCGIFNETMASK, ...): %m");
        printf("\n");
      } while (0);
      close(s);
      return -1;
    }
    *mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<getifaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0xc,-0x44(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119d <getifaddr+0x3d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a9 <getifaddr+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    143e <getifaddr+0x2de>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11fc <getifaddr+0x9c>
jmp    11d4 <getifaddr+0x74>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143e <getifaddr+0x2de>
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    -0x34(%rbp),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
lea    -0x44(%rbp),%rcx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1269 <getifaddr+0x109>
jmp    1239 <getifaddr+0xd9>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143e <getifaddr+0x2de>
mov    -0x40(%rbp),%eax
mov    0x2d5d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12b7 <getifaddr+0x157>
jmp    1283 <getifaddr+0x123>
mov    -0x10(%rbp),%rsi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143e <getifaddr+0x2de>
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2ceb(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    -0x34(%rbp),%edi
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
lea    -0x44(%rbp),%rcx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1324 <getifaddr+0x1c4>
jmp    12f4 <getifaddr+0x194>
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143e <getifaddr+0x2de>
lea    -0x40(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1347 <getifaddr+0x1e7>
mov    -0x28(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     13ae <getifaddr+0x24e>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    13a9 <getifaddr+0x249>
jmp    1379 <getifaddr+0x219>
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xc92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143e <getifaddr+0x2de>
jmp    13ae <getifaddr+0x24e>
cmpq   $0x0,-0x30(%rbp)
je     142f <getifaddr+0x2cf>
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2be9(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    -0x34(%rbp),%edi
mov    0x2c00(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
lea    -0x44(%rbp),%rcx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1426 <getifaddr+0x2c6>
jmp    13f6 <getifaddr+0x296>
lea    0xc91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xc15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    143e <getifaddr+0x2de>
mov    -0x30(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getifaddr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0xc,0x4(%rsp)
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     134e <getifaddr+0x1de>
mov    %rdi,%rbp
cmpb   $0x0,(%rdi)
je     134e <getifaddr+0x1de>
mov    %r8,%rbx
mov    %rcx,%r14
mov    %edx,%r13d
mov    %rsi,%r12
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     12e5 <getifaddr+0x175>
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%edx
mov    %rbp,%rsi
mov    %eax,%r15d
call   1050 <strncpy@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    %r15d,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
js     12ff <getifaddr+0x18f>
mov    %r15d,(%rsp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%rsp)
je     1308 <getifaddr+0x198>
mov    0x10(%rsp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1050 <strncpy@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
js     1328 <getifaddr+0x1b8>
test   %r14,%r14
je     1266 <getifaddr+0xf6>
mov    0xc(%rsp),%eax
mov    %eax,(%r14)
test   %r12,%r12
mov    $0xffffffff,%r15d
je     1292 <getifaddr+0x122>
lea    0xc(%rsp),%rsi
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rdx
mov    %r13d,%ecx
call   1030 <inet_ntop@plt>
test   %eax,%eax
je     1360 <getifaddr+0x1f0>
test   %rbx,%rbx
je     12d8 <getifaddr+0x168>
mov    0x10(%rsp),%edi
mov    0x2d0e(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1050 <strncpy@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
js     1382 <getifaddr+0x212>
mov    0xc(%rsp),%eax
mov    %eax,(%rbx)
mov    (%rsp),%edi
call   1080 <close@plt>
xor    %r15d,%r15d
jmp    134e <getifaddr+0x1de>
lea    0xd14(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
jmp    134e <getifaddr+0x1de>
lea    0xd1a(%rip),%rdi
jmp    132f <getifaddr+0x1bf>
lea    0xd31(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    (%rsp),%edi
jmp    1343 <getifaddr+0x1d3>
lea    0xd2e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd15(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    (%rsp),%edi
call   1080 <close@plt>
jmp    134e <getifaddr+0x1de>
lea    0xd03(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    134e <getifaddr+0x1de>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.ifreq = type { i32, i32, i32 }
%struct.sockaddr_in = type { %struct.in_addr }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [32 x i8] c"socket(PF_INET, SOCK_DGRAM): %m\00"
@.str.1 = [2 x i8] c"\0A\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.2 = [32 x i8] c"ioctl(s, SIOCGIFFLAGS, ...): %m\00"
@IFF_UP = global i32 0
@.str.3 = [29 x i8] c"network interface %s is down\00"
@SIOCGIFADDR = global i32 0
@.str.4 = [31 x i8] c"ioctl(s, SIOCGIFADDR, ...): %m\00"
@AF_INET = global i32 0
@.str.5 = [16 x i8] c"inet_ntop(): %m\00"
@SIOCGIFNETMASK = global i32 0
@.str.6 = [34 x i8] c"ioctl(s, SIOCGIFNETMASK, ...): %m\00"
define i32 @getifaddr(i8* %0, i8* %1, i32 %2, %struct.in_addr* %3, %struct.in_addr* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.in_addr*
%11 = alloca %struct.in_addr*
%12 = alloca i32
%13 = alloca %struct.ifreq
%14 = alloca i32
%15 = alloca %struct.sockaddr_in*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store %struct.in_addr* %3, %struct.in_addr** %10
store %struct.in_addr* %4, %struct.in_addr** %11
store i32 12, i32* %14
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %24
18: ; preds = %5
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18, %5
store i32 -1, i32* %6
br label %144
25: ; preds = %18
%26 = load i32, i32* @PF_INET
%27 = load i32, i32* @SOCK_DGRAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %25
br label %32
32: ; preds = %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %32
store i32 -1, i32* %6
br label %144
36: ; preds = %25
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %13, i32 0, i32 2
%38 = load i32, i32* %37
%39 = load i8*, i8** %7
%40 = load i32, i32* @IFNAMSIZ
%41 = call i32 @strncpy(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %12
%43 = load i32, i32* @SIOCGIFFLAGS
%44 = call i64 @ioctl(i32 %42, i32 %43, %struct.ifreq* %13, i32* %14)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %36
br label %47
47: ; preds = %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %47
%51 = load i32, i32* %12
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %6
br label %144
53: ; preds = %36
%54 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %13, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i32, i32* @IFF_UP
%57 = and i32 %55, %56
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %67
59: ; preds = %53
br label %60
60: ; preds = %59
%61 = load i8*, i8** %7
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %64
64: ; preds = %60
%65 = load i32, i32* %12
%66 = call i32 @close(i32 %65)
store i32 -1, i32* %6
br label %144
67: ; preds = %53
%68 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %13, i32 0, i32 2
%69 = load i32, i32* %68
%70 = load i8*, i8** %7
%71 = load i32, i32* @IFNAMSIZ
%72 = call i32 @strncpy(i32 %69, i8* %70, i32 %71)
%73 = load i32, i32* %12
%74 = load i32, i32* @SIOCGIFADDR
%75 = call i64 @ioctl(i32 %73, i32 %74, %struct.ifreq* %13, i32* %14)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %84
77: ; preds = %67
br label %78
78: ; preds = %77
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %81
81: ; preds = %78
%82 = load i32, i32* %12
%83 = call i32 @close(i32 %82)
store i32 -1, i32* %6
br label %144
84: ; preds = %67
%85 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %13, i32 0, i32 1
%86 = bitcast i32* %85 to %struct.sockaddr_in*
store %struct.sockaddr_in* %86, %struct.sockaddr_in** %15
%87 = load %struct.in_addr*, %struct.in_addr** %10
%88 = icmp ne %struct.in_addr* %87, null
br i1 %88, label %89, label %95
89: ; preds = %84
%90 = load %struct.in_addr*, %struct.in_addr** %10
%91 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%92 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %91, i32 0, i32 0
%93 = bitcast %struct.in_addr* %90 to i8*
%94 = bitcast %struct.in_addr* %92 to i8*
br label %95
95: ; preds = %89, %84
%96 = load i8*, i8** %8
%97 = icmp ne i8* %96, null
br i1 %97, label %98, label %114
98: ; preds = %95
%99 = load i32, i32* @AF_INET
%100 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%101 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %100, i32 0, i32 0
%102 = load i8*, i8** %8
%103 = load i32, i32* %9
%104 = call i32 @inet_ntop(i32 %99, %struct.in_addr* %101, i8* %102, i32 %103)
%105 = icmp ne i32 %104, 0
br i1 %105, label %113, label %106
106: ; preds = %98
br label %107
107: ; preds = %106
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %110
110: ; preds = %107
%111 = load i32, i32* %12
%112 = call i32 @close(i32 %111)
store i32 -1, i32* %6
br label %144
113: ; preds = %98
br label %114
114: ; preds = %113, %95
%115 = load %struct.in_addr*, %struct.in_addr** %11
%116 = icmp ne %struct.in_addr* %115, null
br i1 %116, label %117, label %141
117: ; preds = %114
%118 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %13, i32 0, i32 2
%119 = load i32, i32* %118
%120 = load i8*, i8** %7
%121 = load i32, i32* @IFNAMSIZ
%122 = call i32 @strncpy(i32 %119, i8* %120, i32 %121)
%123 = load i32, i32* %12
%124 = load i32, i32* @SIOCGIFNETMASK
%125 = call i64 @ioctl(i32 %123, i32 %124, %struct.ifreq* %13, i32* %14)
%126 = icmp slt i64 %125, 0
br i1 %126, label %127, label %134
127: ; preds = %117
br label %128
128: ; preds = %127
%129 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%130 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %131
131: ; preds = %128
%132 = load i32, i32* %12
%133 = call i32 @close(i32 %132)
store i32 -1, i32* %6
br label %144
134: ; preds = %117
%135 = load %struct.in_addr*, %struct.in_addr** %11
%136 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %13, i32 0, i32 1
%137 = bitcast i32* %136 to %struct.sockaddr_in*
%138 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %137, i32 0, i32 0
%139 = bitcast %struct.in_addr* %135 to i8*
%140 = bitcast %struct.in_addr* %138 to i8*
br label %141
141: ; preds = %134, %114
%142 = load i32, i32* %12
%143 = call i32 @close(i32 %142)
store i32 0, i32* %6
br label %144
144: ; preds = %141, %131, %110, %81, %64, %50, %35, %24
%145 = load i32, i32* %6
ret i32 %145
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.ifreq = type { i32, i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [32 x i8] c"socket(PF_INET, SOCK_DGRAM): %m\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.2 = [32 x i8] c"ioctl(s, SIOCGIFFLAGS, ...): %m\00"
@IFF_UP = global i32 0
@.str.3 = [29 x i8] c"network interface %s is down\00"
@SIOCGIFADDR = global i32 0
@.str.4 = [31 x i8] c"ioctl(s, SIOCGIFADDR, ...): %m\00"
@AF_INET = global i32 0
@.str.5 = [16 x i8] c"inet_ntop(): %m\00"
@SIOCGIFNETMASK = global i32 0
@.str.6 = [34 x i8] c"ioctl(s, SIOCGIFNETMASK, ...): %m\00"
define i32 @getifaddr(i8* %0, i8* %1, i32 %2, %struct.in_addr* writeonly %3, %struct.in_addr* writeonly %4) {
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = bitcast %struct.ifreq* %6 to i8*
%9 = bitcast i32* %7 to i8*
store i32 12, i32* %7
%10 = icmp eq i8* %0, null
br i1 %10, label %89, label %11
11: ; preds = %5
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
br i1 %13, label %89, label %14
14: ; preds = %11
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @putchar(i32 10)
br label %89
22: ; preds = %14
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i64 0, i32 2
%24 = load i32, i32* @IFNAMSIZ
%25 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %24)
%26 = load i32, i32* @SIOCGIFFLAGS
%27 = call i64 @ioctl(i32 %17, i32 %26, %struct.ifreq* %6, i32* %7)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @putchar(i32 10)
%32 = call i32 @close(i32 %17)
br label %89
33: ; preds = %22
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i64 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @IFF_UP
%37 = and i32 %36, %35
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%41 = call i32 @putchar(i32 10)
%42 = call i32 @close(i32 %17)
br label %89
43: ; preds = %33
%44 = load i32, i32* %23
%45 = load i32, i32* @IFNAMSIZ
%46 = call i32 @strncpy(i32 %44, i8* %0, i32 %45)
%47 = load i32, i32* @SIOCGIFADDR
%48 = call i64 @ioctl(i32 %17, i32 %47, %struct.ifreq* %6, i32* %7)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %43
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%52 = call i32 @putchar(i32 10)
%53 = call i32 @close(i32 %17)
br label %89
54: ; preds = %43
%55 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i64 0, i32 1
%56 = icmp eq %struct.in_addr* %3, null
br i1 %56, label %60, label %57
57: ; preds = %54
%58 = getelementptr %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%59 = load i32, i32* %55
store i32 %59, i32* %58
br label %60
60: ; preds = %57, %54
%61 = icmp eq i8* %1, null
br i1 %61, label %71, label %62
62: ; preds = %60
%63 = load i32, i32* @AF_INET
%64 = bitcast i32* %55 to %struct.in_addr*
%65 = call i32 @inet_ntop(i32 %63, %struct.in_addr* %64, i8* %1, i32 %2)
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%69 = call i32 @putchar(i32 10)
%70 = call i32 @close(i32 %17)
br label %89
71: ; preds = %62, %60
%72 = icmp eq %struct.in_addr* %4, null
br i1 %72, label %87, label %73
73: ; preds = %71
%74 = load i32, i32* %23
%75 = load i32, i32* @IFNAMSIZ
%76 = call i32 @strncpy(i32 %74, i8* %0, i32 %75)
%77 = load i32, i32* @SIOCGIFNETMASK
%78 = call i64 @ioctl(i32 %17, i32 %77, %struct.ifreq* %6, i32* %7)
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %84
80: ; preds = %73
%81 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%82 = call i32 @putchar(i32 10)
%83 = call i32 @close(i32 %17)
br label %89
84: ; preds = %73
%85 = getelementptr %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
%86 = load i32, i32* %55
store i32 %86, i32* %85
br label %87
87: ; preds = %84, %71
%88 = call i32 @close(i32 %17)
br label %89
89: ; preds = %5, %11, %87, %80, %67, %50, %39, %29, %19
%90 = phi i32 [ -1, %19 ], [ -1, %29 ], [ -1, %39 ], [ -1, %50 ], [ -1, %80 ], [ 0, %87 ], [ -1, %67 ], [ -1, %11 ], [ -1, %5 ]
ret i32 %90
}