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

/* Variables and functions */
int NI_MAXHOST;
int NI_MAXSERV;
int NI_NOFQDN;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
scalar_t__ getnameinfo(struct sockaddr *, int, char *, int, char *, int, int);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
char *getNameInfo(struct sockaddr_in *sa) {
  char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
  char *resbuff;
  if (getnameinfo((struct sockaddr *)sa, sizeof(struct sockaddr_in), hbuf,
                  sizeof(hbuf), sbuf, sizeof(sbuf),
                  NI_NOFQDN | NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
    resbuff = malloc(NI_MAXHOST + NI_MAXSERV + 2);
    sprintf(resbuff, "ADDRESS %s:%s", hbuf, sbuf);
  } else {
    perror("getnameinfo");
    resbuff = NULL;
  }
  return resbuff;
}


x86 assembly (clang, -O0):
<getNameInfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rdx
sub    %rax,%rdx
mov    %rdx,-0x38(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x18(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%r8
sub    %rsi,%r8
mov    %r8,-0x30(%rbp)
mov    %r8,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    %eax,%r9d
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    0x2e12(%rip),%rsi
or     (%rsi),%eax
mov    0x2e29(%rip),%rsi
or     (%rsi),%eax
mov    $0x4,%esi
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1030 <getnameinfo@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
jne    121b <getNameInfo+0xdb>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x2de8(%rip),%rax
add    (%rax),%edi
add    $0x2,%edi
call   1040 <malloc@plt>
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1060 <sprintf@plt>
jmp    122f <getNameInfo+0xef>
lea    0xdec(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNameInfo>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e6e(%rip),%r15
mov    (%r15),%ecx
mov    %rsp,%r14
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2e68(%rip),%r12
mov    (%r12),%r9d
mov    %rsp,%rbx
mov    %r9,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e51(%rip),%rsi
mov    0x2e32(%rip),%rdx
mov    (%rdx),%eax
or     (%rsi),%eax
mov    0x2e47(%rip),%rdx
or     (%rdx),%eax
sub    $0x8,%rsp
mov    $0x4,%esi
mov    %r14,%rdx
mov    %rbx,%r8
push   %rax
call   1030 <getnameinfo@plt>
add    $0x10,%rsp
test   %rax,%rax
jne    11fb <getNameInfo+0xbb>
mov    (%r15),%eax
mov    (%r12),%ecx
lea    (%rax,%rcx,1),%edi
add    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
lea    0xe23(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1060 <sprintf@plt>
mov    %r15,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0c(%rip),%rdi
call   1050 <perror@plt>
xor    %r15d,%r15d
jmp    11eb <getNameInfo+0xab>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NOFQDN = global i32 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [14 x i8] c"ADDRESS %s:%s\00"
@.str.1 = [12 x i8] c"getnameinfo\00"
define i8* @getNameInfo(%struct.sockaddr_in* %0) {
%2 = alloca %struct.sockaddr_in*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %2
%7 = load i32, i32* @NI_MAXHOST
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %3
%10 = alloca i8, i64 %8
store i64 %8, i64* %4
%11 = load i32, i32* @NI_MAXSERV
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
store i64 %12, i64* %5
%14 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%15 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%16 = trunc i64 %8 to i32
%17 = trunc i64 %12 to i32
%18 = load i32, i32* @NI_NOFQDN
%19 = load i32, i32* @NI_NUMERICHOST
%20 = or i32 %18, %19
%21 = load i32, i32* @NI_NUMERICSERV
%22 = or i32 %20, %21
%23 = call i64 @getnameinfo(%struct.sockaddr* %15, i32 4, i8* %10, i32 %16, i8* %13, i32 %17, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %1
%26 = load i32, i32* @NI_MAXHOST
%27 = load i32, i32* @NI_MAXSERV
%28 = add nsw i32 %26, %27
%29 = add nsw i32 %28, 2
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %13)
br label %35
33: ; preds = %1
%34 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %6
br label %35
35: ; preds = %33, %25
%36 = load i8*, i8** %6
%37 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NOFQDN = global i32 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str = [14 x i8] c"ADDRESS %s:%s\00"
@.str.1 = [12 x i8] c"getnameinfo\00"
define noalias i8* @getNameInfo(%struct.sockaddr_in* %0) {
%2 = load i32, i32* @NI_MAXHOST
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = load i32, i32* @NI_MAXSERV
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast %struct.sockaddr_in* %0 to %struct.sockaddr*
%9 = load i32, i32* @NI_NOFQDN
%10 = load i32, i32* @NI_NUMERICHOST
%11 = or i32 %10, %9
%12 = load i32, i32* @NI_NUMERICSERV
%13 = or i32 %11, %12
%14 = call i64 @getnameinfo(%struct.sockaddr* %8, i32 4, i8* %4, i32 %2, i8* %7, i32 %5, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %1
%17 = load i32, i32* @NI_MAXHOST
%18 = load i32, i32* @NI_MAXSERV
%19 = add i32 %17, 2
%20 = add i32 %19, %18
%21 = call i8* @malloc(i32 %20)
%22 = call i32 @sprintf(i8* dereferenceable(1) %21, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %7)
br label %25
23: ; preds = %1
%24 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
%26 = phi i8* [ %21, %16 ], [ null, %23 ]
ret i8* %26
}