#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 ifreq {
  int /*<<< orphan*/ ifr_name;
};
struct ifconf {
  int ifc_len;
  int /*<<< orphan*/ *ifc_buf;
  struct ifreq *ifc_req;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFCONF;
int /*<<< orphan*/ add_interface(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int get_socket_for_af(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifconf *);
int /*<<< orphan*/ perror(char *);
int sockets_open(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ *xrealloc(int /*<<< orphan*/ *, int);
int if_readconf(void) {
  int numreqs = 30;
  struct ifconf ifc;
  struct ifreq *ifr;
  int n, err = -1;
  int skfd2;
  skfd2 = get_socket_for_af(AF_INET);
  if (skfd2 < 0) {
    fprintf(stderr, "warning: no inet socket available: %s\n", strerror(errno));
    skfd2 = sockets_open(0);
    if (skfd2 < 0)
      return -1;
  }
  ifc.ifc_buf = NULL;
  for (;;) {
    ifc.ifc_len = sizeof(struct ifreq) * numreqs;
    ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
    if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
      perror("SIOCGIFCONF");
      goto out;
    }
    if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
      numreqs += 10;
      continue;
    }
    break;
  }
  ifr = ifc.ifc_req;
  for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
    add_interface(ifr->ifr_name);
    ifr++;
  }
  err = 0;
out:
  free(ifc.ifc_buf);
  return err;
}


