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

/* Variables and functions */
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 sock_getbroadcastaddr(const struct sockaddr *addr,
                          struct sockaddr *broadcastaddr) {
  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;
    memcpy(broadcastaddr, ifa->ifa_broadaddr, sock_addrlen(ifa->ifa_broadaddr));
    rc = 1;
    break;
  }
  freeifaddrs(ifap);
  return rc;
}


x86 assembly (clang, -O0):
<sock_getbroadcastaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    117e <sock_getbroadcastaddr+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    120f <sock_getbroadcastaddr+0xbf>
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1200 <sock_getbroadcastaddr+0xb0>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
xor    %edx,%edx
call   1050 <sock_cmpaddr@plt>
cmp    $0x0,%rax
je     11b9 <sock_getbroadcastaddr+0x69>
jmp    11ef <sock_getbroadcastaddr+0x9f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1030 <sock_addrlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    %eax,%edx
call   1040 <memcpy@plt>
movl   $0x1,-0x2c(%rbp)
jmp    1200 <sock_getbroadcastaddr+0xb0>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    118d <sock_getbroadcastaddr+0x3d>
mov    -0x20(%rbp),%rdi
call   1070 <freeifaddrs@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_getbroadcastaddr>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     119f <sock_getbroadcastaddr+0x4f>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     119b <sock_getbroadcastaddr+0x4b>
nopl   0x0(%rax,%rax,1)
mov    0x4(%rbx),%esi
mov    %r15,%rdi
xor    %edx,%edx
call   1050 <sock_cmpaddr@plt>
test   %rax,%rax
je     11a3 <sock_getbroadcastaddr+0x53>
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    1180 <sock_getbroadcastaddr+0x30>
xor    %ebx,%ebx
jmp    11bd <sock_getbroadcastaddr+0x6d>
xor    %ebx,%ebx
jmp    11c7 <sock_getbroadcastaddr+0x77>
mov    (%rbx),%ebx
mov    %ebx,%edi
call   1030 <sock_addrlen@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    $0x1,%ebx
mov    0x8(%rsp),%rdi
call   1070 <freeifaddrs@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.ifaddrs = type { i32, i32, %struct.ifaddrs* }
define i32 @sock_getbroadcastaddr(%struct.sockaddr* %0, %struct.sockaddr* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr*
%5 = alloca %struct.sockaddr*
%6 = alloca %struct.ifaddrs*
%7 = alloca %struct.ifaddrs*
%8 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
%9 = call i32 @getifaddrs(%struct.ifaddrs** %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %43
12: ; preds = %2
store i32 0, i32* %8
%13 = load %struct.ifaddrs*, %struct.ifaddrs** %6
store %struct.ifaddrs* %13, %struct.ifaddrs** %7
br label %14
14: ; preds = %35, %12
%15 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%16 = icmp ne %struct.ifaddrs* %15, null
br i1 %16, label %17, label %39
17: ; preds = %14
%18 = load %struct.sockaddr*, %struct.sockaddr** %4
%19 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%20 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = call i64 @sock_cmpaddr(%struct.sockaddr* %18, i32 %21, i32 0)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
br label %35
25: ; preds = %17
%26 = load %struct.sockaddr*, %struct.sockaddr** %5
%27 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%28 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%31 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @sock_addrlen(i32 %32)
%34 = call i32 @memcpy(%struct.sockaddr* %26, i32 %29, i32 %33)
store i32 1, i32* %8
br label %39
35: ; preds = %24
%36 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%37 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %36, i32 0, i32 2
%38 = load %struct.ifaddrs*, %struct.ifaddrs** %37
store %struct.ifaddrs* %38, %struct.ifaddrs** %7
br label %14
39: ; preds = %25, %14
%40 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%41 = call i32 @freeifaddrs(%struct.ifaddrs* %40)
%42 = load i32, i32* %8
store i32 %42, i32* %3
br label %43
43: ; preds = %39, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.ifaddrs = type { i32, i32, %struct.ifaddrs* }
define i32 @sock_getbroadcastaddr(%struct.sockaddr* %0, %struct.sockaddr* %1) {
%3 = alloca %struct.ifaddrs*
%4 = bitcast %struct.ifaddrs** %3 to i8*
%5 = call i32 @getifaddrs(%struct.ifaddrs** %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %29, label %7
7: ; preds = %2
%8 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%9 = icmp eq %struct.ifaddrs* %8, null
br i1 %9, label %25, label %10
10: ; preds = %7, %21
%11 = phi %struct.ifaddrs* [ %23, %21 ], [ %8, %7 ]
%12 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %11, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i64 @sock_cmpaddr(%struct.sockaddr* %0, i32 %13, i32 0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %11, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @sock_addrlen(i32 %18)
%20 = call i32 @memcpy(%struct.sockaddr* %1, i32 %18, i32 %19)
br label %25
21: ; preds = %10
%22 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %11, i64 0, i32 2
%23 = load %struct.ifaddrs*, %struct.ifaddrs** %22
%24 = icmp eq %struct.ifaddrs* %23, null
br i1 %24, label %25, label %10
25: ; preds = %21, %7, %16
%26 = phi i32 [ 1, %16 ], [ 0, %7 ], [ 0, %21 ]
%27 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%28 = call i32 @freeifaddrs(%struct.ifaddrs* %27)
br label %29
29: ; preds = %2, %25
%30 = phi i32 [ %26, %25 ], [ 0, %2 ]
ret i32 %30
}