#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;
};
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 *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifconf *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ printhex(unsigned char const *, int);
char *realloc(char *, int);
int /*<<< orphan*/ sockaddr_to_string(TYPE_1__ *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void listifaces(void) {
  struct ifconf ifc;
  char *buf = NULL;
  int buflen;
  int s, i;
  int j;
  char saddr[256];
  buflen = sizeof(struct ifreq) * 10;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  do {
    buflen += buflen;
    if (buflen > 0)
      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("buffer length=%d - buffer used=%d - sizeof(struct ifreq)=%d\n",
           buflen, ifc.ifc_len, (int)sizeof(struct ifreq));
    printf("IFNAMSIZ=%d  ", IFNAMSIZ);
    printf("sizeof(struct sockaddr)=%d   sizeof(struct sockaddr_in)=%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);
  printf("off index fam name             address\n");
  for (i = 0, j = 0; i < ifc.ifc_len; j++) {
    const struct ifreq *ifrp = (const struct ifreq *)(buf + i);
    saddr[0] = '\0';
    sockaddr_to_string(&ifrp->ifr_addr, saddr, sizeof(saddr));
    printf("0x%03x %2d   %2d %-16s %s\n", i, j, ifrp->ifr_addr.sa_family,
           ifrp->ifr_name, saddr);
    i += sizeof(struct ifreq);
  }
  free(buf);
  close(s);
}


x86 assembly (clang, -O0):
<listifaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movq   $0x0,-0x18(%rbp)
movl   $0xa0,-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)
cmpl   $0x0,-0x1c(%rbp)
jle    11e9 <listifaces+0x59>
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    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1238 <listifaces+0xa8>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    134b <listifaces+0x1bb>
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xdc1(%rip),%rdi
mov    $0x10,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde2(%rip),%rdi
mov    $0x4,%edx
mov    %edx,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    11c6 <listifaces+0x36>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1090 <printhex@plt>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    133a <listifaces+0x1aa>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x138(%rbp)
movb   $0x0,-0x130(%rbp)
mov    -0x138(%rbp),%rdi
add    $0x8,%rdi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
call   1070 <sockaddr_to_string@plt>
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x138(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x138(%rbp),%rax
mov    (%rax),%r8
lea    -0x130(%rbp),%r9
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x24(%rbp),%rax
add    $0x10,%rax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12b1 <listifaces+0x121>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%edi
call   1060 <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    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
mov    $0xa0,%ebp
mov    0x2df7(%rip),%r12
lea    0xe63(%rip),%rbx
lea    0xe6a(%rip),%r15
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%eax
lea    (%rax,%rax,1),%ebp
test   %eax,%eax
jle    1206 <listifaces+0x66>
mov    %r13,%rdi
mov    %ebp,%esi
call   1090 <realloc@plt>
mov    %rax,%r13
mov    %ebp,(%rsp)
mov    %r13,0x8(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rsp,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     1305 <listifaces+0x165>
mov    (%rsp),%edx
lea    0xdd1(%rip),%rdi
mov    %ebp,%esi
mov    $0x10,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%esi
mov    %rbx,%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
jle    11f0 <listifaces+0x50>
mov    0x8(%rsp),%rdi
call   10a0 <printhex@plt>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x0,(%rsp)
jle    12e3 <listifaces+0x143>
lea    0x10(%rsp),%r12
lea    0xdf9(%rip),%r15
xor    %ebp,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rbx
movb   $0x0,0x10(%rsp)
lea    (%rbx,%r13,1),%rdi
add    $0x8,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1080 <sockaddr_to_string@plt>
mov    0x8(%r13,%rbx,1),%ecx
mov    0x0(%r13,%rbx,1),%r8
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %r12,%r9
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x10,%ebx
add    $0x1,%ebp
cmp    (%rsp),%ebx
jl     12a0 <listifaces+0x100>
mov    %r13,%rdi
call   1030 <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    0xcf4(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    12f3 <listifaces+0x153>

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

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