#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 */
typedef int u_int32_t;
struct sockaddr {
  scalar_t__ sa_family;
  int /*<<< orphan*/ *sa_data;
};
struct in6_addr {
  int dummy;
};
struct ifaddrs {
  scalar_t__ ifa_addr;
  int /*<<< orphan*/ ifa_name;
  struct ifaddrs *ifa_next;
};

/* Variables and functions */
scalar_t__ AF_INET6;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ dprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int const htonl(int);
int /*<<< orphan*/ memcpy(struct in6_addr *, int /*<<< orphan*/ *, int);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
int get_linklocal(const char *ifname, struct in6_addr *linklocal) {
  struct ifaddrs *ifa, *ifap;
  struct sockaddr *sd;
  if (getifaddrs(&ifap) < 0) {
    dprintf(LOG_ERR, "getifaddrs error");
    return -1;
  }
  for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    if (strcmp(ifa->ifa_name, ifname))
      continue;
    sd = (struct sockaddr *)ifa->ifa_addr;
    if (!sd || sd->sa_family != AF_INET6)
      continue;
    if (!((((__const u_int32_t *)(&sd->sa_data[6]))[0] & htonl(0xffc00000)) ==
          htonl(0xfe800000)))
      continue;
    memcpy(linklocal, &sd->sa_data[6], sizeof(*linklocal));
    break;
  }
  freeifaddrs(ifap);
  return 0;
}


