#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 /*<<< orphan*/ uint32_t;
struct sockaddr_ll {
  size_t sll_halen;
  int /*<<< orphan*/ sll_addr;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_scope_id;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
  int /*<<< orphan*/ sa_data;
};
typedef int sa_family_t;

/* Variables and functions */
#define AF_INET 130
#define AF_INET6 129
#define AF_PACKET 128
int /*<<< orphan*/ IN6_IS_ADDR_LINKLOCAL(void *);
int /*<<< orphan*/ IN6_IS_ADDR_MC_LINKLOCAL(void *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void *, size_t);
void ifa_make_sockaddr(sa_family_t family, struct sockaddr *sa, void *p,
                       size_t len, uint32_t scope, uint32_t scopeid) {
  if (sa == NULL)
    return;
  switch (family) {
  case AF_INET:
    memcpy(&((struct sockaddr_in *)sa)->sin_addr, (char *)p, len);
    break;
  case AF_INET6:
    memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr, (char *)p, len);
    if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p)) {
      ((struct sockaddr_in6 *)sa)->sin6_scope_id = scopeid;
    }
    break;
  case AF_PACKET:
    memcpy(((struct sockaddr_ll *)sa)->sll_addr, (char *)p, len);
    ((struct sockaddr_ll *)sa)->sll_halen = len;
    break;
  default:
    memcpy(sa->sa_data, p, len);
    break;
  }
  sa->sa_family = family;
}


