#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in6 {
  int sin6_family;
  int /*<<< orphan*/ sin6_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  TYPE_1__ sin_addr;
};
typedef struct sockaddr const sockaddr;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ in6addr_loopback;
const struct sockaddr *loopback_address(int af) {
  static struct sockaddr_in sin;
  static struct sockaddr_in6 six;
  if (sin.sin_family == AF_UNSPEC) {
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    six.sin6_family = AF_INET6;
    six.sin6_addr = in6addr_loopback;
  }
  switch (af) {
  case AF_INET:
    return (struct sockaddr *)&sin;
  case AF_INET6:
    return (struct sockaddr *)&six;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<loopback_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2f0e(%rip),%rax
mov    0x2ebf(%rip),%rcx
cmp    (%rcx),%rax
jne    116a <loopback_address+0x5a>
movq   $0x81,0x2ef3(%rip)
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
call   1030 <htonl@plt>
mov    %eax,0x2ee7(%rip)
movl   $0x80,0x2ee5(%rip)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2eda(%rip)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0x80,%eax
je     11a3 <loopback_address+0x93>
jmp    1180 <loopback_address+0x70>
mov    -0x10(%rbp),%eax
sub    $0x81,%eax
jne    11b3 <loopback_address+0xa3>
jmp    1193 <loopback_address+0x83>
lea    0x2e96(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11bb <loopback_address+0xab>
lea    0x2e96(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11bb <loopback_address+0xab>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loopback_address>:
push   %rbx
mov    %edi,%ebx
mov    0x2f16(%rip),%rax
mov    0x2ec7(%rip),%rcx
cmp    (%rcx),%rax
jne    115e <loopback_address+0x4e>
movq   $0x81,0x2eff(%rip)
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
call   1030 <htonl@plt>
mov    %eax,0x2ef3(%rip)
movl   $0x80,0x2ef1(%rip)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2ee6(%rip)
xor    %ecx,%ecx
cmp    $0x80,%ebx
lea    0x2ed3(%rip),%rax
cmove  %rax,%rcx
cmp    $0x81,%ebx
lea    0x2eb2(%rip),%rax
cmovne %rcx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type opaque
@loopback_address.sin = internal global %struct.sockaddr_in zeroinitializer
@loopback_address.six = internal global %struct.sockaddr_in6 zeroinitializer
@AF_UNSPEC = global i64 0
@INADDR_LOOPBACK = global i32 0
@in6addr_loopback = global i32 0
define %struct.sockaddr* @loopback_address(i32 %0) {
%2 = alloca %struct.sockaddr*
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i64, i64* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @loopback_address.sin, i32 0, i32 0)
%5 = load i64, i64* @AF_UNSPEC
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %11
7: ; preds = %1
store i64 129, i64* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @loopback_address.sin, i32 0, i32 0)
%8 = load i32, i32* @INADDR_LOOPBACK
%9 = call i32 @htonl(i32 %8)
store i32 %9, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @loopback_address.sin, i32 0, i32 1, i32 0)
store i32 128, i32* getelementptr inbounds (%struct.sockaddr_in6, %struct.sockaddr_in6* @loopback_address.six, i32 0, i32 0)
%10 = load i32, i32* @in6addr_loopback
store i32 %10, i32* getelementptr inbounds (%struct.sockaddr_in6, %struct.sockaddr_in6* @loopback_address.six, i32 0, i32 1)
br label %11
11: ; preds = %7, %1
%12 = load i32, i32* %3
switch i32 %12, label %15 [
i32 129, label %13
i32 128, label %14
]
13: ; preds = %11
store %struct.sockaddr* bitcast (%struct.sockaddr_in* @loopback_address.sin to %struct.sockaddr*), %struct.sockaddr** %2
br label %16
14: ; preds = %11
store %struct.sockaddr* bitcast (%struct.sockaddr_in6* @loopback_address.six to %struct.sockaddr*), %struct.sockaddr** %2
br label %16
15: ; preds = %11
store %struct.sockaddr* null, %struct.sockaddr** %2
br label %16
16: ; preds = %15, %14, %13
%17 = load %struct.sockaddr*, %struct.sockaddr** %2
ret %struct.sockaddr* %17
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type opaque
@loopback_address.sin = internal global %struct.sockaddr_in zeroinitializer
@loopback_address.six = internal global %struct.sockaddr_in6 zeroinitializer
@AF_UNSPEC = global i64 0
@INADDR_LOOPBACK = global i32 0
@in6addr_loopback = global i32 0
define %struct.sockaddr* @loopback_address(i32 %0) {
%2 = load i64, i64* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @loopback_address.sin, i64 0, i32 0)
%3 = load i64, i64* @AF_UNSPEC
%4 = icmp eq i64 %2, %3
br i1 %4, label %5, label %9
5: ; preds = %1
store i64 129, i64* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @loopback_address.sin, i64 0, i32 0)
%6 = load i32, i32* @INADDR_LOOPBACK
%7 = tail call i32 @htonl(i32 %6)
store i32 %7, i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @loopback_address.sin, i64 0, i32 1, i32 0)
store i32 128, i32* getelementptr inbounds (%struct.sockaddr_in6, %struct.sockaddr_in6* @loopback_address.six, i64 0, i32 0)
%8 = load i32, i32* @in6addr_loopback
store i32 %8, i32* getelementptr inbounds (%struct.sockaddr_in6, %struct.sockaddr_in6* @loopback_address.six, i64 0, i32 1)
br label %9
9: ; preds = %5, %1
%10 = icmp eq i32 %0, 128
%11 = select i1 %10, %struct.sockaddr* bitcast (%struct.sockaddr_in6* @loopback_address.six to %struct.sockaddr*), %struct.sockaddr* null
%12 = icmp eq i32 %0, 129
%13 = select i1 %12, %struct.sockaddr* bitcast (%struct.sockaddr_in* @loopback_address.sin to %struct.sockaddr*), %struct.sockaddr* %11
ret %struct.sockaddr* %13
}