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

/* Variables and functions */
int get_addr_name(struct sockaddr *, char *, int);
int getpeername(int, struct sockaddr *, int *);
int get_peername(int fd, char *dst, socklen_t dst_size) {
  struct sockaddr_storage addr;
  socklen_t addr_len = sizeof(addr);
  if (getpeername(fd, (struct sockaddr *)&addr, &addr_len) == -1)
    return -1;
  return get_addr_name((struct sockaddr *)&addr, dst, dst_size);
}


x86 assembly (clang, -O0):
<get_peername>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1030 <getpeername@plt>
cmp    $0xffffffff,%eax
jne    115e <get_peername+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1171 <get_peername+0x51>
lea    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <get_addr_name@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_peername>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1030 <getpeername@plt>
cmp    $0xffffffff,%eax
je     1151 <get_peername+0x31>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <get_addr_name@plt>
jmp    1156 <get_peername+0x36>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @get_peername(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 4, i32* %9
%10 = load i32, i32* %5
%11 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%12 = call i32 @getpeername(i32 %10, %struct.sockaddr* %11, i32* %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %20
15: ; preds = %3
%16 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = call i32 @get_addr_name(%struct.sockaddr* %16, i8* %17, i32 %18)
store i32 %19, i32* %4
br label %20
20: ; preds = %15, %14
%21 = load i32, i32* %4
ret i32 %21
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @get_peername(i32 %0, i8* %1, i32 %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 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%9 = call i32 @getpeername(i32 %0, %struct.sockaddr* %8, i32* %5)
%10 = icmp eq i32 %9, -1
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = call i32 @get_addr_name(%struct.sockaddr* %8, i8* %1, i32 %2)
br label %13
13: ; preds = %3, %11
%14 = phi i32 [ %12, %11 ], [ -1, %3 ]
ret i32 %14
}