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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, size_t);
int ntohs(int /*<<< orphan*/);
int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
  struct sockaddr_storage sa;
  socklen_t salen = sizeof(sa);
  if (getpeername(fd, (struct sockaddr *)&sa, &salen) == -1) {
    if (port)
      *port = 0;
    ip[0] = '?';
    ip[1] = '\0';
    return -1;
  }
  if (sa.ss_family == AF_INET) {
    struct sockaddr_in *s = (struct sockaddr_in *)&sa;
    if (ip)
      inet_ntop(AF_INET, (void *)&(s->sin_addr), ip, ip_len);
    if (port)
      *port = ntohs(s->sin_port);
  } else {
    struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
    if (ip)
      inet_ntop(AF_INET6, (void *)&(s->sin6_addr), ip, ip_len);
    if (port)
      *port = ntohs(s->sin6_port);
  }
  return 0;
}


x86 assembly (clang, -O0):
<anetPeerToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1040 <getpeername@plt>
cmp    $0xffffffff,%eax
jne    1197 <anetPeerToString+0x67>
cmpq   $0x0,-0x20(%rbp)
je     117c <anetPeerToString+0x4c>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <anetPeerToString+0x12c>
mov    -0x28(%rbp),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
jne    1200 <anetPeerToString+0xd0>
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11dd <anetPeerToString+0xad>
mov    0x2e23(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     11fb <anetPeerToString+0xcb>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1255 <anetPeerToString+0x125>
lea    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1232 <anetPeerToString+0x102>
mov    0x2dc6(%rip),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     1250 <anetPeerToString+0x120>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1255 <anetPeerToString+0x125>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetPeerToString>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
movl   $0x8,0xc(%rsp)
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
call   1040 <getpeername@plt>
cmp    $0xffffffff,%eax
je     1173 <anetPeerToString+0x43>
mov    (%rsp),%rdi
mov    0x2e81(%rip),%rax
cmp    (%rax),%rdi
jne    118b <anetPeerToString+0x5b>
test   %rbx,%rbx
jne    119a <anetPeerToString+0x6a>
jmp    11aa <anetPeerToString+0x7a>
test   %r14,%r14
je     117f <anetPeerToString+0x4f>
movl   $0x0,(%r14)
movw   $0x3f,(%rbx)
mov    $0xffffffff,%ebx
jmp    11bc <anetPeerToString+0x8c>
test   %rbx,%rbx
je     11aa <anetPeerToString+0x7a>
mov    0x2e49(%rip),%rax
mov    (%rax),%rdi
lea    0x4(%rsp),%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <inet_ntop@plt>
xor    %ebx,%ebx
test   %r14,%r14
je     11bc <anetPeerToString+0x8c>
mov    (%rsp),%edi
call   1050 <ntohs@plt>
mov    %eax,(%r14)
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @anetPeerToString(i32 %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca %struct.sockaddr_storage
%11 = alloca i32
%12 = alloca %struct.sockaddr_in*
%13 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i32 8, i32* %11
%14 = load i32, i32* %6
%15 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%16 = call i32 @getpeername(i32 %14, %struct.sockaddr* %15, i32* %11)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %28
18: ; preds = %4
%19 = load i32*, i32** %9
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i32*, i32** %9
store i32 0, i32* %22
br label %23
23: ; preds = %21, %18
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i64 0
store i8 63, i8* %25
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i64 1
store i8 0, i8* %27
store i32 -1, i32* %5
br label %78
28: ; preds = %4
%29 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %10, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* @AF_INET
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %55
33: ; preds = %28
%34 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in*
store %struct.sockaddr_in* %34, %struct.sockaddr_in** %12
%35 = load i8*, i8** %7
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %45
37: ; preds = %33
%38 = load i64, i64* @AF_INET
%39 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %39, i32 0, i32 1
%41 = bitcast i32* %40 to i8*
%42 = load i8*, i8** %7
%43 = load i64, i64* %8
%44 = call i32 @inet_ntop(i64 %38, i8* %41, i8* %42, i64 %43)
br label %45
45: ; preds = %37, %33
%46 = load i32*, i32** %9
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @ntohs(i32 %51)
%53 = load i32*, i32** %9
store i32 %52, i32* %53
br label %54
54: ; preds = %48, %45
br label %77
55: ; preds = %28
%56 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %56, %struct.sockaddr_in6** %13
%57 = load i8*, i8** %7
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %67
59: ; preds = %55
%60 = load i64, i64* @AF_INET6
%61 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%62 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %61, i32 0, i32 1
%63 = bitcast i32* %62 to i8*
%64 = load i8*, i8** %7
%65 = load i64, i64* %8
%66 = call i32 @inet_ntop(i64 %60, i8* %63, i8* %64, i64 %65)
br label %67
67: ; preds = %59, %55
%68 = load i32*, i32** %9
%69 = icmp ne i32* %68, null
br i1 %69, label %70, label %76
70: ; preds = %67
%71 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%72 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 @ntohs(i32 %73)
%75 = load i32*, i32** %9
store i32 %74, i32* %75
br label %76
76: ; preds = %70, %67
br label %77
77: ; preds = %76, %54
store i32 0, i32* %5
br label %78
78: ; preds = %77, %23
%79 = load i32, i32* %5
ret i32 %79
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @anetPeerToString(i32 %0, i8* %1, i64 %2, i32* writeonly %3) {
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = bitcast %struct.sockaddr_storage* %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 8, i32* %6
%9 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%10 = call i32 @getpeername(i32 %0, %struct.sockaddr* %9, i32* %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %4
%13 = icmp eq i32* %3, null
br i1 %13, label %15, label %14
14: ; preds = %12
store i32 0, i32* %3
br label %15
15: ; preds = %14, %12
store i8 63, i8* %1
%16 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %16
br label %48
17: ; preds = %4
%18 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %5, i64 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @AF_INET
%21 = icmp eq i64 %19, %20
%22 = icmp eq i8* %1, null
br i1 %21, label %23, label %35
23: ; preds = %17
br i1 %22, label %29, label %24
24: ; preds = %23
%25 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i64 0, i32 1
%27 = bitcast i32* %26 to i8*
%28 = call i32 @inet_ntop(i64 %19, i8* %27, i8* %1, i64 %2)
br label %29
29: ; preds = %24, %23
%30 = icmp eq i32* %3, null
br i1 %30, label %48, label %31
31: ; preds = %29
%32 = bitcast %struct.sockaddr_storage* %5 to i32*
%33 = load i32, i32* %32
%34 = call i32 @ntohs(i32 %33)
store i32 %34, i32* %3
br label %48
35: ; preds = %17
br i1 %22, label %42, label %36
36: ; preds = %35
%37 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in6*
%38 = load i64, i64* @AF_INET6
%39 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %37, i64 0, i32 1
%40 = bitcast i32* %39 to i8*
%41 = call i32 @inet_ntop(i64 %38, i8* %40, i8* %1, i64 %2)
br label %42
42: ; preds = %36, %35
%43 = icmp eq i32* %3, null
br i1 %43, label %48, label %44
44: ; preds = %42
%45 = bitcast %struct.sockaddr_storage* %5 to i32*
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
store i32 %47, i32* %3
br label %48
48: ; preds = %31, %29, %44, %42, %15
%49 = phi i32 [ -1, %15 ], [ 0, %42 ], [ 0, %44 ], [ 0, %29 ], [ 0, %31 ]
ret i32 %49
}