#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;

/* Variables and functions */
int NI_MAXHOST;
int NI_MAXSERV;
int /*<<< orphan*/ NI_NUMERICSERV;
scalar_t__ getnameinfo(struct sockaddr const *, int /*<<< orphan*/, char *, int,
                       char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
char *inetAddressStr(const struct sockaddr *addr, socklen_t addrlen,
                     char *addrStr, int addrStrLen) {
  char host[NI_MAXHOST], service[NI_MAXSERV];
  if (getnameinfo(addr, addrlen, host, NI_MAXHOST, service, NI_MAXSERV,
                  NI_NUMERICSERV) == 0)
    snprintf(addrStr, addrStrLen, "(%s %s)", host, service);
  else
    snprintf(addrStr, addrStrLen, "(?UNKNOWN?)");
  addrStr[addrStrLen - 1] = '\0';
  return addrStr;
}


x86 assembly (clang, -O0):
<inetAddressStr>:
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    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%r8
sub    %rcx,%r8
mov    %r8,-0x40(%rbp)
mov    %r8,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e42(%rip),%rax
mov    (%rax),%r9d
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
jne    11e6 <inetAddressStr+0xc6>
mov    -0x40(%rbp),%r8
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    0xe26(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11fb <inetAddressStr+0xdb>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    0xe14(%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):
<inetAddressStr>:
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   %rax,%rax
je     11a4 <inetAddressStr+0x84>
lea    0xe73(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11be <inetAddressStr+0x9e>
lea    0xe55(%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 = [8 x i8] c"(%s %s)\00"
@.str.1 = [12 x i8] c"(?UNKNOWN?)\00"
define i8* @inetAddressStr(%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
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = load i32, i32* @NI_MAXHOST
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %9
%15 = alloca i8, i64 %13
store i64 %13, i64* %10
%16 = load i32, i32* @NI_MAXSERV
%17 = zext i32 %16 to i64
%18 = alloca i8, i64 %17
store i64 %17, i64* %11
%19 = load %struct.sockaddr*, %struct.sockaddr** %5
%20 = load i32, i32* %6
%21 = load i32, i32* @NI_MAXHOST
%22 = load i32, i32* @NI_MAXSERV
%23 = load i32, i32* @NI_NUMERICSERV
%24 = call i64 @getnameinfo(%struct.sockaddr* %19, i32 %20, i8* %15, i32 %21, i8* %18, i32 %22, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %4
%27 = load i8*, i8** %7
%28 = load i32, i32* %8
%29 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %27, i32 %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %18)
br label %34
30: ; preds = %4
%31 = load i8*, i8** %7
%32 = load i32, i32* %8
%33 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %31, i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %30, %26
%35 = load i8*, i8** %7
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %7
%41 = load i8*, i8** %9
ret i8* %40
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [8 x i8] c"(%s %s)\00"
@.str.1 = [12 x i8] c"(?UNKNOWN?)\00"
define i8* @inetAddressStr(%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 i64 @getnameinfo(%struct.sockaddr* %0, i32 %1, i8* %7, i32 %5, i8* %10, i32 %8, i32 %11)
%13 = icmp eq i64 %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 ([8 x i8], [8 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
}