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

/* Variables and functions */
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ getpeername(int, struct sockaddr *, int *);
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int xyz_sock_peeraddr(int sockfd, char *addr, int len) {
  struct sockaddr_in sa_in;
  socklen_t sa_len;
  char *paddr;
  int pport;
  if (addr == NULL || len < 15) {
    return -1;
  }
  sa_len = sizeof(struct sockaddr);
  bzero(&sa_in, sizeof(struct sockaddr_in));
  if (getpeername(sockfd, (struct sockaddr *)&sa_in, &sa_len) < 0) {
    return -1;
  }
  paddr = inet_ntoa(sa_in.sin_addr);
  pport = ntohs(sa_in.sin_port);
  if (paddr == NULL) {
    return -1;
  }
  strncpy(addr, paddr, len);
  return 0;
}


x86 assembly (clang, -O0):
<xyz_sock_peeraddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1177 <xyz_sock_peeraddr+0x27>
cmpl   $0xf,-0x14(%rbp)
jge    1183 <xyz_sock_peeraddr+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <xyz_sock_peeraddr+0xb3>
movl   $0x4,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <getpeername@plt>
cmp    $0x0,%rax
jge    11be <xyz_sock_peeraddr+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <xyz_sock_peeraddr+0xb3>
mov    -0x1c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ec <xyz_sock_peeraddr+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <xyz_sock_peeraddr+0xb3>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xyz_sock_peeraddr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%r15d
test   %rsi,%rsi
je     11cd <xyz_sock_peeraddr+0x7d>
mov    %edx,%r14d
cmp    $0xf,%edx
jl     11cd <xyz_sock_peeraddr+0x7d>
mov    %rsi,%rbp
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
mov    %r12,%rsi
call   1050 <getpeername@plt>
test   %rax,%rax
js     11cd <xyz_sock_peeraddr+0x7d>
mov    0xc(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1060 <ntohs@plt>
test   %rbx,%rbx
je     11cd <xyz_sock_peeraddr+0x7d>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
define i32 @xyz_sock_peeraddr(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 15
br i1 %16, label %17, label %18
17: ; preds = %14, %3
store i32 -1, i32* %4
br label %40
18: ; preds = %14
store i32 4, i32* %9
%19 = call i32 @bzero(%struct.sockaddr_in* %8, i32 8)
%20 = load i32, i32* %5
%21 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%22 = call i64 @getpeername(i32 %20, %struct.sockaddr* %21, i32* %9)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 -1, i32* %4
br label %40
25: ; preds = %18
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i8* @inet_ntoa(i32 %27)
store i8* %28, i8** %10
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @ntohs(i32 %30)
store i32 %31, i32* %11
%32 = load i8*, i8** %10
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %25
store i32 -1, i32* %4
br label %40
35: ; preds = %25
%36 = load i8*, i8** %6
%37 = load i8*, i8** %10
%38 = load i32, i32* %7
%39 = call i32 @strncpy(i8* %36, i8* %37, i32 %38)
store i32 0, i32* %4
br label %40
40: ; preds = %35, %34, %24, %17
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
define i32 @xyz_sock_peeraddr(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = icmp eq i8* %1, null
%9 = icmp slt i32 %2, 15
%10 = or i1 %8, %9
br i1 %10, label %26, label %11
11: ; preds = %3
store i32 4, i32* %5
%12 = call i32 @bzero(%struct.sockaddr_in* %4, i32 8)
%13 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%14 = call i64 @getpeername(i32 %0, %struct.sockaddr* %13, i32* %5)
%15 = icmp slt i64 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %11
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i8* @inet_ntoa(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @ntohs(i32 %21)
%23 = icmp eq i8* %19, null
br i1 %23, label %26, label %24
24: ; preds = %16
%25 = call i32 @strncpy(i8* %1, i8* %19, i32 %2)
br label %26
26: ; preds = %16, %11, %3, %24
%27 = phi i32 [ 0, %24 ], [ -1, %3 ], [ -1, %11 ], [ -1, %16 ]
ret i32 %27
}