#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_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
typedef int /*<<< orphan*/ ip;

/* Variables and functions */
#define AF_INET 131
#define AF_INET6 130
#define AF_UNIX 129
#define AF_UNSPEC 128
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(int const, int /*<<< orphan*/ *, char *, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, size_t, char *, ...);
const char *display_sockaddr(const void *sa, char *buffer, size_t buffer_len) {
  char ip[INET6_ADDRSTRLEN];
  if (sa == NULL || buffer == NULL)
    return NULL;
  switch (((const struct sockaddr *)sa)->sa_family) {
  case AF_INET:
    inet_ntop(AF_INET, &((const struct sockaddr_in *)sa)->sin_addr, ip,
              sizeof(ip));
    if (((struct sockaddr_in *)sa)->sin_port != 0)
      snprintf(buffer, buffer_len, "%s:%d", ip,
               ntohs(((struct sockaddr_in *)sa)->sin_port));
    else
      snprintf(buffer, buffer_len, "%s", ip);
    break;
  case AF_INET6:
    inet_ntop(AF_INET6, &((const struct sockaddr_in6 *)sa)->sin6_addr, ip,
              sizeof(ip));
    if (((struct sockaddr_in6 *)sa)->sin6_port != 0)
      snprintf(buffer, buffer_len, "[%s]:%d", ip,
               ntohs(((struct sockaddr_in6 *)sa)->sin6_port));
    else
      snprintf(buffer, buffer_len, "[%s]", ip);
    break;
  case AF_UNIX:
    snprintf(buffer, buffer_len, "unix:%s",
             ((struct sockaddr_un *)sa)->sun_path);
    break;
  case AF_UNSPEC:
    snprintf(buffer, buffer_len, "NONE");
    break;
  default:
    assert(0);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<display_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119a <display_sockaddr+0x5a>
cmpq   $0x0,-0x18(%rbp)
jne    11ae <display_sockaddr+0x6e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1337 <display_sockaddr+0x1f7>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x50(%rbp)
sub    $0x3,%eax
ja     1321 <display_sockaddr+0x1e1>
mov    -0x50(%rbp),%rax
lea    0xe2f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    %eax,%ecx
mov    $0x83,%edi
call   1030 <inet_ntop@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     1240 <display_sockaddr+0x100>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
lea    0xddc(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    125a <display_sockaddr+0x11a>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xdd5(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1328 <display_sockaddr+0x1e8>
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    %eax,%ecx
mov    $0x82,%edi
call   1030 <inet_ntop@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     12c5 <display_sockaddr+0x185>
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
lea    0xd5d(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    12df <display_sockaddr+0x19f>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xd46(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1328 <display_sockaddr+0x1e8>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xd29(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1328 <display_sockaddr+0x1e8>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xd16(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1328 <display_sockaddr+0x1e8>
xor    %edi,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_sockaddr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e96(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%r12
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
xor    %eax,%eax
test   %rdi,%rdi
je     1253 <display_sockaddr+0x113>
mov    %rsi,%r15
test   %rsi,%rsi
je     1253 <display_sockaddr+0x113>
mov    %rdi,%rbx
mov    (%rdi),%eax
add    $0xffffff80,%eax
cmp    $0x3,%eax
ja     11b5 <display_sockaddr+0x75>
mov    %rdx,%r14
lea    0xe6d(%rip),%rdx
movslq (%rdx,%rax,4),%rax
add    %rdx,%rax
jmp    *%rax
lea    0xe88(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    1250 <display_sockaddr+0x110>
xor    %edi,%edi
call   1050 <assert@plt>
jmp    1250 <display_sockaddr+0x110>
mov    (%rbx),%rcx
lea    0xe58(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
jmp    1249 <display_sockaddr+0x109>
lea    0x4(%rbx),%rsi
mov    $0x82,%edi
mov    %r12,%rdx
call   1030 <inet_ntop@plt>
mov    (%rbx),%edi
test   %edi,%edi
je     1230 <display_sockaddr+0xf0>
call   1060 <ntohs@plt>
lea    0xe20(%rip),%rdx
jmp    121b <display_sockaddr+0xdb>
lea    0x4(%rbx),%rsi
mov    $0x83,%edi
mov    %r12,%rdx
call   1030 <inet_ntop@plt>
mov    (%rbx),%edi
test   %edi,%edi
je     1239 <display_sockaddr+0xf9>
call   1060 <ntohs@plt>
lea    0xdf5(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    1250 <display_sockaddr+0x110>
lea    0xde7(%rip),%rdx
jmp    1240 <display_sockaddr+0x100>
lea    0xde8(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r15,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr_un = type { i8* }
@INET6_ADDRSTRLEN = global i32 0
@.str = [6 x i8] c"%s:%d\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [8 x i8] c"[%s]:%d\00"
@.str.3 = [5 x i8] c"[%s]\00"
@.str.4 = [8 x i8] c"unix:%s\00"
@.str.5 = [5 x i8] c"NONE\00"
define i8* @display_sockaddr(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i32, i32* @INET6_ADDRSTRLEN
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17, %3
store i8* null, i8** %4
store i32 1, i32* %10
br label %92
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = bitcast i8* %22 to %struct.sockaddr*
%24 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %23, i32 0, i32 0
%25 = load i32, i32* %24
switch i32 %25, label %88 [
i32 131, label %26
i32 130, label %51
i32 129, label %76
i32 128, label %84
]
26: ; preds = %21
%27 = load i8*, i8** %5
%28 = bitcast i8* %27 to %struct.sockaddr_in*
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 1
%30 = trunc i64 %12 to i32
%31 = call i32 @inet_ntop(i32 131, i32* %29, i8* %14, i32 %30)
%32 = load i8*, i8** %5
%33 = bitcast i8* %32 to %struct.sockaddr_in*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %26
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = load i8*, i8** %5
%41 = bitcast i8* %40 to %struct.sockaddr_in*
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @ntohs(i32 %43)
%45 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %38, i64 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %44)
br label %50
46: ; preds = %26
%47 = load i8*, i8** %6
%48 = load i64, i64* %7
%49 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %47, i64 %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %50
50: ; preds = %46, %37
br label %90
51: ; preds = %21
%52 = load i8*, i8** %5
%53 = bitcast i8* %52 to %struct.sockaddr_in6*
%54 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %53, i32 0, i32 1
%55 = trunc i64 %12 to i32
%56 = call i32 @inet_ntop(i32 130, i32* %54, i8* %14, i32 %55)
%57 = load i8*, i8** %5
%58 = bitcast i8* %57 to %struct.sockaddr_in6*
%59 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %71
62: ; preds = %51
%63 = load i8*, i8** %6
%64 = load i64, i64* %7
%65 = load i8*, i8** %5
%66 = bitcast i8* %65 to %struct.sockaddr_in6*
%67 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @ntohs(i32 %68)
%70 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %63, i64 %64, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %14, i32 %69)
br label %75
71: ; preds = %51
%72 = load i8*, i8** %6
%73 = load i64, i64* %7
%74 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %72, i64 %73, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %75
75: ; preds = %71, %62
br label %90
76: ; preds = %21
%77 = load i8*, i8** %6
%78 = load i64, i64* %7
%79 = load i8*, i8** %5
%80 = bitcast i8* %79 to %struct.sockaddr_un*
%81 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %80, i32 0, i32 0
%82 = load i8*, i8** %81
%83 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %77, i64 %78, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %82)
br label %90
84: ; preds = %21
%85 = load i8*, i8** %6
%86 = load i64, i64* %7
%87 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %85, i64 %86, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %90
88: ; preds = %21
%89 = call i32 @assert(i32 0)
br label %90
90: ; preds = %88, %84, %76, %75, %50
%91 = load i8*, i8** %6
store i8* %91, i8** %4
store i32 1, i32* %10
br label %92
92: ; preds = %90, %20
%93 = load i8*, i8** %8
%94 = load i8*, i8** %4
ret i8* %94
}

LLVM IR (-O2):
@INET6_ADDRSTRLEN = global i32 0
@.str = [6 x i8] c"%s:%d\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [8 x i8] c"[%s]:%d\00"
@.str.3 = [5 x i8] c"[%s]\00"
@.str.4 = [8 x i8] c"unix:%s\00"
@.str.5 = [5 x i8] c"NONE\00"
define i8* @display_sockaddr(i8* %0, i8* writeonly %1, i64 %2) {
%4 = load i32, i32* @INET6_ADDRSTRLEN
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp eq i8* %0, null
%8 = icmp eq i8* %1, null
%9 = or i1 %7, %8
br i1 %9, label %43, label %10
10: ; preds = %3
%11 = bitcast i8* %0 to i32*
%12 = load i32, i32* %11
switch i32 %12, label %41 [
i32 131, label %13
i32 130, label %24
i32 129, label %35
i32 128, label %39
]
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %0, i64 4
%15 = bitcast i8* %14 to i32*
%16 = call i32 @inet_ntop(i32 131, i32* %15, i8* %6, i32 %4)
%17 = load i32, i32* %11
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = call i32 @ntohs(i32 %17)
%21 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %6, i32 %20)
br label %43
22: ; preds = %13
%23 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %43
24: ; preds = %10
%25 = getelementptr inbounds i8, i8* %0, i64 4
%26 = bitcast i8* %25 to i32*
%27 = call i32 @inet_ntop(i32 130, i32* %26, i8* %6, i32 %4)
%28 = load i32, i32* %11
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %24
%31 = call i32 @ntohs(i32 %28)
%32 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %6, i32 %31)
br label %43
33: ; preds = %24
%34 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %43
35: ; preds = %10
%36 = bitcast i8* %0 to i8**
%37 = load i8*, i8** %36
%38 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %37)
br label %43
39: ; preds = %10
%40 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %43
41: ; preds = %10
%42 = tail call i32 @assert(i32 0)
br label %43
43: ; preds = %35, %39, %41, %22, %19, %33, %30, %3
%44 = phi i8* [ null, %3 ], [ %1, %30 ], [ %1, %33 ], [ %1, %19 ], [ %1, %22 ], [ %1, %41 ], [ %1, %39 ], [ %1, %35 ]
ret i8* %44
}