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

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SIOCGIFNAME;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int i;
  int s = socket(PF_INET, SOCK_STREAM, 0);
  for (i = 2;; i++) {
    struct ifreq ifr;
    struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
    char *ip;
    ifr.ifr_ifindex = i;
    if (ioctl(s, SIOCGIFNAME, &ifr) < 0)
      break;
    if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
      continue;
    ip = inet_ntoa(sin->sin_addr);
    printf("%s\n", ip);
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
movl   $0x2,-0x14(%rbp)
lea    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11bf <main+0x6f>
jmp    1210 <main+0xc0>
mov    -0x18(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11e3 <main+0x93>
jmp    1202 <main+0xb2>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1189 <main+0x39>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
movl   $0x2,(%rsp)
mov    0x2e54(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     11f2 <main+0xa2>
mov    $0x3,%ebp
mov    0x2e45(%rip),%r13
mov    %rsp,%r14
lea    0xe53(%rip),%r15
jmp    11c9 <main+0x79>
nop
mov    %ebp,(%rsp)
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
add    $0x1,%ebp
test   %rax,%rax
js     11f2 <main+0xa2>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11b0 <main+0x60>
mov    0x4(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    11b0 <main+0x60>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFNAME = global i32 0
@SIOCGIFADDR = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.ifreq
%9 = alloca %struct.sockaddr_in*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %7
store i32 2, i32* %6
br label %14
14: ; preds = %37, %2
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%16 = bitcast i32* %15 to %struct.sockaddr_in*
store %struct.sockaddr_in* %16, %struct.sockaddr_in** %9
%17 = load i32, i32* %6
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %7
%20 = load i32, i32* @SIOCGIFNAME
%21 = call i64 @ioctl(i32 %19, i32 %20, %struct.ifreq* %8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %14
br label %40
24: ; preds = %14
%25 = load i32, i32* %7
%26 = load i32, i32* @SIOCGIFADDR
%27 = call i64 @ioctl(i32 %25, i32 %26, %struct.ifreq* %8)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %37
30: ; preds = %24
%31 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i8* @inet_ntoa(i32 %33)
store i8* %34, i8** %10
%35 = load i8*, i8** %10
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %30, %29
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
br label %14
40: ; preds = %23
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
ret i32 0
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFNAME = global i32 0
@SIOCGIFADDR = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.ifreq
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = bitcast %struct.ifreq* %3 to i8*
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 2, i32* %9
%10 = load i32, i32* @SIOCGIFNAME
%11 = call i64 @ioctl(i32 %6, i32 %10, %struct.ifreq* %3)
%12 = icmp slt i64 %11, 0
br i1 %12, label %27, label %13
13: ; preds = %2, %22
%14 = phi i32 [ %23, %22 ], [ 2, %2 ]
%15 = load i32, i32* @SIOCGIFADDR
%16 = call i64 @ioctl(i32 %6, i32 %15, %struct.ifreq* %3)
%17 = icmp slt i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = load i32, i32* %8
%20 = call i8* @inet_ntoa(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %18, %13
%23 = add nuw nsw i32 %14, 1
store i32 %23, i32* %9
%24 = load i32, i32* @SIOCGIFNAME
%25 = call i64 @ioctl(i32 %6, i32 %24, %struct.ifreq* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %13
27: ; preds = %22, %2
%28 = call i32 @close(i32 %6)
ret i32 0
}