#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 {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ sbuf;
typedef int /*<<< orphan*/ hbuf;

/* Variables and functions */
int NI_MAXHOST;
int NI_MAXSERV;
int /*<<< orphan*/ NI_NUMERICSERV;
int getnameinfo(struct sockaddr const *, int /*<<< orphan*/, char *, int,
                char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
char *get_addr_str(const struct sockaddr *addr, socklen_t addrlen,
                   char *addr_str, int addr_str_len) {
  char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
  int ret = getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
                        NI_NUMERICSERV);
  if (ret == 0) {
    snprintf(addr_str, addr_str_len, "(%s, %s)", hbuf, sbuf);
  } else {
    snprintf(addr_str, addr_str_len, "(?UNKNOWN?)");
  }
  addr_str[addr_str_len - 1] = '\0';
  return addr_str;
}


x86 assembly (clang, -O0):
<get_addr_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rdx
sub    %rax,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%r8
sub    %rsi,%r8
mov    %r8,-0x48(%rbp)
mov    %r8,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    %eax,%r9d
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    11db <get_addr_str+0xbb>
mov    -0x48(%rbp),%r8
mov    -0x50(%rbp),%rcx
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    0xe31(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11f0 <get_addr_str+0xd0>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    0xe20(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_addr_str>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%r15
mov    0x2e98(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%r12
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x2e8b(%rip),%rax
mov    (%rax),%r9d
mov    %rsp,%rbx
mov    %r9,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    %r12,%rdx
mov    %rbx,%r8
push   %rax
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
je     11a3 <get_addr_str+0x83>
lea    0xe75(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11bd <get_addr_str+0x9d>
lea    0xe56(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %r12,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1030 <snprintf@plt>
movslq %r14d,%rax
movb   $0x0,-0x1(%rax,%r15,1)
mov    %r15,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [9 x i8] c"(%s, %s)\00"
@.str.1 = [12 x i8] c"(?UNKNOWN?)\00"
define i8* @get_addr_str(%struct.sockaddr* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = load i32, i32* @NI_MAXHOST
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = load i32, i32* @NI_MAXSERV
%18 = zext i32 %17 to i64
%19 = alloca i8, i64 %18
store i64 %18, i64* %11
%20 = load %struct.sockaddr*, %struct.sockaddr** %5
%21 = load i32, i32* %6
%22 = trunc i64 %14 to i32
%23 = trunc i64 %18 to i32
%24 = load i32, i32* @NI_NUMERICSERV
%25 = call i32 @getnameinfo(%struct.sockaddr* %20, i32 %21, i8* %16, i32 %22, i8* %19, i32 %23, i32 %24)
store i32 %25, i32* %12
%26 = load i32, i32* %12
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %4
%29 = load i8*, i8** %7
%30 = load i32, i32* %8
%31 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %29, i32 %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %19)
br label %36
32: ; preds = %4
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %33, i32 %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %32, %28
%37 = load i8*, i8** %7
%38 = load i32, i32* %8
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %7
%43 = load i8*, i8** %9
ret i8* %42
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [9 x i8] c"(%s, %s)\00"
@.str.1 = [12 x i8] c"(?UNKNOWN?)\00"
define i8* @get_addr_str(%struct.sockaddr* %0, i32 %1, i8* returned %2, i32 %3) {
%5 = load i32, i32* @NI_MAXHOST
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = load i32, i32* @NI_MAXSERV
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = load i32, i32* @NI_NUMERICSERV
%12 = call i32 @getnameinfo(%struct.sockaddr* %0, i32 %1, i8* %7, i32 %5, i8* %10, i32 %8, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %10)
br label %18
16: ; preds = %4
%17 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = add nsw i32 %3, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %2, i64 %20
store i8 0, i8* %21
ret i8* %2
}