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

/* Variables and functions */
int /*<<< orphan*/ errno;
int getpeername(int, struct sockaddr *, int *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sa_log(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *peerid(int fd) {
  static char buf[64];
  struct sockaddr_in addr;
  socklen_t addrlen;
  int ret;
  memset(buf, 0, 64);
  addrlen = sizeof(addr);
  ret = getpeername(fd, (struct sockaddr *)&addr, &addrlen);
  if (ret != 0) {
    sa_log("ERROR: getpeername failed: %s", strerror(errno));
    sprintf(buf, "%s", "ERROR");
    return buf;
  }
  sprintf(buf, "%s:%u", inet_ntoa(addr.sin_addr), addr.sin_port);
  return buf;
}


x86 assembly (clang, -O0):
<peerid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
lea    0x2eee(%rip),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
movl   $0x8,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <getpeername@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11ea <peerid+0x8a>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe47(%rip),%rdi
call   1060 <sa_log@plt>
lea    0x2e9b(%rip),%rdi
lea    0xe4f(%rip),%rsi
lea    0xe4b(%rip),%rdx
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    0x2e7f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1218 <peerid+0xb8>
mov    -0x14(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%ecx
lea    0x2e61(%rip),%rdi
lea    0xe1e(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    0x2e4c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peerid>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x2ef2(%rip),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1040 <getpeername@plt>
test   %eax,%eax
je     11c8 <peerid+0x68>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe54(%rip),%rdi
mov    %eax,%esi
call   1060 <sa_log@plt>
movl   $0x4f525245,0x2ea3(%rip)
52 52 4f
movw   $0x52,0x2e9e(%rip)
jmp    11ed <peerid+0x8d>
mov    0xc(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    0x8(%rsp),%ecx
lea    0x2e84(%rip),%rdi
lea    0xe41(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
lea    0x2e6c(%rip),%rax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@peerid.buf = internal global [64 x i8] zeroinitializer
@.str = [30 x i8] c"ERROR: getpeername failed: %s\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"ERROR\00"
@.str.3 = [6 x i8] c"%s:%u\00"
define i8* @peerid(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 @memset(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @peerid.buf, i64 0, i64 0), i32 0, i32 64)
store i32 8, i32* %3
%7 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%8 = call i32 @getpeername(i32 %0, %struct.sockaddr* %7, i32* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = call i32 @sa_log(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %21
14: ; preds = %1
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i8* @inet_ntoa(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @peerid.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %17, i32 %19)
br label %21
21: ; preds = %14, %10
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @peerid.buf, i64 0, i64 0)
}