#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_un {
  int sun_family;
  int /*<<< orphan*/ sun_path;
};
struct sockaddr_storage {
  int dummy;
};
struct sockaddr_in6 {
  int sin6_family;
  int /*<<< orphan*/ sin6_addr;
  void *sin6_port;
};
struct sockaddr_in {
  int sin_family;
  int /*<<< orphan*/ sin_addr;
  void *sin_port;
};
typedef int sa_family_t;

/* Variables and functions */
#define AF_INET 130
#define AF_INET6 129
#define AF_UNIX 128
void *htons(int);
int /*<<< orphan*/ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ inet_pton(int, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
void init_socket_addr(sa_family_t family, struct sockaddr_storage *sock_addr,
                      const char *addr, int port) {
  switch (family) {
  case AF_INET:
    memset(sock_addr, 0, sizeof(struct sockaddr_in));
    ((struct sockaddr_in *)sock_addr)->sin_family = family;
    ((struct sockaddr_in *)sock_addr)->sin_port = htons(port);
    inet_aton(addr, &((struct sockaddr_in *)sock_addr)->sin_addr);
    break;
  case AF_INET6:
    memset(sock_addr, 0, sizeof(struct sockaddr_in6));
    ((struct sockaddr_in6 *)sock_addr)->sin6_family = family;
    ((struct sockaddr_in6 *)sock_addr)->sin6_port = htons(port);
    inet_pton(family, addr, &((struct sockaddr_in6 *)sock_addr)->sin6_addr);
    break;
  case AF_UNIX:
    ((struct sockaddr_un *)sock_addr)->sun_family = family;
    strncpy(((struct sockaddr_un *)sock_addr)->sun_path, addr,
            sizeof(((struct sockaddr_un *)sock_addr)->sun_path));
    break;
  default:
    break;
  }
}


x86 assembly (clang, -O0):
<init_socket_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     1229 <init_socket_addr+0xd9>
jmp    117c <init_socket_addr+0x2c>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
je     11e4 <init_socket_addr+0x94>
jmp    118f <init_socket_addr+0x3f>
mov    -0x20(%rbp),%eax
sub    $0x82,%eax
jne    124c <init_socket_addr+0xfc>
jmp    11a2 <init_socket_addr+0x52>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
call   1070 <inet_aton@plt>
jmp    1251 <init_socket_addr+0x101>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
add    $0x4,%rdx
call   1060 <inet_pton@plt>
jmp    1251 <init_socket_addr+0x101>
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
jmp    1251 <init_socket_addr+0x101>
jmp    1251 <init_socket_addr+0x101>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_socket_addr>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
cmp    $0x80,%edi
je     11ab <init_socket_addr+0x5b>
mov    %ecx,%ebp
cmp    $0x81,%edi
je     11c5 <init_socket_addr+0x75>
cmp    $0x82,%edi
jne    11fd <init_socket_addr+0xad>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x82,(%rbx)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rbx)
add    $0x4,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <inet_aton@plt>
movl   $0x80,(%rbx)
mov    0x4(%rbx),%edi
mov    %r14,%rsi
mov    $0x4,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <strncpy@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x81,(%rbx)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rbx)
add    $0x4,%rbx
mov    $0x81,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <inet_pton@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { i32, i32, i8* }
%struct.sockaddr_in6 = type { i32, i32, i8* }
%struct.sockaddr_un = type { i32, i32 }
define void @init_socket_addr(i32 %0, %struct.sockaddr_storage* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_storage*
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr_storage* %1, %struct.sockaddr_storage** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%9 = load i32, i32* %5
switch i32 %9, label %56 [
i32 130, label %10
i32 129, label %27
i32 128, label %45
]
10: ; preds = %4
%11 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%12 = call i32 @memset(%struct.sockaddr_storage* %11, i32 0, i32 16)
%13 = load i32, i32* %5
%14 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%15 = bitcast %struct.sockaddr_storage* %14 to %struct.sockaddr_in*
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i32 %13, i32* %16
%17 = load i32, i32* %8
%18 = call i8* @htons(i32 %17)
%19 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%20 = bitcast %struct.sockaddr_storage* %19 to %struct.sockaddr_in*
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 2
store i8* %18, i8** %21
%22 = load i8*, i8** %7
%23 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%24 = bitcast %struct.sockaddr_storage* %23 to %struct.sockaddr_in*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 1
%26 = call i32 @inet_aton(i8* %22, i32* %25)
br label %57
27: ; preds = %4
%28 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%29 = call i32 @memset(%struct.sockaddr_storage* %28, i32 0, i32 16)
%30 = load i32, i32* %5
%31 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%32 = bitcast %struct.sockaddr_storage* %31 to %struct.sockaddr_in6*
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %32, i32 0, i32 0
store i32 %30, i32* %33
%34 = load i32, i32* %8
%35 = call i8* @htons(i32 %34)
%36 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%37 = bitcast %struct.sockaddr_storage* %36 to %struct.sockaddr_in6*
%38 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %37, i32 0, i32 2
store i8* %35, i8** %38
%39 = load i32, i32* %5
%40 = load i8*, i8** %7
%41 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%42 = bitcast %struct.sockaddr_storage* %41 to %struct.sockaddr_in6*
%43 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %42, i32 0, i32 1
%44 = call i32 @inet_pton(i32 %39, i8* %40, i32* %43)
br label %57
45: ; preds = %4
%46 = load i32, i32* %5
%47 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%48 = bitcast %struct.sockaddr_storage* %47 to %struct.sockaddr_un*
%49 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %48, i32 0, i32 0
store i32 %46, i32* %49
%50 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %6
%51 = bitcast %struct.sockaddr_storage* %50 to %struct.sockaddr_un*
%52 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load i8*, i8** %7
%55 = call i32 @strncpy(i32 %53, i8* %54, i32 4)
br label %57
56: ; preds = %4
br label %57
57: ; preds = %56, %45, %27, %10
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
define void @init_socket_addr(i32 %0, %struct.sockaddr_storage* %1, i8* %2, i32 %3) {
switch i32 %0, label %26 [
i32 130, label %5
i32 129, label %13
i32 128, label %21
]
5: ; preds = %4
%6 = tail call i32 @memset(%struct.sockaddr_storage* %1, i32 0, i32 16)
%7 = getelementptr %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 0, i32 0
store i32 130, i32* %7
%8 = tail call i8* @htons(i32 %3)
%9 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 2
%10 = bitcast %struct.sockaddr_storage* %9 to i8**
store i8* %8, i8** %10
%11 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 1, i32 0
%12 = tail call i32 @inet_aton(i8* %2, i32* %11)
br label %26
13: ; preds = %4
%14 = tail call i32 @memset(%struct.sockaddr_storage* %1, i32 0, i32 16)
%15 = getelementptr %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 0, i32 0
store i32 129, i32* %15
%16 = tail call i8* @htons(i32 %3)
%17 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 2
%18 = bitcast %struct.sockaddr_storage* %17 to i8**
store i8* %16, i8** %18
%19 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 1, i32 0
%20 = tail call i32 @inet_pton(i32 129, i8* %2, i32* %19)
br label %26
21: ; preds = %4
%22 = getelementptr %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 0, i32 0
store i32 128, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %1, i64 1, i32 0
%24 = load i32, i32* %23
%25 = tail call i32 @strncpy(i32 %24, i8* %2, i32 4)
br label %26
26: ; preds = %4, %21, %13, %5
ret void
}