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

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memcpy(struct sockaddr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ sock_addrlen(int /*<<< orphan*/);
scalar_t__ sock_cmpaddr(struct sockaddr const *, int /*<<< orphan*/,
                        int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int sock_getifinfo(const struct sockaddr *addr, char *ifname,
                   struct sockaddr *broadcast, struct sockaddr *netmask) {
  struct ifaddrs *ifap, *ifa;
  int rc;
  if (getifaddrs(&ifap) == -1)
    return 0;
  rc = 0;
  for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
    if (sock_cmpaddr(addr, ifa->ifa_addr, 0))
      continue;
    if (addr->sa_family == AF_INET) {
      if (broadcast)
        memcpy(broadcast, ifa->ifa_broadaddr, sock_addrlen(ifa->ifa_broadaddr));
      if (netmask)
        memcpy(netmask, ifa->ifa_netmask, sock_addrlen(ifa->ifa_netmask));
    }
    if (ifname)
      strcpy(ifname, ifa->ifa_name);
    rc = 1;
    break;
  }
  freeifaddrs(ifap);
  return rc;
}


x86 assembly (clang, -O0):
<sock_getifinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    1196 <sock_getifinfo+0x36>
movl   $0x0,-0x4(%rbp)
jmp    12a1 <sock_getifinfo+0x141>
movl   $0x0,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1292 <sock_getifinfo+0x132>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
xor    %edx,%edx
call   1060 <sock_cmpaddr@plt>
cmp    $0x0,%rax
je     11d1 <sock_getifinfo+0x71>
jmp    1281 <sock_getifinfo+0x121>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
jne    125b <sock_getifinfo+0xfb>
cmpq   $0x0,-0x20(%rbp)
je     121f <sock_getifinfo+0xbf>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edi
call   1040 <sock_addrlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%esi
mov    %eax,%edx
call   1050 <memcpy@plt>
cmpq   $0x0,-0x28(%rbp)
je     1256 <sock_getifinfo+0xf6>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <sock_addrlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    %eax,%edx
call   1050 <memcpy@plt>
jmp    125b <sock_getifinfo+0xfb>
cmpq   $0x0,-0x18(%rbp)
je     1275 <sock_getifinfo+0x115>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
movl   $0x1,-0x3c(%rbp)
jmp    1292 <sock_getifinfo+0x132>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11a5 <sock_getifinfo+0x45>
mov    -0x30(%rbp),%rdi
call   1080 <freeifaddrs@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_getifinfo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rsp,%rdi
call   1070 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     11af <sock_getifinfo+0x4f>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     11ab <sock_getifinfo+0x4b>
nopl   (%rax)
mov    0xc(%rbx),%esi
mov    %r13,%rdi
xor    %edx,%edx
call   1060 <sock_cmpaddr@plt>
test   %rax,%rax
je     11b3 <sock_getifinfo+0x53>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    1190 <sock_getifinfo+0x30>
xor    %ebp,%ebp
jmp    120d <sock_getifinfo+0xad>
xor    %ebp,%ebp
jmp    1216 <sock_getifinfo+0xb6>
mov    0x0(%r13),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jne    11f9 <sock_getifinfo+0x99>
test   %r12,%r12
je     11de <sock_getifinfo+0x7e>
mov    0x8(%rbx),%ebp
mov    %ebp,%edi
call   1040 <sock_addrlen@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <memcpy@plt>
test   %r14,%r14
je     11f9 <sock_getifinfo+0x99>
mov    0x4(%rbx),%ebp
mov    %ebp,%edi
call   1040 <sock_addrlen@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    $0x1,%ebp
test   %r15,%r15
je     120d <sock_getifinfo+0xad>
mov    (%rbx),%esi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    (%rsp),%rdi
call   1080 <freeifaddrs@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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