#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_storage {
  scalar_t__ ss_family;
};
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ service;
typedef int /*<<< orphan*/ host;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int NI_DGRAM;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
int /*<<< orphan*/ gai_strerror(int);
int getnameinfo(struct sockaddr *, int, char *, int, char *, int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
scalar_t__ strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *endpoint(const struct sockaddr_storage *addr) {
  char host[4096 + 1];
  char service[512 + 1];
  static char buf[sizeof(host) + sizeof(service) + 4];
  int ret;
  socklen_t addr_len = 0;
  memset(buf, 0, sizeof(buf));
  if (addr->ss_family == AF_INET)
    addr_len = sizeof(struct sockaddr_in);
  else if (addr->ss_family == AF_INET6)
    addr_len = sizeof(struct sockaddr_in6);
  ret = getnameinfo((struct sockaddr *)addr, addr_len, host, sizeof(host),
                    service, sizeof(service),
                    NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST);
  if (ret)
    strncpy(buf, gai_strerror(ret), sizeof(buf) - 1);
  else
    snprintf(buf, sizeof(buf) - 1,
             (addr->ss_family == AF_INET6 && strchr(host, ':')) ? "[%s]:%s"
                                                                : "%s:%s",
             host, service);
  return buf;
}


x86 assembly (clang, -O0):
<endpoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1240,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1228(%rbp)
lea    0x2ee0(%rip),%rdi
xor    %esi,%esi
mov    $0x1206,%edx
call   1070 <memset@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <endpoint+0x52>
movl   $0x4,-0x1228(%rbp)
jmp    11d8 <endpoint+0x78>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11d3 <endpoint+0x73>
movl   $0x4,-0x1228(%rbp)
jmp    11d8 <endpoint+0x78>
mov    -0x8(%rbp),%rdi
mov    -0x1228(%rbp),%esi
lea    -0x1010(%rbp),%rdx
lea    -0x1220(%rbp),%r8
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    0x2de8(%rip),%rcx
or     (%rcx),%eax
mov    0x2dbf(%rip),%rcx
or     (%rcx),%eax
mov    $0x1001,%ecx
mov    $0x201,%r9d
mov    %eax,(%rsp)
call   1080 <getnameinfo@plt>
mov    %eax,-0x1224(%rbp)
cmpl   $0x0,-0x1224(%rbp)
je     1254 <endpoint+0xf4>
mov    -0x1224(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    %eax,%esi
lea    0x2e1b(%rip),%rdi
mov    $0x1205,%edx
call   1030 <strncpy@plt>
jmp    12cb <endpoint+0x16b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2d74(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1229(%rbp)
jne    1291 <endpoint+0x131>
lea    -0x1010(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1229(%rbp)
mov    -0x1229(%rbp),%cl
lea    0xd6a(%rip),%rdx
lea    0xd5b(%rip),%rax
test   $0x1,%cl
cmovne %rax,%rdx
lea    -0x1010(%rbp),%rcx
lea    -0x1220(%rbp),%r8
lea    0x2d9f(%rip),%rdi
mov    $0x1205,%esi
call   1050 <snprintf@plt>
lea    0x2d8e(%rip),%rax
add    $0x1240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<endpoint>:
push   %r14
push   %rbx
sub    $0x1228,%rsp
mov    %rdi,%rbx
lea    0x2eec(%rip),%rdi
xor    %esi,%esi
mov    $0x1206,%edx
call   1070 <memset@plt>
mov    (%rbx),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
sete   %cl
mov    0x2e41(%rip),%r14
cmp    (%r14),%rax
sete   %al
or     %cl,%al
movzbl %al,%esi
mov    0x2e0f(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
shl    $0x2,%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
lea    0x220(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
mov    $0x1001,%ecx
mov    $0x201,%r9d
call   1080 <getnameinfo@plt>
test   %eax,%eax
je     1203 <endpoint+0xa3>
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0x2e6b(%rip),%rdi
mov    %eax,%esi
mov    $0x1205,%edx
call   1030 <strncpy@plt>
jmp    1259 <endpoint+0xf9>
mov    (%rbx),%rax
cmp    (%r14),%rax
jne    1234 <endpoint+0xd4>
lea    0x220(%rsp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
lea    0xde1(%rip),%rax
lea    0xdd2(%rip),%rdx
cmove  %rax,%rdx
jmp    123b <endpoint+0xdb>
lea    0xdcd(%rip),%rdx
lea    0x2e1e(%rip),%rdi
lea    0x220(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    $0x1205,%esi
call   1050 <snprintf@plt>
lea    0x2e00(%rip),%rax
add    $0x1228,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@endpoint.buf = internal global [4614 x i8] zeroinitializer
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@NI_DGRAM = global i32 0
@NI_NUMERICSERV = global i32 0
@NI_NUMERICHOST = global i32 0
@.str = [8 x i8] c"[%s]:%s\00"
@.str.1 = [6 x i8] c"%s:%s\00"
define i8* @endpoint(%struct.sockaddr_storage* %0) {
%2 = alloca %struct.sockaddr_storage*
%3 = alloca [4097 x i8]
%4 = alloca [513 x i8]
%5 = alloca i32
%6 = alloca i32
store %struct.sockaddr_storage* %0, %struct.sockaddr_storage** %2
store i32 0, i32* %6
%7 = call i32 @memset(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 0, i32 4614)
%8 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %2
%9 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %8, i32 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @AF_INET
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 4, i32* %6
br label %22
14: ; preds = %1
%15 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %2
%16 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %15, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @AF_INET6
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 4, i32* %6
br label %21
21: ; preds = %20, %14
br label %22
22: ; preds = %21, %13
%23 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %2
%24 = bitcast %struct.sockaddr_storage* %23 to %struct.sockaddr*
%25 = load i32, i32* %6
%26 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%27 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%28 = load i32, i32* @NI_DGRAM
%29 = load i32, i32* @NI_NUMERICSERV
%30 = or i32 %28, %29
%31 = load i32, i32* @NI_NUMERICHOST
%32 = or i32 %30, %31
%33 = call i32 @getnameinfo(%struct.sockaddr* %24, i32 %25, i8* %26, i32 4097, i8* %27, i32 513, i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %22
%37 = load i32, i32* %5
%38 = call i32 @gai_strerror(i32 %37)
%39 = call i32 @strncpy(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 %38, i32 4613)
br label %57
40: ; preds = %22
%41 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %2
%42 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* @AF_INET6
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%48 = call i64 @strchr(i8* %47, i8 signext 58)
%49 = icmp ne i64 %48, 0
br label %50
50: ; preds = %46, %40
%51 = phi i1 [ false, %40 ], [ %49, %46 ]
%52 = zext i1 %51 to i64
%53 = select i1 %51, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0)
%54 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%55 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%56 = call i32 @snprintf(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 4613, i8* %53, i8* %54, i8* %55)
br label %57
57: ; preds = %50, %36
ret i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@endpoint.buf = internal global [4614 x i8] zeroinitializer
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@NI_DGRAM = global i32 0
@NI_NUMERICSERV = global i32 0
@NI_NUMERICHOST = global i32 0
@.str = [8 x i8] c"[%s]:%s\00"
@.str.1 = [6 x i8] c"%s:%s\00"
define i8* @endpoint(%struct.sockaddr_storage* %0) {
%2 = alloca [4097 x i8]
%3 = alloca [513 x i8]
%4 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @memset(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 0, i32 4614)
%7 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @AF_INET
%10 = icmp eq i64 %8, %9
%11 = load i64, i64* @AF_INET6
%12 = icmp eq i64 %8, %11
%13 = select i1 %10, i1 true, i1 %12
%14 = select i1 %13, i32 4, i32 0
%15 = bitcast %struct.sockaddr_storage* %0 to %struct.sockaddr*
%16 = load i32, i32* @NI_DGRAM
%17 = load i32, i32* @NI_NUMERICSERV
%18 = or i32 %17, %16
%19 = load i32, i32* @NI_NUMERICHOST
%20 = or i32 %18, %19
%21 = call i32 @getnameinfo(%struct.sockaddr* %15, i32 %14, i8* %4, i32 4097, i8* %5, i32 513, i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %1
%24 = call i32 @gai_strerror(i32 %21)
%25 = call i32 @strncpy(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 %24, i32 4613)
br label %37
26: ; preds = %1
%27 = load i64, i64* %7
%28 = load i64, i64* @AF_INET6
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = call i64 @strchr(i8* %4, i8 signext 58)
%32 = icmp eq i64 %31, 0
%33 = select i1 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0)
br label %34
34: ; preds = %30, %26
%35 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %26 ], [ %33, %30 ]
%36 = call i32 @snprintf(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 4613, i8* %35, i8* %4, i8* %5)
br label %37
37: ; preds = %34, %23
ret i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0)
}