#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;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *,
                             int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
void getpeerinfo(int socket, char ipstr[], int *port) {
  struct sockaddr_storage addr;
  socklen_t len;
  len = sizeof(addr);
  int connection_counter = 0;
  getpeername(socket, (struct sockaddr *)&addr, &len);
  if (addr.ss_family == AF_INET) {
    struct sockaddr_in *s = (struct sockaddr_in *)&addr;
    *port = ntohs(s->sin_port);
    inet_ntop(AF_INET, &s->sin_addr, ipstr, INET6_ADDRSTRLEN);
  } else {
    struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
    *port = ntohs(s->sin6_port);
    inet_ntop(AF_INET6, &s->sin6_addr, ipstr, INET6_ADDRSTRLEN);
  }
  printf("\n Peer IP address: %s\n", ipstr);
  printf("\n Peer port      : %d\n", *port);
}


x86 assembly (clang, -O0):
<getpeerinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x8,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <getpeername@plt>
mov    -0x20(%rbp),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <getpeerinfo+0x86>
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e38(%rip),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2e2e(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
jmp    1202 <getpeerinfo+0xc2>
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2def(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    -0x10(%rbp),%rsi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpeerinfo>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%rdx
mov    %r15,%rsi
call   1040 <getpeername@plt>
mov    0x10(%rsp),%rax
mov    0x2e69(%rip),%rbx
mov    %rax,%rdi
shr    $0x20,%rdi
cmp    (%rbx),%rax
je     118a <getpeerinfo+0x4a>
mov    0x2e4e(%rip),%rbx
call   1060 <ntohs@plt>
mov    %eax,(%r14)
mov    (%rbx),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <inet_ntop@plt>
lea    0xe50(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r14),%esi
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
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
@INET6_ADDRSTRLEN = global i32 0
@AF_INET6 = global i64 0
@.str = [23 x i8] c"\0A Peer IP address: %s\0A\00"
@.str.1 = [23 x i8] c"\0A Peer port : %d\0A\00"
define void @getpeerinfo(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.sockaddr_storage
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in*
%11 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i32 8, i32* %8
store i32 0, i32* %9
%12 = load i32, i32* %4
%13 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%14 = call i32 @getpeername(i32 %12, %struct.sockaddr* %13, i32* %8)
%15 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %7, i32 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @AF_INET
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %32
19: ; preds = %3
%20 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in*
store %struct.sockaddr_in* %20, %struct.sockaddr_in** %10
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
%25 = load i32*, i32** %6
store i32 %24, i32* %25
%26 = load i64, i64* @AF_INET
%27 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i32 0, i32 0
%29 = load i8*, i8** %5
%30 = load i32, i32* @INET6_ADDRSTRLEN
%31 = call i32 @inet_ntop(i64 %26, i32* %28, i8* %29, i32 %30)
br label %45
32: ; preds = %3
%33 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %33, %struct.sockaddr_in6** %11
%34 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%35 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 @ntohs(i32 %36)
%38 = load i32*, i32** %6
store i32 %37, i32* %38
%39 = load i64, i64* @AF_INET6
%40 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%41 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %40, i32 0, i32 0
%42 = load i8*, i8** %5
%43 = load i32, i32* @INET6_ADDRSTRLEN
%44 = call i32 @inet_ntop(i64 %39, i32* %41, i8* %42, i32 %43)
br label %45
45: ; preds = %32, %19
%46 = load i8*, i8** %5
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %6
%49 = load i32, i32* %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %49)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
@INET6_ADDRSTRLEN = global i32 0
@AF_INET6 = global i64 0
@.str = [23 x i8] c"\0A Peer IP address: %s\0A\00"
@.str.1 = [23 x i8] c"\0A Peer port : %d\0A\00"
define void @getpeerinfo(i32 %0, i8* %1, i32* nocapture %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 8, i32* %5
%8 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%9 = call i32 @getpeername(i32 %0, %struct.sockaddr* %8, i32* %5)
%10 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* @AF_INET
%13 = icmp eq i64 %11, %12
%14 = lshr i64 %11, 32
%15 = trunc i64 %14 to i32
%16 = call i32 @ntohs(i32 %15)
store i32 %16, i32* %2
%17 = load i64, i64* @AF_INET
%18 = load i64, i64* @AF_INET6
%19 = select i1 %13, i64 %17, i64 %18
%20 = bitcast %struct.sockaddr_storage* %4 to i32*
%21 = load i32, i32* @INET6_ADDRSTRLEN
%22 = call i32 @inet_ntop(i64 %19, i32* %20, i8* %1, i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %1)
%24 = load i32, i32* %2
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %24)
ret void
}