#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 {
  char *sun_path;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};

/* Variables and functions */
#define AF_INET 130
#define AF_INET6 129
#define AF_UNIX 128
int /*<<< orphan*/ inet_ntop(int const, int /*<<< orphan*/ *, char *, size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, ...);
char *addr2str(struct sockaddr *addr, char *buf, size_t len) {
  switch (addr->sa_family) {
  case AF_INET: {
    struct sockaddr_in *sa = (struct sockaddr_in *)addr;
    inet_ntop(AF_INET, &sa->sin_addr, buf, len);
    break;
  }
  case AF_INET6: {
    struct sockaddr_in6 *sa = (struct sockaddr_in6 *)addr;
    inet_ntop(AF_INET, &sa->sin6_addr, buf, len);
    break;
  }
  case AF_UNIX: {
    struct sockaddr_un *sa = (struct sockaddr_un *)addr;
    snprintf(buf, len, "%s", sa->sun_path);
    break;
  }
  default:
    snprintf(buf, len, "unknown host");
    break;
  }
  return buf;
}


x86 assembly (clang, -O0):
<addr2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
sub    $0x80,%eax
je     11b9 <addr2str+0x99>
jmp    114d <addr2str+0x2d>
mov    -0x34(%rbp),%eax
sub    $0x81,%eax
je     1196 <addr2str+0x76>
jmp    1160 <addr2str+0x40>
mov    -0x34(%rbp),%eax
sub    $0x82,%eax
jne    11e3 <addr2str+0xc3>
jmp    1173 <addr2str+0x53>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x82,%edi
call   1030 <inet_ntop@plt>
jmp    11f9 <addr2str+0xd9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x82,%edi
call   1030 <inet_ntop@plt>
jmp    11f9 <addr2str+0xd9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xe29(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    11f9 <addr2str+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    0xe11(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr2str>:
push   %rbx
mov    %rdx,%rax
mov    %rsi,%rbx
mov    %rdi,%rsi
mov    (%rdi),%ecx
cmp    $0x80,%ecx
je     1156 <addr2str+0x36>
cmp    $0x81,%ecx
je     1144 <addr2str+0x24>
cmp    $0x82,%ecx
jne    116f <addr2str+0x4f>
mov    $0x82,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <inet_ntop@plt>
jmp    1183 <addr2str+0x63>
mov    (%rsi),%rcx
lea    0xea0(%rip),%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    1183 <addr2str+0x63>
lea    0xe8d(%rip),%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%s\00"
@.str.1 = [13 x i8] c"unknown host\00"
define i8* @addr2str(%struct.sockaddr* %0, i8* returned %1, i64 %2) {
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%5 = load i32, i32* %4
switch i32 %5, label %14 [
i32 130, label %6
i32 129, label %8
i32 128, label %10
]
6: ; preds = %3
%7 = tail call i32 @inet_ntop(i32 130, i32* %4, i8* %1, i64 %2)
br label %16
8: ; preds = %3
%9 = tail call i32 @inet_ntop(i32 130, i32* %4, i8* %1, i64 %2)
br label %16
10: ; preds = %3
%11 = bitcast %struct.sockaddr* %0 to i8**
%12 = load i8*, i8** %11
%13 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %16
14: ; preds = %3
%15 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10, %8, %6
ret i8* %1
}