#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 {
  scalar_t__ sin_family;
};
struct ifaddrs {
  struct ifaddrs *ifa_next;
  int /*<<< orphan*/ ifa_name;
  scalar_t__ ifa_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, struct sockaddr_in const *,
                          int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int get_ips(char *name, int name_len, struct sockaddr_in *ip) {
  static int first = 1;
  static struct ifaddrs *int_addrs;
  static const struct ifaddrs *int_cursor;
  const struct sockaddr_in *dl_addr;
  if (first == 1) {
    first = 0;
    if (getifaddrs(&int_addrs) == 0) {
      int_cursor = int_addrs;
    } else {
      first = 1;
      return 0;
    }
  }
  if (int_cursor != NULL) {
    while (int_cursor != NULL) {
      dl_addr = (const struct sockaddr_in *)int_cursor->ifa_addr;
      if (dl_addr != NULL && dl_addr->sin_family == AF_INET) {
        memcpy(ip, dl_addr, sizeof(struct sockaddr_in));
        strncpy(name, int_cursor->ifa_name, name_len - 1);
        name[name_len - 1] = '\0';
        int_cursor = int_cursor->ifa_next;
        return 1;
      }
      int_cursor = int_cursor->ifa_next;
    }
  }
  if (int_addrs != NULL) {
    freeifaddrs(int_addrs);
    int_addrs = NULL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_ips>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x1,0x2ee6(%rip)
jne    11ae <get_ips+0x6e>
movl   $0x0,0x2ed6(%rip)
lea    0x2edf(%rip),%rdi
call   1050 <getifaddrs@plt>
cmp    $0x0,%rax
jne    1193 <get_ips+0x53>
mov    0x2ec9(%rip),%rax
mov    %rax,0x2eca(%rip)
jmp    11a9 <get_ips+0x69>
movl   $0x1,0x2ea3(%rip)
movl   $0x0,-0x4(%rbp)
jmp    12a0 <get_ips+0x160>
jmp    11ae <get_ips+0x6e>
cmpq   $0x0,0x2ea2(%rip)
je     1274 <get_ips+0x134>
jmp    11c1 <get_ips+0x81>
cmpq   $0x0,0x2e8f(%rip)
je     126f <get_ips+0x12f>
mov    0x2e82(%rip),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1259 <get_ips+0x119>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    1259 <get_ips+0x119>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    0x8(%rax),%esi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2e0b(%rip)
movl   $0x1,-0x4(%rbp)
jmp    12a0 <get_ips+0x160>
mov    0x2df8(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2dee(%rip)
jmp    11c1 <get_ips+0x81>
jmp    1274 <get_ips+0x134>
cmpq   $0x0,0x2dd4(%rip)
je     1299 <get_ips+0x159>
mov    0x2dc7(%rip),%rdi
call   1060 <freeifaddrs@plt>
movq   $0x0,0x2db7(%rip)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ips>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
cmpb   $0x0,0x2ef4(%rip)
je     1167 <get_ips+0x27>
mov    0x2efb(%rip),%rax
test   %rax,%rax
jne    11a0 <get_ips+0x60>
jmp    1210 <get_ips+0xd0>
movb   $0x1,0x2eda(%rip)
lea    0x2edb(%rip),%rdi
call   1050 <getifaddrs@plt>
test   %rax,%rax
je     118d <get_ips+0x4d>
movb   $0x0,0x2ec2(%rip)
xor    %ebx,%ebx
jmp    122e <get_ips+0xee>
mov    0x2ebc(%rip),%rax
mov    %rax,0x2ebd(%rip)
test   %rax,%rax
je     1210 <get_ips+0xd0>
mov    0x2e41(%rip),%rcx
mov    (%rcx),%rcx
jmp    11bf <get_ips+0x7f>
nopl   0x0(%rax)
mov    (%rax),%rax
mov    %rax,0x2e9e(%rip)
test   %rax,%rax
je     1210 <get_ips+0xd0>
mov    0x10(%rax),%rsi
test   %rsi,%rsi
je     11b0 <get_ips+0x70>
cmp    %rcx,(%rsi)
jne    11b0 <get_ips+0x70>
mov    %rbp,%rdi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    0x2e77(%rip),%rax
mov    0x8(%rax),%esi
movslq %r14d,%rbp
lea    -0x1(%rbp),%rdx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%rbp,1)
mov    0x2e59(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2e4f(%rip)
mov    $0x1,%ebx
jmp    122e <get_ips+0xee>
mov    0x2e39(%rip),%rdi
xor    %ebx,%ebx
test   %rdi,%rdi
je     122e <get_ips+0xee>
call   1060 <freeifaddrs@plt>
movq   $0x0,0x2e22(%rip)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifaddrs = type { %struct.ifaddrs*, i32, i64 }
%struct.sockaddr_in = type { i64 }
@get_ips.first = internal global i32 1
@get_ips.int_addrs = internal global %struct.ifaddrs* null
@get_ips.int_cursor = internal global %struct.ifaddrs* null
@AF_INET = global i64 0
define i32 @get_ips(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.sockaddr_in*
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%9 = load i32, i32* @get_ips.first
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %18
11: ; preds = %3
store i32 0, i32* @get_ips.first
%12 = call i64 @getifaddrs(%struct.ifaddrs** @get_ips.int_addrs)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_addrs
store %struct.ifaddrs* %15, %struct.ifaddrs** @get_ips.int_cursor
br label %17
16: ; preds = %11
store i32 1, i32* @get_ips.first
store i32 0, i32* %4
br label %69
17: ; preds = %14
br label %18
18: ; preds = %17, %3
%19 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
%20 = icmp ne %struct.ifaddrs* %19, null
br i1 %20, label %21, label %62
21: ; preds = %18
br label %22
22: ; preds = %57, %21
%23 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
%24 = icmp ne %struct.ifaddrs* %23, null
br i1 %24, label %25, label %61
25: ; preds = %22
%26 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
%27 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %26, i32 0, i32 2
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to %struct.sockaddr_in*
store %struct.sockaddr_in* %29, %struct.sockaddr_in** %8
%30 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%31 = icmp ne %struct.sockaddr_in* %30, null
br i1 %31, label %32, label %57
32: ; preds = %25
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* @AF_INET
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %57
38: ; preds = %32
%39 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%40 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%41 = call i32 @memcpy(%struct.sockaddr_in* %39, %struct.sockaddr_in* %40, i32 8)
%42 = load i8*, i8** %5
%43 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
%44 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load i32, i32* %6
%47 = sub nsw i32 %46, 1
%48 = call i32 @strncpy(i8* %42, i32 %45, i32 %47)
%49 = load i8*, i8** %5
%50 = load i32, i32* %6
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
store i8 0, i8* %53
%54 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
%55 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %54, i32 0, i32 0
%56 = load %struct.ifaddrs*, %struct.ifaddrs** %55
store %struct.ifaddrs* %56, %struct.ifaddrs** @get_ips.int_cursor
store i32 1, i32* %4
br label %69
57: ; preds = %32, %25
%58 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
%59 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %58, i32 0, i32 0
%60 = load %struct.ifaddrs*, %struct.ifaddrs** %59
store %struct.ifaddrs* %60, %struct.ifaddrs** @get_ips.int_cursor
br label %22
61: ; preds = %22
br label %62
62: ; preds = %61, %18
%63 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_addrs
%64 = icmp ne %struct.ifaddrs* %63, null
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_addrs
%67 = call i32 @freeifaddrs(%struct.ifaddrs* %66)
store %struct.ifaddrs* null, %struct.ifaddrs** @get_ips.int_addrs
br label %68
68: ; preds = %65, %62
store i32 0, i32* %4
br label %69
69: ; preds = %68, %38, %16
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
%struct.ifaddrs = type { %struct.ifaddrs*, i32, i64 }
%struct.sockaddr_in = type { i64 }
@get_ips.first = internal unnamed_addr global i1 false
@get_ips.int_addrs = internal global %struct.ifaddrs* null
@get_ips.int_cursor = internal unnamed_addr global %struct.ifaddrs* null
@AF_INET = global i64 0
define i32 @get_ips(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = load i1, i1* @get_ips.first
br i1 %4, label %11, label %5
5: ; preds = %3
store i1 true, i1* @get_ips.first
%6 = tail call i64 @getifaddrs(%struct.ifaddrs** @get_ips.int_addrs)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_addrs
store %struct.ifaddrs* %9, %struct.ifaddrs** @get_ips.int_cursor
br label %13
10: ; preds = %5
store i1 false, i1* @get_ips.first
br label %50
11: ; preds = %3
%12 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
br label %13
13: ; preds = %11, %8
%14 = phi %struct.ifaddrs* [ %12, %11 ], [ %9, %8 ]
%15 = icmp eq %struct.ifaddrs* %14, null
br i1 %15, label %45, label %16
16: ; preds = %13
%17 = load i64, i64* @AF_INET
br label %18
18: ; preds = %16, %41
%19 = phi %struct.ifaddrs* [ %14, %16 ], [ %43, %41 ]
%20 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %19, i64 0, i32 2
%21 = load i64, i64* %20
%22 = icmp eq i64 %21, 0
br i1 %22, label %41, label %23
23: ; preds = %18
%24 = inttoptr i64 %21 to %struct.sockaddr_in*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i64 0, i32 0
%26 = load i64, i64* %25
%27 = icmp eq i64 %26, %17
br i1 %27, label %28, label %41
28: ; preds = %23
%29 = inttoptr i64 %21 to %struct.sockaddr_in*
%30 = tail call i32 @memcpy(%struct.sockaddr_in* %2, %struct.sockaddr_in* %29, i32 8)
%31 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
%32 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %31, i64 0, i32 1
%33 = load i32, i32* %32
%34 = add nsw i32 %1, -1
%35 = tail call i32 @strncpy(i8* %0, i32 %33, i32 %34)
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %0, i64 %36
store i8 0, i8* %37
%38 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_cursor
%39 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %38, i64 0, i32 0
%40 = load %struct.ifaddrs*, %struct.ifaddrs** %39
store %struct.ifaddrs* %40, %struct.ifaddrs** @get_ips.int_cursor
br label %50
41: ; preds = %23, %18
%42 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %19, i64 0, i32 0
%43 = load %struct.ifaddrs*, %struct.ifaddrs** %42
store %struct.ifaddrs* %43, %struct.ifaddrs** @get_ips.int_cursor
%44 = icmp eq %struct.ifaddrs* %43, null
br i1 %44, label %45, label %18
45: ; preds = %41, %13
%46 = load %struct.ifaddrs*, %struct.ifaddrs** @get_ips.int_addrs
%47 = icmp eq %struct.ifaddrs* %46, null
br i1 %47, label %50, label %48
48: ; preds = %45
%49 = tail call i32 @freeifaddrs(%struct.ifaddrs* %46)
store %struct.ifaddrs* null, %struct.ifaddrs** @get_ips.int_addrs
br label %50
50: ; preds = %45, %48, %28, %10
%51 = phi i32 [ 1, %28 ], [ 0, %10 ], [ 0, %48 ], [ 0, %45 ]
ret i32 %51
}