#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;
};
struct hostent {
  char const *h_name;
};
typedef scalar_t__ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EAI_NONAME;
int EAI_OVERFLOW;
int NI_NAMEREQD;
int NI_NUMERICHOST;
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int getnameinfo_get_host(const struct sockaddr_in *sockaddr, char *node,
                         socklen_t nodelen, int flags) {
  struct hostent *hostent;
  const char *ipaddr;
  if ((flags & NI_NUMERICHOST) != NI_NUMERICHOST) {
    hostent = gethostbyaddr((char *)&sockaddr->sin_addr,
                            sizeof(sockaddr->sin_addr), AF_INET);
    if (hostent && hostent->h_name) {
      if (nodelen <= strlen(hostent->h_name))
        return EAI_OVERFLOW;
      strcpy(node, hostent->h_name);
      return 0;
    }
  }
  if ((flags & NI_NAMEREQD) == NI_NAMEREQD)
    return EAI_NONAME;
  ipaddr = inet_ntoa(sockaddr->sin_addr);
  if (nodelen <= strlen(ipaddr))
    return EAI_OVERFLOW;
  strcpy(node, ipaddr);
  return 0;
}


x86 assembly (clang, -O0):
<getnameinfo_get_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e5f(%rip),%rcx
and    (%rcx),%eax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%eax
je     11fc <getnameinfo_get_host+0xbc>
mov    -0x10(%rbp),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1060 <gethostbyaddr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f7 <getnameinfo_get_host+0xb7>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     11f7 <getnameinfo_get_host+0xb7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jg     11db <getnameinfo_get_host+0x9b>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127d <getnameinfo_get_host+0x13d>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    127d <getnameinfo_get_host+0x13d>
jmp    11fc <getnameinfo_get_host+0xbc>
mov    -0x24(%rbp),%eax
mov    0x2dea(%rip),%rcx
and    (%rcx),%eax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%eax
jne    1228 <getnameinfo_get_host+0xe8>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127d <getnameinfo_get_host+0x13d>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
jg     1269 <getnameinfo_get_host+0x129>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127d <getnameinfo_get_host+0x13d>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnameinfo_get_host>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e66(%rip),%rax
not    %ebp
test   %ebp,(%rax)
je     1198 <getnameinfo_get_host+0x58>
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1060 <gethostbyaddr@plt>
test   %rax,%rax
je     1198 <getnameinfo_get_host+0x58>
mov    (%rax),%r12
test   %r12,%r12
je     1198 <getnameinfo_get_host+0x58>
mov    %r12,%rdi
call   1050 <strlen@plt>
cmp    %r15,%rax
jge    11d2 <getnameinfo_get_host+0x92>
mov    %r14,%rdi
mov    %r12,%rsi
jmp    11c9 <getnameinfo_get_host+0x89>
mov    0x2e51(%rip),%rax
test   %ebp,(%rax)
jne    11ac <getnameinfo_get_host+0x6c>
mov    0x2e2e(%rip),%rax
jmp    11d9 <getnameinfo_get_host+0x99>
mov    (%rbx),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    %r15,%rax
jge    11d2 <getnameinfo_get_host+0x92>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
jmp    11db <getnameinfo_get_host+0x9b>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.hostent = type { i8* }
@NI_NUMERICHOST = global i32 0
@AF_INET = global i32 0
@EAI_OVERFLOW = global i32 0
@NI_NAMEREQD = global i32 0
@EAI_NONAME = global i32 0
define i32 @getnameinfo_get_host(%struct.sockaddr_in* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.hostent*
%11 = alloca i8*
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = load i32, i32* @NI_NUMERICHOST
%14 = and i32 %12, %13
%15 = load i32, i32* @NI_NUMERICHOST
%16 = icmp ne i32 %14, %15
br i1 %16, label %17, label %46
17: ; preds = %4
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
%20 = bitcast i32* %19 to i8*
%21 = load i32, i32* @AF_INET
%22 = call %struct.hostent* @gethostbyaddr(i8* %20, i32 4, i32 %21)
store %struct.hostent* %22, %struct.hostent** %10
%23 = load %struct.hostent*, %struct.hostent** %10
%24 = icmp ne %struct.hostent* %23, null
br i1 %24, label %25, label %45
25: ; preds = %17
%26 = load %struct.hostent*, %struct.hostent** %10
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %45
30: ; preds = %25
%31 = load i64, i64* %8
%32 = load %struct.hostent*, %struct.hostent** %10
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i64 @strlen(i8* %34)
%36 = icmp sle i64 %31, %35
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = load i32, i32* @EAI_OVERFLOW
store i32 %38, i32* %5
br label %69
39: ; preds = %30
%40 = load i8*, i8** %7
%41 = load %struct.hostent*, %struct.hostent** %10
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @strcpy(i8* %40, i8* %43)
store i32 0, i32* %5
br label %69
45: ; preds = %25, %17
br label %46
46: ; preds = %45, %4
%47 = load i32, i32* %9
%48 = load i32, i32* @NI_NAMEREQD
%49 = and i32 %47, %48
%50 = load i32, i32* @NI_NAMEREQD
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = load i32, i32* @EAI_NONAME
store i32 %53, i32* %5
br label %69
54: ; preds = %46
%55 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i8* @inet_ntoa(i32 %57)
store i8* %58, i8** %11
%59 = load i64, i64* %8
%60 = load i8*, i8** %11
%61 = call i64 @strlen(i8* %60)
%62 = icmp sle i64 %59, %61
br i1 %62, label %63, label %65
63: ; preds = %54
%64 = load i32, i32* @EAI_OVERFLOW
store i32 %64, i32* %5
br label %69
65: ; preds = %54
%66 = load i8*, i8** %7
%67 = load i8*, i8** %11
%68 = call i32 @strcpy(i8* %66, i8* %67)
store i32 0, i32* %5
br label %69
69: ; preds = %65, %63, %52, %39, %37
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.hostent = type { i8* }
@NI_NUMERICHOST = global i32 0
@AF_INET = global i32 0
@EAI_OVERFLOW = global i32 0
@NI_NAMEREQD = global i32 0
@EAI_NONAME = global i32 0
define i32 @getnameinfo_get_host(%struct.sockaddr_in* %0, i8* %1, i64 %2, i32 %3) {
%5 = load i32, i32* @NI_NUMERICHOST
%6 = and i32 %5, %3
%7 = icmp eq i32 %6, %5
br i1 %7, label %24, label %8
8: ; preds = %4
%9 = bitcast %struct.sockaddr_in* %0 to i8*
%10 = load i32, i32* @AF_INET
%11 = tail call %struct.hostent* @gethostbyaddr(i8* %9, i32 4, i32 %10)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %24, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %24, label %17
17: ; preds = %13
%18 = tail call i64 @strlen(i8* %15)
%19 = icmp slt i64 %18, %2
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = load i32, i32* @EAI_OVERFLOW
br label %40
22: ; preds = %17
%23 = tail call i32 @strcpy(i8* %1, i8* %15)
br label %40
24: ; preds = %8, %13, %4
%25 = load i32, i32* @NI_NAMEREQD
%26 = and i32 %25, %3
%27 = icmp eq i32 %26, %25
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* @EAI_NONAME
br label %40
30: ; preds = %24
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%32 = load i32, i32* %31
%33 = tail call i8* @inet_ntoa(i32 %32)
%34 = tail call i64 @strlen(i8* dereferenceable(1) %33)
%35 = icmp slt i64 %34, %2
br i1 %35, label %38, label %36
36: ; preds = %30
%37 = load i32, i32* @EAI_OVERFLOW
br label %40
38: ; preds = %30
%39 = tail call i32 @strcpy(i8* %1, i8* %33)
br label %40
40: ; preds = %38, %36, %28, %22, %20
%41 = phi i32 [ %21, %20 ], [ 0, %22 ], [ %29, %28 ], [ %37, %36 ], [ 0, %38 ]
ret i32 %41
}