x86 assembly (clang, -O0):
<if_readconf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x1e,-0x8(%rbp)
movl   $0xffffffff,-0x30(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1050 <get_socket_for_af@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1212 <if_readconf+0x82>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xe18(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1080 <sockets_open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    120d <if_readconf+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <if_readconf+0x155>
jmp    1212 <if_readconf+0x82>
movq   $0x0,-0x18(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <xrealloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1265 <if_readconf+0xd5>
lea    0xdcc(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d6 <if_readconf+0x146>
movslq -0x20(%rbp),%rax
movslq -0x8(%rbp),%rcx
shl    $0x2,%rcx
cmp    %rcx,%rax
jne    1288 <if_readconf+0xf8>
mov    -0x8(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x8(%rbp)
jmp    121a <if_readconf+0x8a>
jmp    128d <if_readconf+0xfd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12cf <if_readconf+0x13f>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1090 <add_interface@plt>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
movslq -0x2c(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x2c(%rbp)
jmp    129c <if_readconf+0x10c>
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_readconf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1050 <get_socket_for_af@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11e7 <if_readconf+0x57>
mov    0x2e23(%rip),%rax
mov    (%rax),%ebx
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe34(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1080 <sockets_open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12af <if_readconf+0x11f>
movl   $0x78,(%rsp)
xor    %edi,%edi
mov    $0x78,%esi
call   1040 <xrealloc@plt>
mov    %rax,0x8(%rsp)
mov    0x2dc2(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     1253 <if_readconf+0xc3>
mov    $0x78,%ebx
mov    %rsp,%r14
movslq (%rsp),%rax
cmp    %rax,%rbx
jne    1267 <if_readconf+0xd7>
add    $0x28,%rbx
mov    %ebx,(%rsp)
mov    0x8(%rsp),%rdi
mov    %ebx,%esi
call   1040 <xrealloc@plt>
mov    %rax,0x8(%rsp)
mov    (%r15),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
jns    1220 <if_readconf+0x90>
lea    0xdcd(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1297 <if_readconf+0x107>
test   %eax,%eax
jle    1294 <if_readconf+0x104>
mov    0x10(%rsp),%rbp
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%rbp,%rbx,1),%edi
call   1090 <add_interface@plt>
add    $0x4,%rbx
cmp    (%rsp),%ebx
jl     1280 <if_readconf+0xf0>
jmp    1297 <if_readconf+0x107>
xor    %r14d,%r14d
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r14d
jmp    12a1 <if_readconf+0x111>

LLVM IR (-O0):
%struct.ifconf = type { i32, i32*, %struct.ifreq* }
%struct.ifreq = type { i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"warning: no inet socket available: %s\0A\00"
@errno = global i32 0
@SIOCGIFCONF = global i32 0
@.str.1 = [12 x i8] c"SIOCGIFCONF\00"
define i32 @if_readconf() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.ifconf
%4 = alloca %struct.ifreq*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 30, i32* %2
store i32 -1, i32* %6
%8 = load i32, i32* @AF_INET
%9 = call i32 @get_socket_for_af(i32 %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @sockets_open(i32 0)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %12
store i32 -1, i32* %1
br label %80
21: ; preds = %12
br label %22
22: ; preds = %21, %0
%23 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 1
store i32* null, i32** %23
br label %24
24: ; preds = %50, %22
%25 = load i32, i32* %2
%26 = sext i32 %25 to i64
%27 = mul i64 4, %26
%28 = trunc i64 %27 to i32
%29 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 1
%31 = load i32*, i32** %30
%32 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32* @xrealloc(i32* %31, i32 %33)
%35 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 1
store i32* %34, i32** %35
%36 = load i32, i32* %7
%37 = load i32, i32* @SIOCGIFCONF
%38 = call i64 @ioctl(i32 %36, i32 %37, %struct.ifconf* %3)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %24
%41 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %75
42: ; preds = %24
%43 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 0
%44 = load i32, i32* %43
%45 = sext i32 %44 to i64
%46 = load i32, i32* %2
%47 = sext i32 %46 to i64
%48 = mul i64 4, %47
%49 = icmp eq i64 %45, %48
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = load i32, i32* %2
%52 = add nsw i32 %51, 10
store i32 %52, i32* %2
br label %24
53: ; preds = %42
br label %54
54: ; preds = %53
%55 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 2
%56 = load %struct.ifreq*, %struct.ifreq** %55
store %struct.ifreq* %56, %struct.ifreq** %4
store i32 0, i32* %5
br label %57
57: ; preds = %69, %54
%58 = load i32, i32* %5
%59 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 0
%60 = load i32, i32* %59
%61 = icmp slt i32 %58, %60
br i1 %61, label %62, label %74
62: ; preds = %57
%63 = load %struct.ifreq*, %struct.ifreq** %4
%64 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i32 @add_interface(i32 %65)
%67 = load %struct.ifreq*, %struct.ifreq** %4
%68 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %67, i32 1
store %struct.ifreq* %68, %struct.ifreq** %4
br label %69
69: ; preds = %62
%70 = load i32, i32* %5
%71 = sext i32 %70 to i64
%72 = add i64 %71, 4
%73 = trunc i64 %72 to i32
store i32 %73, i32* %5
br label %57
74: ; preds = %57
store i32 0, i32* %6
br label %75
75: ; preds = %74, %40
%76 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i32 0, i32 1
%77 = load i32*, i32** %76
%78 = call i32 @free(i32* %77)
%79 = load i32, i32* %6
store i32 %79, i32* %1
br label %80
80: ; preds = %75, %20
%81 = load i32, i32* %1
ret i32 %81
}

LLVM IR (-O2):
%struct.ifconf = type { i32, i32*, %struct.ifreq* }
%struct.ifreq = type { i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"warning: no inet socket available: %s\0A\00"
@errno = global i32 0
@SIOCGIFCONF = global i32 0
@.str.1 = [12 x i8] c"SIOCGIFCONF\00"
define i32 @if_readconf() {
%1 = alloca %struct.ifconf
%2 = bitcast %struct.ifconf* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = tail call i32 @get_socket_for_af(i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @sockets_open(i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %57, label %13
13: ; preds = %6, %0
%14 = phi i32 [ %11, %6 ], [ %4, %0 ]
%15 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i64 0, i32 1
%16 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i64 0, i32 0
store i32 120, i32* %16
%17 = call i32* @xrealloc(i32* null, i32 120)
store i32* %17, i32** %15
%18 = load i32, i32* @SIOCGIFCONF
%19 = call i64 @ioctl(i32 %14, i32 %18, %struct.ifconf* %1)
%20 = icmp slt i64 %19, 0
br i1 %20, label %29, label %31
21: ; preds = %31
%22 = shl nuw nsw i64 %37, 2
%23 = trunc i64 %22 to i32
store i32 %23, i32* %16
%24 = load i32*, i32** %15
%25 = call i32* @xrealloc(i32* %24, i32 %23)
store i32* %25, i32** %15
%26 = load i32, i32* @SIOCGIFCONF
%27 = call i64 @ioctl(i32 %14, i32 %26, %struct.ifconf* %1)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %21, %13
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %53
31: ; preds = %13, %21
%32 = phi i64 [ %37, %21 ], [ 30, %13 ]
%33 = phi i64 [ %22, %21 ], [ 120, %13 ]
%34 = load i32, i32* %16
%35 = sext i32 %34 to i64
%36 = icmp eq i64 %33, %35
%37 = add nuw i64 %32, 10
br i1 %36, label %21, label %38
38: ; preds = %31
%39 = icmp sgt i32 %34, 0
br i1 %39, label %40, label %53
40: ; preds = %38
%41 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i64 0, i32 2
%42 = load %struct.ifreq*, %struct.ifreq** %41
br label %43
43: ; preds = %40, %43
%44 = phi i32 [ %50, %43 ], [ 0, %40 ]
%45 = phi %struct.ifreq* [ %49, %43 ], [ %42, %40 ]
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %45, i64 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @add_interface(i32 %47)
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %45, i64 1
%50 = add i32 %44, 4
%51 = load i32, i32* %16
%52 = icmp slt i32 %50, %51
br i1 %52, label %43, label %53
53: ; preds = %43, %38, %29
%54 = phi i32 [ -1, %29 ], [ 0, %38 ], [ 0, %43 ]
%55 = load i32*, i32** %15
%56 = call i32 @free(i32* %55)
br label %57
57: ; preds = %6, %53
%58 = phi i32 [ %54, %53 ], [ -1, %6 ]
ret i32 %58
}