x86 assembly (clang, -O0):
<get_linklocal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
call   1070 <getifaddrs@plt>
cmp    $0x0,%rax
jge    11a4 <get_linklocal+0x44>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1040 <dprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <get_linklocal+0x126>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1276 <get_linklocal+0x116>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11d6 <get_linklocal+0x76>
jmp    1265 <get_linklocal+0x105>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1203 <get_linklocal+0xa3>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
je     1208 <get_linklocal+0xa8>
jmp    1265 <get_linklocal+0x105>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x18(%rax),%eax
mov    %eax,-0x38(%rbp)
mov    $0xffc00000,%edi
call   1030 <htonl@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
and    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    $0xfe800000,%edi
call   1030 <htonl@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
je     1246 <get_linklocal+0xe6>
jmp    1265 <get_linklocal+0x105>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rsi
add    $0x18,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
jmp    1276 <get_linklocal+0x116>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11ac <get_linklocal+0x4c>
mov    -0x28(%rbp),%rdi
call   1080 <freeifaddrs@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_linklocal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x8(%rsp),%rdi
call   1070 <getifaddrs@plt>
test   %rax,%rax
js     1213 <get_linklocal+0xb3>
mov    %rbx,0x10(%rsp)
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1205 <get_linklocal+0xa5>
mov    0x2e4b(%rip),%r13
jmp    11a9 <get_linklocal+0x49>
nop
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     1205 <get_linklocal+0xa5>
mov    0x8(%rbx),%edi
mov    %r12,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11a0 <get_linklocal+0x40>
mov    (%rbx),%rax
test   %rax,%rax
je     11a0 <get_linklocal+0x40>
mov    (%rax),%rcx
cmp    0x0(%r13),%rcx
jne    11a0 <get_linklocal+0x40>
mov    0x8(%rax),%r15
mov    0x18(%r15),%r14d
mov    $0xffc00000,%edi
call   1030 <htonl@plt>
mov    %eax,%ebp
and    %r14d,%ebp
mov    $0xfe800000,%edi
call   1030 <htonl@plt>
cmp    %eax,%ebp
jne    11a0 <get_linklocal+0x40>
add    $0x18,%r15
mov    0x10(%rsp),%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeifaddrs@plt>
xor    %eax,%eax
jmp    122d <get_linklocal+0xcd>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1040 <dprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
%struct.ifaddrs = type { i64, i32, %struct.ifaddrs* }
%struct.sockaddr = type { i64, i32* }
@LOG_ERR = global i32 0
@.str = [17 x i8] c"getifaddrs error\00"
@AF_INET6 = global i64 0
define i32 @get_linklocal(i8* %0, %struct.in6_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in6_addr*
%6 = alloca %struct.ifaddrs*
%7 = alloca %struct.ifaddrs*
%8 = alloca %struct.sockaddr*
store i8* %0, i8** %4
store %struct.in6_addr* %1, %struct.in6_addr** %5
%9 = call i64 @getifaddrs(%struct.ifaddrs** %7)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @LOG_ERR
%13 = call i32 @dprintf(i32 %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
14: ; preds = %2
%15 = load %struct.ifaddrs*, %struct.ifaddrs** %7
store %struct.ifaddrs* %15, %struct.ifaddrs** %6
br label %16
16: ; preds = %60, %14
%17 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%18 = icmp ne %struct.ifaddrs* %17, null
br i1 %18, label %19, label %64
19: ; preds = %16
%20 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%21 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i8*, i8** %4
%24 = call i64 @strcmp(i32 %22, i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
br label %60
27: ; preds = %19
%28 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%29 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.sockaddr*
store %struct.sockaddr* %31, %struct.sockaddr** %8
%32 = load %struct.sockaddr*, %struct.sockaddr** %8
%33 = icmp ne %struct.sockaddr* %32, null
br i1 %33, label %34, label %40
34: ; preds = %27
%35 = load %struct.sockaddr*, %struct.sockaddr** %8
%36 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = load i64, i64* @AF_INET6
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %34, %27
br label %60
41: ; preds = %34
%42 = load %struct.sockaddr*, %struct.sockaddr** %8
%43 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %42, i32 0, i32 1
%44 = load i32*, i32** %43
%45 = getelementptr inbounds i32, i32* %44, i64 6
%46 = getelementptr inbounds i32, i32* %45, i64 0
%47 = load i32, i32* %46
%48 = call i32 @htonl(i32 -4194304)
%49 = and i32 %47, %48
%50 = call i32 @htonl(i32 -25165824)
%51 = icmp eq i32 %49, %50
br i1 %51, label %53, label %52
52: ; preds = %41
br label %60
53: ; preds = %41
%54 = load %struct.in6_addr*, %struct.in6_addr** %5
%55 = load %struct.sockaddr*, %struct.sockaddr** %8
%56 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %55, i32 0, i32 1
%57 = load i32*, i32** %56
%58 = getelementptr inbounds i32, i32* %57, i64 6
%59 = call i32 @memcpy(%struct.in6_addr* %54, i32* %58, i32 4)
br label %64
60: ; preds = %52, %40, %26
%61 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%62 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %61, i32 0, i32 2
%63 = load %struct.ifaddrs*, %struct.ifaddrs** %62
store %struct.ifaddrs* %63, %struct.ifaddrs** %6
br label %16
64: ; preds = %53, %16
%65 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%66 = call i32 @freeifaddrs(%struct.ifaddrs* %65)
store i32 0, i32* %3
br label %67
67: ; preds = %64, %11
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
%struct.ifaddrs = type { i64, i32, %struct.ifaddrs* }
%struct.sockaddr = type { i64, i32* }
@LOG_ERR = global i32 0
@.str = [17 x i8] c"getifaddrs error\00"
@AF_INET6 = global i64 0
define i32 @get_linklocal(i8* %0, %struct.in6_addr* %1) {
%3 = alloca %struct.ifaddrs*
%4 = bitcast %struct.ifaddrs** %3 to i8*
%5 = call i64 @getifaddrs(%struct.ifaddrs** %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%9 = icmp eq %struct.ifaddrs* %8, null
br i1 %9, label %45, label %13
10: ; preds = %2
%11 = load i32, i32* @LOG_ERR
%12 = call i32 @dprintf(i32 %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %48
13: ; preds = %7, %41
%14 = phi %struct.ifaddrs* [ %43, %41 ], [ %8, %7 ]
%15 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %14, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i64 @strcmp(i32 %16, i8* %0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %41
19: ; preds = %13
%20 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %14, i64 0, i32 0
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to %struct.sockaddr*
%23 = icmp eq i64 %21, 0
br i1 %23, label %41, label %24
24: ; preds = %19
%25 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %22, i64 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @AF_INET6
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %41
29: ; preds = %24
%30 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %22, i64 0, i32 1
%31 = load i32*, i32** %30
%32 = getelementptr inbounds i32, i32* %31, i64 6
%33 = load i32, i32* %32
%34 = call i32 @htonl(i32 -4194304)
%35 = and i32 %34, %33
%36 = call i32 @htonl(i32 -25165824)
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = getelementptr inbounds i32, i32* %31, i64 6
%40 = call i32 @memcpy(%struct.in6_addr* %1, i32* %39, i32 4)
br label %45
41: ; preds = %29, %19, %24, %13
%42 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %14, i64 0, i32 2
%43 = load %struct.ifaddrs*, %struct.ifaddrs** %42
%44 = icmp eq %struct.ifaddrs* %43, null
br i1 %44, label %45, label %13
45: ; preds = %41, %7, %38
%46 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%47 = call i32 @freeifaddrs(%struct.ifaddrs* %46)
br label %48
48: ; preds = %45, %10
%49 = phi i32 [ -1, %10 ], [ 0, %45 ]
ret i32 %49
}