x86 assembly (clang, -O0):
<ifa_make_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115f <ifa_make_sockaddr+0x2f>
jmp    123d <ifa_make_sockaddr+0x10d>
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     11fc <ifa_make_sockaddr+0xcc>
jmp    1175 <ifa_make_sockaddr+0x45>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
je     11b3 <ifa_make_sockaddr+0x83>
jmp    1188 <ifa_make_sockaddr+0x58>
mov    -0x2c(%rbp),%eax
sub    $0x82,%eax
jne    1220 <ifa_make_sockaddr+0xf0>
jmp    119b <ifa_make_sockaddr+0x6b>
mov    -0x10(%rbp),%rax
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
jmp    1234 <ifa_make_sockaddr+0x104>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1040 <IN6_IS_ADDR_LINKLOCAL@plt>
cmp    $0x0,%eax
jne    11ee <ifa_make_sockaddr+0xbe>
mov    -0x18(%rbp),%rdi
call   1050 <IN6_IS_ADDR_MC_LINKLOCAL@plt>
cmp    $0x0,%eax
je     11f7 <ifa_make_sockaddr+0xc7>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1234 <ifa_make_sockaddr+0x104>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1234 <ifa_make_sockaddr+0x104>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifa_make_sockaddr>:
test   %rsi,%rsi
je     11b4 <ifa_make_sockaddr+0x84>
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x80,%edi
je     1163 <ifa_make_sockaddr+0x33>
cmp    $0x81,%ebp
je     1176 <ifa_make_sockaddr+0x46>
cmp    $0x82,%ebp
jne    119a <ifa_make_sockaddr+0x6a>
mov    %ebx,%edi
jmp    119d <ifa_make_sockaddr+0x6d>
mov    0x8(%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <memcpy@plt>
mov    %r15,(%rbx)
jmp    11a8 <ifa_make_sockaddr+0x78>
lea    0x4(%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <memcpy@plt>
mov    %r14,%rdi
call   1040 <IN6_IS_ADDR_LINKLOCAL@plt>
test   %eax,%eax
jne    11a8 <ifa_make_sockaddr+0x78>
mov    %r14,%rdi
call   1050 <IN6_IS_ADDR_MC_LINKLOCAL@plt>
jmp    11a8 <ifa_make_sockaddr+0x78>
mov    0x4(%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <memcpy@plt>
mov    %ebp,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr_ll = type { i64, i32 }
define void @ifa_make_sockaddr(i32 %0, %struct.sockaddr* %1, i8* %2, i64 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca %struct.sockaddr*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %7
store %struct.sockaddr* %1, %struct.sockaddr** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%13 = load %struct.sockaddr*, %struct.sockaddr** %8
%14 = icmp eq %struct.sockaddr* %13, null
br i1 %14, label %15, label %16
15: ; preds = %6
br label %70
16: ; preds = %6
%17 = load i32, i32* %7
switch i32 %17, label %59 [
i32 130, label %18
i32 129, label %26
i32 128, label %47
]
18: ; preds = %16
%19 = load %struct.sockaddr*, %struct.sockaddr** %8
%20 = bitcast %struct.sockaddr* %19 to %struct.sockaddr_in*
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 0
%22 = ptrtoint i32* %21 to i32
%23 = load i8*, i8** %9
%24 = load i64, i64* %10
%25 = call i32 @memcpy(i32 %22, i8* %23, i64 %24)
br label %66
26: ; preds = %16
%27 = load %struct.sockaddr*, %struct.sockaddr** %8
%28 = bitcast %struct.sockaddr* %27 to %struct.sockaddr_in6*
%29 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %28, i32 0, i32 1
%30 = ptrtoint i32* %29 to i32
%31 = load i8*, i8** %9
%32 = load i64, i64* %10
%33 = call i32 @memcpy(i32 %30, i8* %31, i64 %32)
%34 = load i8*, i8** %9
%35 = call i32 @IN6_IS_ADDR_LINKLOCAL(i8* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %26
%38 = load i8*, i8** %9
%39 = call i32 @IN6_IS_ADDR_MC_LINKLOCAL(i8* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37, %26
%42 = load i32, i32* %12
%43 = load %struct.sockaddr*, %struct.sockaddr** %8
%44 = bitcast %struct.sockaddr* %43 to %struct.sockaddr_in6*
%45 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %44, i32 0, i32 0
store i32 %42, i32* %45
br label %46
46: ; preds = %41, %37
br label %66
47: ; preds = %16
%48 = load %struct.sockaddr*, %struct.sockaddr** %8
%49 = bitcast %struct.sockaddr* %48 to %struct.sockaddr_ll*
%50 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load i8*, i8** %9
%53 = load i64, i64* %10
%54 = call i32 @memcpy(i32 %51, i8* %52, i64 %53)
%55 = load i64, i64* %10
%56 = load %struct.sockaddr*, %struct.sockaddr** %8
%57 = bitcast %struct.sockaddr* %56 to %struct.sockaddr_ll*
%58 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %57, i32 0, i32 0
store i64 %55, i64* %58
br label %66
59: ; preds = %16
%60 = load %struct.sockaddr*, %struct.sockaddr** %8
%61 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %60, i32 0, i32 1
%62 = load i32, i32* %61
%63 = load i8*, i8** %9
%64 = load i64, i64* %10
%65 = call i32 @memcpy(i32 %62, i8* %63, i64 %64)
br label %66
66: ; preds = %59, %47, %46, %18
%67 = load i32, i32* %7
%68 = load %struct.sockaddr*, %struct.sockaddr** %8
%69 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %68, i32 0, i32 0
store i32 %67, i32* %69
br label %70
70: ; preds = %66, %15
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32, i32 }
define void @ifa_make_sockaddr(i32 %0, %struct.sockaddr* %1, i8* %2, i64 %3, i32 %4, i32 %5) {
%7 = icmp eq %struct.sockaddr* %1, null
br i1 %7, label %33, label %8
8: ; preds = %6
switch i32 %0, label %27 [
i32 130, label %9
i32 129, label %13
i32 128, label %22
]
9: ; preds = %8
%10 = ptrtoint %struct.sockaddr* %1 to i64
%11 = trunc i64 %10 to i32
%12 = tail call i32 @memcpy(i32 %11, i8* %2, i64 %3)
br label %31
13: ; preds = %8
%14 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 1
%15 = ptrtoint i32* %14 to i64
%16 = trunc i64 %15 to i32
%17 = tail call i32 @memcpy(i32 %16, i8* %2, i64 %3)
%18 = tail call i32 @IN6_IS_ADDR_LINKLOCAL(i8* %2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %13
%21 = tail call i32 @IN6_IS_ADDR_MC_LINKLOCAL(i8* %2)
br label %31
22: ; preds = %8
%23 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 1, i32 0
%24 = load i32, i32* %23
%25 = tail call i32 @memcpy(i32 %24, i8* %2, i64 %3)
%26 = bitcast %struct.sockaddr* %1 to i64*
store i64 %3, i64* %26
br label %31
27: ; preds = %8
%28 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 1
%29 = load i32, i32* %28
%30 = tail call i32 @memcpy(i32 %29, i8* %2, i64 %3)
br label %31
31: ; preds = %20, %13, %27, %22, %9
%32 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
store i32 %0, i32* %32
br label %33
33: ; preds = %6, %31
ret void
}