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

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ errno;
scalar_t__ getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(int const, int /*<<< orphan*/ *, char *, int);
int ret_err(char **, char *, int);
char *strdup(char *);
int strerror(int /*<<< orphan*/);
int etcp_getpeeraddr(int fd, char **address, char **errstr) {
  struct sockaddr_storage ss_addr;
  socklen_t addrlen = sizeof(ss_addr);
  char buf[INET6_ADDRSTRLEN];
  if (getpeername(fd, (struct sockaddr *)&ss_addr, &addrlen) == 0) {
    switch (ss_addr.ss_family) {
    case AF_INET:
      inet_ntop(AF_INET, &((struct sockaddr_in *)&ss_addr)->sin_addr, buf,
                sizeof(buf));
      break;
    case AF_INET6:
      inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&ss_addr)->sin6_addr, buf,
                sizeof(buf));
      break;
    default:
      return ret_err(errstr, "Unknown address family %d", ss_addr.ss_family);
    }
    *address = strdup(buf);
    return 0;
  }
  return ret_err(errstr, "getpeername: %s", strerror(errno));
}


x86 assembly (clang, -O0):
<etcp_getpeeraddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x4,-0x24(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <getpeername@plt>
cmp    $0x0,%rax
jne    125f <etcp_getpeeraddr+0x10f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x54(%rbp)
sub    $0x80,%eax
je     11fa <etcp_getpeeraddr+0xaa>
jmp    11ca <etcp_getpeeraddr+0x7a>
mov    -0x54(%rbp),%eax
sub    $0x81,%eax
jne    1217 <etcp_getpeeraddr+0xc7>
jmp    11dd <etcp_getpeeraddr+0x8d>
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rax
lea    -0x20(%rbp),%rsi
mov    %eax,%ecx
mov    $0x81,%edi
call   1030 <inet_ntop@plt>
jmp    1239 <etcp_getpeeraddr+0xe9>
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rax
lea    -0x20(%rbp),%rsi
mov    %eax,%ecx
mov    $0x80,%edi
call   1030 <inet_ntop@plt>
jmp    1239 <etcp_getpeeraddr+0xe9>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xddb(%rip),%rsi
call   1050 <ret_err@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1291 <etcp_getpeeraddr+0x141>
mov    -0x48(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1291 <etcp_getpeeraddr+0x141>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
lea    0xd98(%rip),%rsi
call   1050 <ret_err@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<etcp_getpeeraddr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rsp,%r13
movl   $0x4,-0x34(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1040 <getpeername@plt>
test   %rax,%rax
je     11c0 <etcp_getpeeraddr+0x70>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe66(%rip),%rsi
mov    %r15,%rdi
mov    %eax,%edx
call   1050 <ret_err@plt>
jmp    11f6 <etcp_getpeeraddr+0xa6>
mov    -0x30(%rbp),%edx
cmp    $0x81,%edx
je     11d8 <etcp_getpeeraddr+0x88>
cmp    $0x80,%edx
jne    1208 <etcp_getpeeraddr+0xb8>
mov    $0x80,%edx
lea    -0x30(%rbp),%rsi
mov    %edx,%edi
mov    %rbx,%rdx
mov    %r12d,%ecx
call   1030 <inet_ntop@plt>
mov    %rbx,%rdi
call   1060 <strdup@plt>
mov    %rax,(%r14)
xor    %eax,%eax
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf1(%rip),%rsi
mov    %r15,%rdi
jmp    11b9 <etcp_getpeeraddr+0x69>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [26 x i8] c"Unknown address family %d\00"
@.str.1 = [16 x i8] c"getpeername: %s\00"
@errno = global i32 0
define i32 @etcp_getpeeraddr(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 4, i32* %9
%13 = load i32, i32* @INET6_ADDRSTRLEN
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = load i32, i32* %5
%18 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%19 = call i64 @getpeername(i32 %17, %struct.sockaddr* %18, i32* %9)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %42
21: ; preds = %3
%22 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %8, i32 0, i32 0
%23 = load i32, i32* %22
switch i32 %23, label %34 [
i32 129, label %24
i32 128, label %29
]
24: ; preds = %21
%25 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr_in*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i32 0, i32 0
%27 = trunc i64 %14 to i32
%28 = call i32 @inet_ntop(i32 129, i32* %26, i8* %16, i32 %27)
br label %39
29: ; preds = %21
%30 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr_in6*
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %30, i32 0, i32 0
%32 = trunc i64 %14 to i32
%33 = call i32 @inet_ntop(i32 128, i32* %31, i8* %16, i32 %32)
br label %39
34: ; preds = %21
%35 = load i8**, i8*** %7
%36 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %8, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @ret_err(i8** %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %37)
store i32 %38, i32* %4
store i32 1, i32* %12
br label %47
39: ; preds = %29, %24
%40 = call i8* @strdup(i8* %16)
%41 = load i8**, i8*** %6
store i8* %40, i8** %41
store i32 0, i32* %4
store i32 1, i32* %12
br label %47
42: ; preds = %3
%43 = load i8**, i8*** %7
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 @ret_err(i8** %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %45)
store i32 %46, i32* %4
store i32 1, i32* %12
br label %47
47: ; preds = %42, %39, %34
%48 = load i8*, i8** %10
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [26 x i8] c"Unknown address family %d\00"
@.str.1 = [16 x i8] c"getpeername: %s\00"
@errno = global i32 0
define i32 @etcp_getpeeraddr(i32 %0, i8** nocapture writeonly %1, i8** %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast %struct.sockaddr_storage* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%8 = load i32, i32* @INET6_ADDRSTRLEN
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%13 = call i64 @getpeername(i32 %0, %struct.sockaddr* %12, i32* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %3
%16 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
%17 = load i32, i32* %16
switch i32 %17, label %19 [
i32 129, label %21
i32 128, label %18
]
18: ; preds = %15
br label %21
19: ; preds = %15
%20 = call i32 @ret_err(i8** %2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %29
21: ; preds = %15, %18
%22 = phi i32 [ 128, %18 ], [ %17, %15 ]
%23 = call i32 @inet_ntop(i32 %22, i32* %16, i8* %11, i32 %8)
%24 = call i8* @strdup(i8* %11)
store i8* %24, i8** %1
br label %29
25: ; preds = %3
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @ret_err(i8** %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %21, %19
%30 = phi i32 [ %20, %19 ], [ 0, %21 ], [ %28, %25 ]
ret i32 %30
}