#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_storage {
  int dummy;
};
struct sockaddr_in6 {
  int sin6_family;
  void *sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int sin_family;
  void *sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
void *htons(int /*<<< orphan*/);
int /*<<< orphan*/ in6addr_any;
struct sockaddr_storage *malloc(int);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
struct sockaddr *new_any_sockaddr(int family, uint16_t port) {
  struct sockaddr_storage *addr;
  struct sockaddr_in *addr4;
  struct sockaddr_in6 *addr6;
  addr = malloc(sizeof(struct sockaddr_storage));
  memset(addr, 0, sizeof(struct sockaddr_storage));
  switch (family) {
  case AF_INET:
    addr4 = (struct sockaddr_in *)addr;
    addr4->sin_family = AF_INET;
    addr4->sin_addr.s_addr = htonl(INADDR_ANY);
    addr4->sin_port = htons(port);
    break;
  case AF_INET6:
    addr6 = (struct sockaddr_in6 *)addr;
    addr6->sin6_family = AF_INET6;
    addr6->sin6_addr = in6addr_any;
    addr6->sin6_port = htons(port);
    break;
  default:
    error(1, 0, "Unsupported family %d", family);
  }
  return (struct sockaddr *)addr;
}


x86 assembly (clang, -O0):
<new_any_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x4,%edi
mov    %edi,-0x28(%rbp)
call   1060 <malloc@plt>
mov    -0x28(%rbp),%edx
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     11e7 <new_any_sockaddr+0x97>
jmp    1193 <new_any_sockaddr+0x43>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
jne    1221 <new_any_sockaddr+0xd1>
jmp    11a6 <new_any_sockaddr+0x56>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x81,(%rax)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    1237 <new_any_sockaddr+0xe7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x80,(%rax)
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    1237 <new_any_sockaddr+0xe7>
mov    -0x4(%rbp),%ecx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xdce(%rip),%rdx
call   1070 <error@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_any_sockaddr>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
cmp    $0x80,%ebp
je     119b <new_any_sockaddr+0x4b>
cmp    $0x81,%ebp
jne    11bb <new_any_sockaddr+0x6b>
movl   $0x81,(%rbx)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
jmp    11aa <new_any_sockaddr+0x5a>
movl   $0x80,(%rbx)
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rbx)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%rbx)
jmp    11d0 <new_any_sockaddr+0x80>
lea    0xe3e(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %ebp,%ecx
call   1070 <error@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { i32, i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in6 = type { i32, i8*, i32 }
@INADDR_ANY = global i32 0
@in6addr_any = global i32 0
@.str = [22 x i8] c"Unsupported family %d\00"
define %struct.sockaddr* @new_any_sockaddr(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage*
%6 = alloca %struct.sockaddr_in*
%7 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = call %struct.sockaddr_storage* @malloc(i32 4)
store %struct.sockaddr_storage* %8, %struct.sockaddr_storage** %5
%9 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%10 = call i32 @memset(%struct.sockaddr_storage* %9, i32 0, i32 4)
%11 = load i32, i32* %3
switch i32 %11, label %38 [
i32 129, label %12
i32 128, label %26
]
12: ; preds = %2
%13 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%14 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr_in*
store %struct.sockaddr_in* %14, %struct.sockaddr_in** %6
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i32 129, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = call i32 @htonl(i32 %17)
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 2
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %18, i32* %21
%22 = load i32, i32* %4
%23 = call i8* @htons(i32 %22)
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 1
store i8* %23, i8** %25
br label %41
26: ; preds = %2
%27 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%28 = bitcast %struct.sockaddr_storage* %27 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %28, %struct.sockaddr_in6** %7
%29 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %29, i32 0, i32 0
store i32 128, i32* %30
%31 = load i32, i32* @in6addr_any
%32 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %32, i32 0, i32 2
store i32 %31, i32* %33
%34 = load i32, i32* %4
%35 = call i8* @htons(i32 %34)
%36 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %36, i32 0, i32 1
store i8* %35, i8** %37
br label %41
38: ; preds = %2
%39 = load i32, i32* %3
%40 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %38, %26, %12
%42 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%43 = bitcast %struct.sockaddr_storage* %42 to %struct.sockaddr*
ret %struct.sockaddr* %43
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
@INADDR_ANY = global i32 0
@in6addr_any = global i32 0
@.str = [22 x i8] c"Unsupported family %d\00"
define %struct.sockaddr* @new_any_sockaddr(i32 %0, i32 %1) {
%3 = tail call %struct.sockaddr_storage* @malloc(i32 4)
%4 = tail call i32 @memset(%struct.sockaddr_storage* %3, i32 0, i32 4)
switch i32 %0, label %20 [
i32 129, label %5
i32 128, label %13
]
5: ; preds = %2
%6 = getelementptr %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 0, i32 0
store i32 129, i32* %6
%7 = load i32, i32* @INADDR_ANY
%8 = tail call i32 @htonl(i32 %7)
%9 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 4, i32 0
store i32 %8, i32* %9
%10 = tail call i8* @htons(i32 %1)
%11 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 2
%12 = bitcast %struct.sockaddr_storage* %11 to i8**
store i8* %10, i8** %12
br label %22
13: ; preds = %2
%14 = getelementptr %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 0, i32 0
store i32 128, i32* %14
%15 = load i32, i32* @in6addr_any
%16 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 4, i32 0
store i32 %15, i32* %16
%17 = tail call i8* @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 2
%19 = bitcast %struct.sockaddr_storage* %18 to i8**
store i8* %17, i8** %19
br label %22
20: ; preds = %2
%21 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %22
22: ; preds = %20, %13, %5
%23 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
ret %struct.sockaddr* %23
}