#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct TYPE_2__ {
  int sa_family;
  int /*<<< orphan*/ *sa_data;
};
struct ifreq {
  char *ifr_name;
  TYPE_1__ ifr_addr;
};
struct ifconf {
  int ifc_len;
  scalar_t__ ifc_buf;
};
typedef int /*<<< orphan*/ saddr;
typedef scalar_t__ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFCONF;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ inet_ntop(int, int /*<<< orphan*/ *, char *, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifconf *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ printhex(unsigned char const *, int);
char *realloc(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void listifaces() {
  struct ifconf ifc;
  char *buf = 0;
  int buflen = sizeof(struct ifreq) * 20;
  int s, i;
  int j;
  char saddr[256];
  s = socket(AF_INET, SOCK_DGRAM, 0);
  do {
    buflen += buflen;
    buf = realloc(buf, buflen);
    ifc.ifc_len = buflen;
    ifc.ifc_buf = (caddr_t)buf;
    if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
      perror("ioctl");
      close(s);
      free(buf);
      return;
    }
    printf("%d - %d - %d\n", buflen, ifc.ifc_len, (int)sizeof(struct ifreq));
    printf(" %d\n", IFNAMSIZ);
    printf(" %d %d\n", (int)sizeof(struct sockaddr),
           (int)sizeof(struct sockaddr_in));
  } while (buflen == ifc.ifc_len);
  printhex((const unsigned char *)ifc.ifc_buf, ifc.ifc_len);
  j = 0;
  for (i = 0; i < ifc.ifc_len;) {
    const struct ifreq *ifrp = (const struct ifreq *)(buf + i);
    i += sizeof(ifrp->ifr_name) + 16;
    saddr[0] = '\0';
    inet_ntop(ifrp->ifr_addr.sa_family, &(ifrp->ifr_addr.sa_data[2]), saddr,
              sizeof(saddr));
    printf("%2d %d %d %s %s\n", j, ifrp->ifr_addr.sa_family, -1, ifrp->ifr_name,
           saddr);
    j++;
  }
  free(buf);
  close(s);
}


x86 assembly (clang, -O0):
<listifaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movq   $0x0,-0x18(%rbp)
movl   $0x1e0,-0x1c(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    122e <listifaces+0x9e>
lea    0xded(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
jmp    1343 <listifaces+0x1b3>
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xdcb(%rip),%rdi
mov    $0x18,%ecx
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdaf(%rip),%rdi
mov    $0x4,%edx
mov    %edx,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     11c6 <listifaces+0x36>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1090 <printhex@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1332 <listifaces+0x1a2>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x138(%rbp)
movslq -0x24(%rbp),%rax
add    $0x18,%rax
mov    %eax,-0x24(%rbp)
movb   $0x0,-0x130(%rbp)
mov    -0x138(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x138(%rbp),%rax
mov    0x10(%rax),%rsi
add    $0x8,%rsi
lea    -0x130(%rbp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    -0x28(%rbp),%esi
mov    -0x138(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x138(%rbp),%rax
mov    (%rax),%r8
lea    -0x130(%rbp),%r9
lea    0xd04(%rip),%rdi
mov    $0xffffffff,%ecx
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1299 <listifaces+0x109>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listifaces>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%r14d
mov    $0x1e0,%ebp
mov    0x2e07(%rip),%r12
lea    0xe3f(%rip),%r13
lea    0xe3d(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
add    %ebp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,%rbx
mov    %ebp,(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rsp,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     12e3 <listifaces+0x153>
mov    (%rsp),%edx
lea    0xde8(%rip),%rdi
mov    %ebp,%esi
mov    $0x18,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%esi
cmp    %esi,%ebp
je     11e0 <listifaces+0x50>
mov    0x8(%rsp),%rdi
call   1090 <printhex@plt>
cmpl   $0x0,(%rsp)
jle    12c1 <listifaces+0x131>
lea    0x10(%rsp),%r12
xor    %ebp,%ebp
xor    %r13d,%r13d
nop
movslq %r13d,%r15
lea    0x18(%r15),%r13d
movb   $0x0,0x10(%rsp)
mov    0x8(%rbx,%r15,1),%edi
mov    0x10(%rbx,%r15,1),%rsi
add    $0x8,%rsi
mov    %r12,%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    0x8(%rbx,%r15,1),%edx
mov    (%rbx,%r15,1),%r8
lea    0xd75(%rip),%rdi
mov    %ebp,%esi
mov    $0xffffffff,%ecx
mov    %r12,%r9
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
cmp    (%rsp),%r13d
jl     1270 <listifaces+0xe0>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14d,%edi
call   1070 <close@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd16(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
jmp    12d1 <listifaces+0x141>

LLVM IR (-O0):
%struct.ifconf = type { i32, i64 }
%struct.ifreq = type { i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFCONF = global i32 0
@.str = [6 x i8] c"ioctl\00"
@.str.1 = [14 x i8] c"%d - %d - %d\0A\00"
@.str.2 = [5 x i8] c" %d\0A\00"
@IFNAMSIZ = global i32 0
@.str.3 = [8 x i8] c" %d %d\0A\00"
@.str.4 = [17 x i8] c"%2d %d %d %s %s\0A\00"
define void @listifaces() {
%1 = alloca %struct.ifconf
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca %struct.ifreq*
store i8* null, i8** %2
store i32 480, i32* %3
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
br label %12
12: ; preds = %42, %0
%13 = load i32, i32* %3
%14 = load i32, i32* %3
%15 = add nsw i32 %14, %13
store i32 %15, i32* %3
%16 = load i8*, i8** %2
%17 = load i32, i32* %3
%18 = call i8* @realloc(i8* %16, i32 %17)
store i8* %18, i8** %2
%19 = load i32, i32* %3
%20 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i8*, i8** %2
%22 = ptrtoint i8* %21 to i64
%23 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i32 0, i32 1
store i64 %22, i64* %23
%24 = load i32, i32* %4
%25 = load i32, i32* @SIOCGIFCONF
%26 = call i64 @ioctl(i32 %24, i32 %25, %struct.ifconf* %1)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %12
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = load i8*, i8** %2
%33 = call i32 @free(i8* %32)
br label %98
34: ; preds = %12
%35 = load i32, i32* %3
%36 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 %37, i32 24)
%39 = load i32, i32* @IFNAMSIZ
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 4, i32 4)
br label %42
42: ; preds = %34
%43 = load i32, i32* %3
%44 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i32 0, i32 0
%45 = load i32, i32* %44
%46 = icmp eq i32 %43, %45
br i1 %46, label %12, label %47
47: ; preds = %42
%48 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i32 0, i32 1
%49 = load i64, i64* %48
%50 = inttoptr i64 %49 to i8*
%51 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @printhex(i8* %50, i32 %52)
store i32 0, i32* %6
store i32 0, i32* %5
br label %54
54: ; preds = %59, %47
%55 = load i32, i32* %5
%56 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i32 0, i32 0
%57 = load i32, i32* %56
%58 = icmp slt i32 %55, %57
br i1 %58, label %59, label %93
59: ; preds = %54
%60 = load i8*, i8** %2
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = bitcast i8* %63 to %struct.ifreq*
store %struct.ifreq* %64, %struct.ifreq** %8
%65 = load i32, i32* %5
%66 = sext i32 %65 to i64
%67 = add i64 %66, 24
%68 = trunc i64 %67 to i32
store i32 %68, i32* %5
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
store i8 0, i8* %69
%70 = load %struct.ifreq*, %struct.ifreq** %8
%71 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %70, i32 0, i32 1
%72 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = load %struct.ifreq*, %struct.ifreq** %8
%75 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %74, i32 0, i32 1
%76 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %75, i32 0, i32 1
%77 = load i32*, i32** %76
%78 = getelementptr inbounds i32, i32* %77, i64 2
%79 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%80 = call i32 @inet_ntop(i32 %73, i32* %78, i8* %79, i32 256)
%81 = load i32, i32* %6
%82 = load %struct.ifreq*, %struct.ifreq** %8
%83 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %82, i32 0, i32 1
%84 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %83, i32 0, i32 0
%85 = load i32, i32* %84
%86 = load %struct.ifreq*, %struct.ifreq** %8
%87 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %86, i32 0, i32 0
%88 = load i8*, i8** %87
%89 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%90 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %81, i32 %85, i32 -1, i8* %88, i8* %89)
%91 = load i32, i32* %6
%92 = add nsw i32 %91, 1
store i32 %92, i32* %6
br label %54
93: ; preds = %54
%94 = load i8*, i8** %2
%95 = call i32 @free(i8* %94)
%96 = load i32, i32* %4
%97 = call i32 @close(i32 %96)
br label %98
98: ; preds = %93, %28
ret void
}

LLVM IR (-O2):
%struct.ifconf = type { i32, i64 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFCONF = global i32 0
@.str = [6 x i8] c"ioctl\00"
@.str.1 = [14 x i8] c"%d - %d - %d\0A\00"
@.str.2 = [5 x i8] c" %d\0A\00"
@IFNAMSIZ = global i32 0
@.str.3 = [8 x i8] c" %d %d\0A\00"
@.str.4 = [17 x i8] c"%2d %d %d %s %s\0A\00"
define void @listifaces() {
%1 = alloca %struct.ifconf
%2 = alloca [256 x i8]
%3 = bitcast %struct.ifconf* %1 to i8*
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i64 0, i32 0
%9 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %1, i64 0, i32 1
br label %10
10: ; preds = %23, %0
%11 = phi i32 [ 480, %0 ], [ %13, %23 ]
%12 = phi i8* [ null, %0 ], [ %14, %23 ]
%13 = shl nsw i32 %11, 1
%14 = call i8* @realloc(i8* %12, i32 %13)
store i32 %13, i32* %8
%15 = ptrtoint i8* %14 to i64
store i64 %15, i64* %9
%16 = load i32, i32* @SIOCGIFCONF
%17 = call i64 @ioctl(i32 %7, i32 %16, %struct.ifconf* %1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @close(i32 %7)
%22 = call i32 @free(i8* %14)
br label %61
23: ; preds = %10
%24 = load i32, i32* %8
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %24, i32 24)
%26 = load i32, i32* @IFNAMSIZ
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 4, i32 4)
%29 = load i32, i32* %8
%30 = icmp eq i32 %13, %29
br i1 %30, label %10, label %31
31: ; preds = %23
%32 = load i64, i64* %9
%33 = inttoptr i64 %32 to i8*
%34 = call i32 @printhex(i8* %33, i32 %29)
%35 = load i32, i32* %8
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %58
37: ; preds = %31, %37
%38 = phi i32 [ %55, %37 ], [ 0, %31 ]
%39 = phi i32 [ %42, %37 ], [ 0, %31 ]
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %14, i64 %40
%42 = add i32 %39, 24
store i8 0, i8* %4
%43 = getelementptr inbounds i8, i8* %41, i64 8
%44 = bitcast i8* %43 to i32*
%45 = load i32, i32* %44
%46 = getelementptr inbounds i8, i8* %43, i64 8
%47 = bitcast i8* %46 to i32**
%48 = load i32*, i32** %47
%49 = getelementptr inbounds i32, i32* %48, i64 2
%50 = call i32 @inet_ntop(i32 %45, i32* %49, i8* %4, i32 256)
%51 = load i32, i32* %44
%52 = bitcast i8* %41 to i8**
%53 = load i8*, i8** %52
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %38, i32 %51, i32 -1, i8* %53, i8* %4)
%55 = add nuw nsw i32 %38, 1
%56 = load i32, i32* %8
%57 = icmp slt i32 %42, %56
br i1 %57, label %37, label %58
58: ; preds = %37, %31
%59 = call i32 @free(i8* %14)
%60 = call i32 @close(i32 %7)
br label %61
61: ; preds = %58, %19
ret void
}