#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_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
typedef int /*<<< orphan*/ str;
typedef int socklen_t;

/* Variables and functions */
#define AF_INET 129
#define AF_UNIX 128
int /*<<< orphan*/ *inet_ntop(int const, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
char *sock_ntop_host(const struct sockaddr *sa, socklen_t salen) {
  static char str[128];
  switch (sa->sa_family) {
  case AF_INET: {
    struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL) {
      return NULL;
    } else {
      return str;
    }
  }
  case AF_UNIX: {
    struct sockaddr_un *unp = (struct sockaddr_un *)sa;
    if (unp->sun_path[0] == 0)
      strcpy(str, "(no pathname bound)");
    else
      snprintf(str, sizeof(str), "%s", unp->sun_path);
    return str;
  }
  default:
    snprintf(str, sizeof(str), "sock_ntop_host: unknown AF_xxx: %d, len %d",
             sa->sa_family, salen);
    return str;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<sock_ntop_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     11b4 <sock_ntop_host+0x84>
jmp    1158 <sock_ntop_host+0x28>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
jne    1218 <sock_ntop_host+0xe8>
jmp    116b <sock_ntop_host+0x3b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    $0x81,%edi
lea    0x2ecd(%rip),%rdx
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    11a4 <sock_ntop_host+0x74>
movq   $0x0,-0x8(%rbp)
jmp    1247 <sock_ntop_host+0x117>
lea    0x2ea5(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1247 <sock_ntop_host+0x117>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11e7 <sock_ntop_host+0xb7>
lea    0x2e7a(%rip),%rdi
lea    0xe23(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1208 <sock_ntop_host+0xd8>
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0x2e5b(%rip),%rdi
mov    $0x80,%esi
lea    0xe13(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    0x2e41(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1247 <sock_ntop_host+0x117>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
lea    0x2e27(%rip),%rdi
mov    $0x80,%esi
lea    0xde2(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    0x2e0d(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_ntop_host>:
push   %rbx
mov    %esi,%r8d
mov    %rdi,%rsi
mov    (%rdi),%ecx
cmp    $0x80,%ecx
je     116b <sock_ntop_host+0x3b>
lea    0x2f08(%rip),%rbx
cmp    $0x81,%ecx
jne    1192 <sock_ntop_host+0x62>
mov    $0x81,%edi
mov    %rbx,%rdx
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
cmove  %rax,%rbx
jmp    11c0 <sock_ntop_host+0x90>
mov    (%rsi),%rcx
cmpb   $0x0,(%rcx)
je     11aa <sock_ntop_host+0x7a>
lea    0x2ed6(%rip),%rbx
lea    0xe93(%rip),%rdx
mov    %rbx,%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    11c0 <sock_ntop_host+0x90>
lea    0xe7e(%rip),%rdx
mov    %rbx,%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    11c0 <sock_ntop_host+0x90>
lea    0x2e9f(%rip),%rbx
lea    0xe48(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@sock_ntop_host.str = internal global [128 x i8] zeroinitializer
@.str = [20 x i8] c"(no pathname bound)\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [43 x i8] c"sock_ntop_host: unknown AF_xxx: %d, len %d\00"
define i8* @sock_ntop_host(%struct.sockaddr* %0, i32 %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i32, i32* %3
switch i32 %4, label %18 [
i32 129, label %5
i32 128, label %9
]
5: ; preds = %2
%6 = tail call i32* @inet_ntop(i32 129, i32* %3, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop_host.str, i64 0, i64 0), i32 128)
%7 = icmp eq i32* %6, null
%8 = select i1 %7, i8* null, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop_host.str, i64 0, i64 0)
br label %20
9: ; preds = %2
%10 = bitcast %struct.sockaddr* %0 to i8**
%11 = load i8*, i8** %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @strcpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop_host.str, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %20
16: ; preds = %9
%17 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop_host.str, i64 0, i64 0), i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %20
18: ; preds = %2
%19 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop_host.str, i64 0, i64 0), i32 128, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 %1)
br label %20
20: ; preds = %14, %16, %18, %5
%21 = phi i8* [ getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop_host.str, i64 0, i64 0), %18 ], [ %8, %5 ], [ getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop_host.str, i64 0, i64 0), %16 ], [ getelementptr inbounds ([128 x i8], [128 x i8]* @sock_ntop_host.str, i64 0, i64 0), %14 ]
ret i8* %21
}