#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 /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ asprintf(char **, char *, char *, int);
int errno;
int getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ *inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, int);
int ntohs(int /*<<< orphan*/);
char *strdup(char *);
char *strerror(int);
char *get_endpoint_name(int fd) {
  char *result = NULL;
  struct sockaddr_storage addr;
  socklen_t addr_len = sizeof addr;
  if (getpeername(fd, (struct sockaddr *)&addr, &addr_len) == -1)
    asprintf(&result, "getpeername failed: %s (%d)", strerror(errno), errno);
  else {
    char peer_name[4096] = {0};
    int port = -1, rc = 0;
    if (addr.ss_family == AF_INET) {
      struct sockaddr_in *s = (struct sockaddr_in *)&addr;
      port = ntohs(s->sin_port);
      rc =
          inet_ntop(AF_INET, &s->sin_addr, peer_name, sizeof peer_name) != NULL;
    } else {
      struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
      port = ntohs(s->sin6_port);
      rc = inet_ntop(AF_INET6, &s->sin6_addr, peer_name, sizeof peer_name) !=
           NULL;
    }
    if (rc)
      asprintf(&result, "[%s]:%d", peer_name, port);
    else
      result = strdup("inet_ntop failed");
  }
  return result;
}


x86 assembly (clang, -O0):
<get_endpoint_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x8,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <getpeername@plt>
cmp    $0xffffffff,%eax
jne    11d5 <get_endpoint_name+0x65>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rdx
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
lea    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1060 <asprintf@plt>
jmp    1301 <get_endpoint_name+0x191>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
movl   $0xffffffff,-0x1024(%rbp)
ff ff ff
movl   $0x0,-0x1028(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
jne    1269 <get_endpoint_name+0xf9>
lea    -0x18(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <ntohs@plt>
mov    %eax,-0x1024(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%rdi
mov    -0x1030(%rbp),%rsi
lea    -0x1020(%rbp),%rdx
mov    $0x1000,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1028(%rbp)
jmp    12bd <get_endpoint_name+0x14d>
lea    -0x18(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    -0x1038(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <ntohs@plt>
mov    %eax,-0x1024(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%rdi
mov    -0x1038(%rbp),%rsi
lea    -0x1020(%rbp),%rdx
mov    $0x1000,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1028(%rbp)
cmpl   $0x0,-0x1028(%rbp)
je     12ec <get_endpoint_name+0x17c>
lea    -0x1020(%rbp),%rdx
mov    -0x1024(%rbp),%ecx
lea    -0x10(%rbp),%rdi
lea    0xd3a(%rip),%rsi
call   1060 <asprintf@plt>
jmp    12fc <get_endpoint_name+0x18c>
lea    0xd31(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x10(%rbp)
jmp    1301 <get_endpoint_name+0x191>
mov    -0x10(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_endpoint_name>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1020,%rsp
movq   $0x0,0x8(%rsp)
movl   $0x8,0x14(%rsp)
lea    0x18(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   1040 <getpeername@plt>
cmp    $0xffffffff,%eax
je     120e <get_endpoint_name+0x9e>
lea    0x20(%rsp),%r14
mov    $0x1000,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x18(%rsp),%rax
mov    0x2e28(%rip),%rbx
mov    %rax,%rdi
shr    $0x20,%rdi
cmp    (%rbx),%rax
je     11d3 <get_endpoint_name+0x63>
mov    0x2e0d(%rip),%rbx
call   1070 <ntohs@plt>
mov    %eax,%ebp
mov    (%rbx),%rdi
lea    0x18(%rsp),%rsi
mov    %r14,%rdx
mov    $0x1000,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     1234 <get_endpoint_name+0xc4>
lea    0xe21(%rip),%rsi
lea    0x8(%rsp),%rdi
lea    0x20(%rsp),%rdx
mov    %ebp,%ecx
call   1060 <asprintf@plt>
jmp    1245 <get_endpoint_name+0xd5>
mov    0x2dbb(%rip),%rbx
mov    (%rbx),%edi
call   1090 <strerror@plt>
mov    (%rbx),%ecx
lea    0xddb(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rax,%rdx
call   1060 <asprintf@plt>
jmp    1245 <get_endpoint_name+0xd5>
lea    0xde9(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,0x8(%rsp)
mov    0x8(%rsp),%rax
add    $0x1020,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [28 x i8] c"getpeername failed: %s (%d)\00"
@errno = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str.1 = [8 x i8] c"[%s]:%d\00"
@.str.2 = [17 x i8] c"inet_ntop failed\00"
define i8* @get_endpoint_name(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %2
store i8* null, i8** %3
store i32 8, i32* %5
%11 = load i32, i32* %2
%12 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%13 = call i32 @getpeername(i32 %11, %struct.sockaddr* %12, i32* %5)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = load i32, i32* @errno
%19 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
br label %62
20: ; preds = %1
%21 = bitcast [4096 x i8]* %6 to i8*
store i32 -1, i32* %7
store i32 0, i32* %8
%22 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @AF_INET
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %39
26: ; preds = %20
%27 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr_in*
store %struct.sockaddr_in* %27, %struct.sockaddr_in** %9
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 @ntohs(i32 %30)
store i32 %31, i32* %7
%32 = load i64, i64* @AF_INET
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 0
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%36 = call i32* @inet_ntop(i64 %32, i32* %34, i8* %35, i32 4096)
%37 = icmp ne i32* %36, null
%38 = zext i1 %37 to i32
store i32 %38, i32* %8
br label %52
39: ; preds = %20
%40 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %40, %struct.sockaddr_in6** %10
%41 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %10
%42 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = call i32 @ntohs(i32 %43)
store i32 %44, i32* %7
%45 = load i64, i64* @AF_INET6
%46 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %10
%47 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %46, i32 0, i32 0
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%49 = call i32* @inet_ntop(i64 %45, i32* %47, i8* %48, i32 4096)
%50 = icmp ne i32* %49, null
%51 = zext i1 %50 to i32
store i32 %51, i32* %8
br label %52
52: ; preds = %39, %26
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%57 = load i32, i32* %7
%58 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %56, i32 %57)
br label %61
59: ; preds = %52
%60 = call i8* @strdup(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i8* %60, i8** %3
br label %61
61: ; preds = %59, %55
br label %62
62: ; preds = %61, %15
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [28 x i8] c"getpeername failed: %s (%d)\00"
@errno = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str.1 = [8 x i8] c"[%s]:%d\00"
@.str.2 = [17 x i8] c"inet_ntop failed\00"
define i8* @get_endpoint_name(i32 %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%7 = bitcast %struct.sockaddr_storage* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 8, i32* %4
%9 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%10 = call i32 @getpeername(i32 %0, %struct.sockaddr* %9, i32* %4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = load i32, i32* @errno
%16 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
br label %37
17: ; preds = %1
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%19 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @AF_INET
%22 = icmp eq i64 %20, %21
%23 = lshr i64 %20, 32
%24 = trunc i64 %23 to i32
%25 = call i32 @ntohs(i32 %24)
%26 = load i64, i64* @AF_INET
%27 = load i64, i64* @AF_INET6
%28 = select i1 %22, i64 %26, i64 %27
%29 = bitcast %struct.sockaddr_storage* %3 to i32*
%30 = call i32* @inet_ntop(i64 %28, i32* %29, i8* %18, i32 4096)
%31 = icmp eq i32* %30, null
br i1 %31, label %34, label %32
32: ; preds = %17
%33 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %25)
br label %36
34: ; preds = %17
%35 = call dereferenceable_or_null(17) i8* @strdup(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %2
br label %36
36: ; preds = %34, %32
br label %37
37: ; preds = %36, %12
%38 = load i8*, i8** %2
ret i8* %38
}