#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_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  scalar_t__ sa_family;
};
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 const *, 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 *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->sa_family == AF_INET)
    addr_len = sizeof(struct sockaddr_in);
  else if (addr->sa_family == AF_INET6)
    addr_len = sizeof(struct sockaddr_in6);
  ret =
      getnameinfo(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);
    buf[sizeof(buf) - 1] = '\0';
  } else
    snprintf(buf, sizeof(buf),
             (addr->sa_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     125b <endpoint+0xfb>
mov    -0x1224(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    %eax,%esi
lea    0x2e1b(%rip),%rdi
mov    $0x1205,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x400f(%rip)
jmp    12d2 <endpoint+0x172>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2d6d(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1229(%rbp)
jne    1298 <endpoint+0x138>
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    0xd63(%rip),%rdx
lea    0xd54(%rip),%rax
test   $0x1,%cl
cmovne %rax,%rdx
lea    -0x1010(%rbp),%rcx
lea    -0x1220(%rbp),%r8
lea    0x2d98(%rip),%rdi
mov    $0x1206,%esi
call   1050 <snprintf@plt>
lea    0x2d87(%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     120a <endpoint+0xaa>
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0x2e6b(%rip),%rdi
mov    %eax,%esi
mov    $0x1205,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x405d(%rip)
jmp    1260 <endpoint+0x100>
mov    (%rbx),%rax
cmp    (%r14),%rax
jne    123b <endpoint+0xdb>
lea    0x220(%rsp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
lea    0xdda(%rip),%rax
lea    0xdcb(%rip),%rdx
cmove  %rax,%rdx
jmp    1242 <endpoint+0xe2>
lea    0xdc6(%rip),%rdx
lea    0x2e17(%rip),%rdi
lea    0x220(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    $0x1206,%esi
call   1050 <snprintf@plt>
lea    0x2df9(%rip),%rax
add    $0x1228,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
@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* %0) {
%2 = alloca %struct.sockaddr*
%3 = alloca [4097 x i8]
%4 = alloca [513 x i8]
%5 = alloca i32
%6 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %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*, %struct.sockaddr** %2
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %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*, %struct.sockaddr** %2
%16 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %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*, %struct.sockaddr** %2
%24 = load i32, i32* %6
%25 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%26 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%27 = load i32, i32* @NI_DGRAM
%28 = load i32, i32* @NI_NUMERICSERV
%29 = or i32 %27, %28
%30 = load i32, i32* @NI_NUMERICHOST
%31 = or i32 %29, %30
%32 = call i32 @getnameinfo(%struct.sockaddr* %23, i32 %24, i8* %25, i32 4097, i8* %26, i32 513, i32 %31)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %22
%36 = load i32, i32* %5
%37 = call i32 @gai_strerror(i32 %36)
%38 = call i32 @strncpy(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 %37, i32 4613)
store i8 0, i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 4613)
br label %56
39: ; preds = %22
%40 = load %struct.sockaddr*, %struct.sockaddr** %2
%41 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = load i64, i64* @AF_INET6
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %39
%46 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%47 = call i64 @strchr(i8* %46, i8 signext 58)
%48 = icmp ne i64 %47, 0
br label %49
49: ; preds = %45, %39
%50 = phi i1 [ false, %39 ], [ %48, %45 ]
%51 = zext i1 %50 to i64
%52 = select i1 %50, 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)
%53 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%54 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%55 = call i32 @snprintf(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 4614, i8* %52, i8* %53, i8* %54)
br label %56
56: ; preds = %49, %35
ret i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@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* %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, %struct.sockaddr* %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 = load i32, i32* @NI_DGRAM
%16 = load i32, i32* @NI_NUMERICSERV
%17 = or i32 %16, %15
%18 = load i32, i32* @NI_NUMERICHOST
%19 = or i32 %17, %18
%20 = call i32 @getnameinfo(%struct.sockaddr* %0, i32 %14, i8* %4, i32 4097, i8* %5, i32 513, i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %1
%23 = call i32 @gai_strerror(i32 %20)
%24 = call i32 @strncpy(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 %23, i32 4613)
store i8 0, i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 4613)
br label %36
25: ; preds = %1
%26 = load i64, i64* %7
%27 = load i64, i64* @AF_INET6
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = call i64 @strchr(i8* %4, i8 signext 58)
%31 = icmp eq i64 %30, 0
%32 = select i1 %31, 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 %33
33: ; preds = %29, %25
%34 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %25 ], [ %32, %29 ]
%35 = call i32 @snprintf(i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0), i32 4614, i8* %34, i8* %4, i8* %5)
br label %36
36: ; preds = %33, %22
ret i8* getelementptr inbounds ([4614 x i8], [4614 x i8]* @endpoint.buf, i64 0, i64 0)
}