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

/* Variables and functions */
int /*<<< orphan*/ errno;
int getpeername(int, struct sockaddr *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ ssgl_log(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void get_peer_name(int connection, char *buffer) {
  struct sockaddr_in peer;
  socklen_t addrlen;
  int rc;
  rc = getpeername(connection, (struct sockaddr *__restrict__)&peer, &addrlen);
  if (rc != 0) {
    ssgl_log(1, "getpeername failed: %s\n", strerror(errno));
    sprintf(buffer, "(UNKNOWN)");
    return;
  }
  sprintf(buffer, "%s:%hu", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));
}


x86 assembly (clang, -O0):
<get_peer_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <getpeername@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11c4 <get_peer_name+0x64>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe58(%rip),%rsi
call   1050 <ssgl_log@plt>
mov    -0x10(%rbp),%rdi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    11f8 <get_peer_name+0x98>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <ntohs@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_peer_name>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   1040 <getpeername@plt>
test   %eax,%eax
je     11b3 <get_peer_name+0x53>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe6e(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1050 <ssgl_log@plt>
movabs $0x4e574f4e4b4e5528,%rax
4f 57 4e
mov    %rax,(%rbx)
movw   $0x29,0x8(%rbx)
jmp    11de <get_peer_name+0x7e>
mov    0x8(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r14
mov    0xc(%rsp),%edi
call   1060 <ntohs@plt>
lea    0xe53(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <sprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"getpeername failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"(UNKNOWN)\00"
@.str.2 = [7 x i8] c"%s:%hu\00"
define void @get_peer_name(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i32, i32* %3
%9 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%10 = call i32 @getpeername(i32 %8, %struct.sockaddr* %9, i32* %6)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @ssgl_log(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i8*, i8** %4
%18 = call i32 (i8*, i8*, ...) @sprintf(i8* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %28
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i8* @inet_ntoa(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 @ntohs(i32 %25)
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %23, i32 %26)
br label %28
28: ; preds = %19, %13
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"getpeername failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"(UNKNOWN)\00"
@.str.2 = [7 x i8] c"%s:%hu\00"
define void @get_peer_name(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%8 = call i32 @getpeername(i32 %0, %struct.sockaddr* %7, i32* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = call i32 @ssgl_log(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %22
14: ; preds = %2
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i8* @inet_ntoa(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%19 = load i32, i32* %18
%20 = call i32 @ntohs(i32 %19)
%21 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %17, i32 %20)
br label %22
22: ; preds = %14, %10
ret void
}