#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
typedef int socklen_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int EAI_FAMILY;
int EAI_NONAME;
int EAI_OVERFLOW;
int NI_NAMEREQD;
int NI_NUMERICHOST;
scalar_t__ __dns_get_rr(char *, int /*<<< orphan*/, int, int, unsigned char *,
                        int, int);
scalar_t__ __dns_query(unsigned char *, unsigned char *, int, int);
int /*<<< orphan*/ inet_ntop(int, unsigned char *, char *, int);
int ntohs(int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
                char *restrict node, socklen_t nodelen, char *restrict serv,
                socklen_t servlen, int flags) {
  char buf[256];
  unsigned char reply[512];
  int af = sa->sa_family;
  unsigned char *a;
  switch (af) {
  case AF_INET:
    a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
    if (sl != sizeof(struct sockaddr_in))
      return EAI_FAMILY;
    break;
  case AF_INET6:
    a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
    if (sl != sizeof(struct sockaddr_in6))
      return EAI_FAMILY;
    break;
  default:
    return EAI_FAMILY;
  }
  if (node && nodelen) {
    if ((flags & NI_NUMERICHOST) || __dns_query(reply, a, af, 1) <= 0 ||
        __dns_get_rr(buf, 0, 256, 1, reply, 12, 1) <= 0) {
      if (flags & NI_NAMEREQD)
        return EAI_NONAME;
      inet_ntop(af, a, buf, sizeof buf);
    }
    if (strlen(buf) >= nodelen)
      return EAI_OVERFLOW;
    strcpy(node, buf);
  }
  if (serv && servlen) {
    if (snprintf(buf, sizeof buf, "%d",
                 ntohs(((struct sockaddr_in *)sa)->sin_port)) >= servlen)
      return EAI_OVERFLOW;
    strcpy(serv, buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<getnameinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x370,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x344(%rbp)
mov    -0x344(%rbp),%eax
mov    %eax,-0x354(%rbp)
sub    $0x80,%eax
je     1205 <getnameinfo+0x95>
jmp    11bc <getnameinfo+0x4c>
mov    -0x354(%rbp),%eax
sub    $0x81,%eax
jne    1234 <getnameinfo+0xc4>
jmp    11d2 <getnameinfo+0x62>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x350(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x8,%rax
je     1200 <getnameinfo+0x90>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13c0 <getnameinfo+0x250>
jmp    1245 <getnameinfo+0xd5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x350(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     122f <getnameinfo+0xbf>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13c0 <getnameinfo+0x250>
jmp    1245 <getnameinfo+0xd5>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13c0 <getnameinfo+0x250>
cmpq   $0x0,-0x20(%rbp)
je     1347 <getnameinfo+0x1d7>
cmpl   $0x0,-0x24(%rbp)
je     1347 <getnameinfo+0x1d7>
mov    0x10(%rbp),%eax
mov    0x2d5c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12cd <getnameinfo+0x15d>
lea    -0x340(%rbp),%rdi
mov    -0x350(%rbp),%rsi
mov    -0x344(%rbp),%edx
mov    $0x1,%ecx
call   1090 <__dns_query@plt>
cmp    $0x0,%rax
jle    12cd <getnameinfo+0x15d>
lea    -0x140(%rbp),%rdi
lea    -0x340(%rbp),%r8
xor    %esi,%esi
mov    $0x100,%edx
mov    $0x1,%ecx
mov    $0xc,%r9d
movl   $0x1,(%rsp)
call   1060 <__dns_get_rr@plt>
cmp    $0x0,%rax
jg     1311 <getnameinfo+0x1a1>
mov    0x10(%rbp),%eax
mov    0x2d19(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12f3 <getnameinfo+0x183>
mov    0x2cf7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13c0 <getnameinfo+0x250>
mov    -0x344(%rbp),%edi
mov    -0x350(%rbp),%rsi
lea    -0x140(%rbp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
lea    -0x140(%rbp),%rdi
call   1050 <strlen@plt>
cmp    -0x24(%rbp),%eax
jl     1337 <getnameinfo+0x1c7>
mov    0x2ca3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13c0 <getnameinfo+0x250>
mov    -0x20(%rbp),%rdi
lea    -0x140(%rbp),%rsi
call   1040 <strcpy@plt>
cmpq   $0x0,-0x30(%rbp)
je     13b9 <getnameinfo+0x249>
cmpl   $0x0,-0x34(%rbp)
je     13b9 <getnameinfo+0x249>
lea    -0x140(%rbp),%rax
mov    %rax,-0x360(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1080 <ntohs@plt>
mov    -0x360(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xc76(%rip),%rdx
call   1070 <snprintf@plt>
cmp    -0x34(%rbp),%eax
jl     13a9 <getnameinfo+0x239>
mov    0x2c31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13c0 <getnameinfo+0x250>
mov    -0x30(%rbp),%rdi
lea    -0x140(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x370,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnameinfo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %r9d,%r15d
mov    %r8,%r14
mov    %ecx,%r13d
mov    %rdx,%r12
mov    %rdi,%rbx
mov    (%rdi),%ebp
cmp    $0x80,%ebp
je     11ad <getnameinfo+0x3d>
cmp    $0x81,%ebp
jne    11f1 <getnameinfo+0x81>
cmp    $0x8,%esi
jne    11f1 <getnameinfo+0x81>
lea    0x4(%rbx),%rcx
jmp    11b5 <getnameinfo+0x45>
mov    %rbx,%rcx
cmp    $0x4,%esi
jne    11f1 <getnameinfo+0x81>
test   %r12,%r12
je     12ac <getnameinfo+0x13c>
test   %r13d,%r13d
je     12ac <getnameinfo+0x13c>
mov    0x350(%rsp),%edx
mov    0x2deb(%rip),%rax
test   %edx,(%rax)
je     120e <getnameinfo+0x9e>
mov    0x2e10(%rip),%rax
test   %edx,(%rax)
je     127c <getnameinfo+0x10c>
mov    0x2df1(%rip),%rax
jmp    11f8 <getnameinfo+0x88>
mov    0x2de0(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x110(%rsp),%rdi
mov    %rcx,%rsi
mov    %ebp,%edx
mov    %rcx,0x8(%rsp)
mov    $0x1,%ecx
call   1090 <__dns_query@plt>
mov    0x350(%rsp),%edx
mov    0x8(%rsp),%rcx
test   %rax,%rax
jle    11d9 <getnameinfo+0x69>
movl   $0x1,(%rsp)
lea    0x10(%rsp),%rdi
lea    0x110(%rsp),%r8
xor    %esi,%esi
mov    $0x100,%edx
mov    $0x1,%ecx
mov    $0xc,%r9d
call   1060 <__dns_get_rr@plt>
mov    0x350(%rsp),%edx
mov    0x8(%rsp),%rcx
test   %rax,%rax
jg     1290 <getnameinfo+0x120>
jmp    11d9 <getnameinfo+0x69>
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
mov    %rcx,%rsi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
lea    0x10(%rsp),%rdi
call   1050 <strlen@plt>
cmp    %r13d,%eax
jge    12f6 <getnameinfo+0x186>
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
call   1040 <strcpy@plt>
xor    %ebp,%ebp
test   %r14,%r14
je     11fa <getnameinfo+0x8a>
test   %r15d,%r15d
je     11fa <getnameinfo+0x8a>
mov    (%rbx),%edi
call   1080 <ntohs@plt>
lea    0xd32(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
cmp    %r15d,%eax
jge    12f6 <getnameinfo+0x186>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    11fa <getnameinfo+0x8a>
mov    0x2cd3(%rip),%rax
jmp    11f8 <getnameinfo+0x88>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32 }
@EAI_FAMILY = global i32 0
@NI_NUMERICHOST = global i32 0
@NI_NAMEREQD = global i32 0
@EAI_NONAME = global i32 0
@EAI_OVERFLOW = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @getnameinfo(%struct.sockaddr* noalias %0, i32 %1, i8* noalias %2, i32 %3, i8* noalias %4, i32 %5, i32 %6) {
%8 = alloca i32
%9 = alloca %struct.sockaddr*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca [256 x i8]
%17 = alloca [512 x i8]
%18 = alloca i32
%19 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %9
store i32 %1, i32* %10
store i8* %2, i8** %11
store i32 %3, i32* %12
store i8* %4, i8** %13
store i32 %5, i32* %14
store i32 %6, i32* %15
%20 = load %struct.sockaddr*, %struct.sockaddr** %9
%21 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %20, i32 0, i32 0
%22 = load i32, i32* %21
store i32 %22, i32* %18
%23 = load i32, i32* %18
switch i32 %23, label %46 [
i32 129, label %24
i32 128, label %35
]
24: ; preds = %7
%25 = load %struct.sockaddr*, %struct.sockaddr** %9
%26 = bitcast %struct.sockaddr* %25 to %struct.sockaddr_in*
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 1
%28 = bitcast i32* %27 to i8*
store i8* %28, i8** %19
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = icmp ne i64 %30, 8
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = load i32, i32* @EAI_FAMILY
store i32 %33, i32* %8
br label %116
34: ; preds = %24
br label %48
35: ; preds = %7
%36 = load %struct.sockaddr*, %struct.sockaddr** %9
%37 = bitcast %struct.sockaddr* %36 to %struct.sockaddr_in6*
%38 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %37, i32 0, i32 0
%39 = bitcast i32* %38 to i8*
store i8* %39, i8** %19
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = icmp ne i64 %41, 4
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = load i32, i32* @EAI_FAMILY
store i32 %44, i32* %8
br label %116
45: ; preds = %35
br label %48
46: ; preds = %7
%47 = load i32, i32* @EAI_FAMILY
store i32 %47, i32* %8
br label %116
48: ; preds = %45, %34
%49 = load i8*, i8** %11
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %93
51: ; preds = %48
%52 = load i32, i32* %12
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %93
54: ; preds = %51
%55 = load i32, i32* %15
%56 = load i32, i32* @NI_NUMERICHOST
%57 = and i32 %55, %56
%58 = icmp ne i32 %57, 0
br i1 %58, label %70, label %59
59: ; preds = %54
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%61 = load i8*, i8** %19
%62 = load i32, i32* %18
%63 = call i64 @__dns_query(i8* %60, i8* %61, i32 %62, i32 1)
%64 = icmp sle i64 %63, 0
br i1 %64, label %70, label %65
65: ; preds = %59
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %17, i64 0, i64 0
%68 = call i64 @__dns_get_rr(i8* %66, i32 0, i32 256, i32 1, i8* %67, i32 12, i32 1)
%69 = icmp sle i64 %68, 0
br i1 %69, label %70, label %82
70: ; preds = %65, %59, %54
%71 = load i32, i32* %15
%72 = load i32, i32* @NI_NAMEREQD
%73 = and i32 %71, %72
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %70
%76 = load i32, i32* @EAI_NONAME
store i32 %76, i32* %8
br label %116
77: ; preds = %70
%78 = load i32, i32* %18
%79 = load i8*, i8** %19
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%81 = call i32 @inet_ntop(i32 %78, i8* %79, i8* %80, i32 256)
br label %82
82: ; preds = %77, %65
%83 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%84 = call i32 @strlen(i8* %83)
%85 = load i32, i32* %12
%86 = icmp sge i32 %84, %85
br i1 %86, label %87, label %89
87: ; preds = %82
%88 = load i32, i32* @EAI_OVERFLOW
store i32 %88, i32* %8
br label %116
89: ; preds = %82
%90 = load i8*, i8** %11
%91 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%92 = call i32 @strcpy(i8* %90, i8* %91)
br label %93
93: ; preds = %89, %51, %48
%94 = load i8*, i8** %13
%95 = icmp ne i8* %94, null
br i1 %95, label %96, label %115
96: ; preds = %93
%97 = load i32, i32* %14
%98 = icmp ne i32 %97, 0
br i1 %98, label %99, label %115
99: ; preds = %96
%100 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%101 = load %struct.sockaddr*, %struct.sockaddr** %9
%102 = bitcast %struct.sockaddr* %101 to %struct.sockaddr_in*
%103 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %102, i32 0, i32 0
%104 = load i32, i32* %103
%105 = call i32 @ntohs(i32 %104)
%106 = call i32 @snprintf(i8* %100, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %105)
%107 = load i32, i32* %14
%108 = icmp sge i32 %106, %107
br i1 %108, label %109, label %111
109: ; preds = %99
%110 = load i32, i32* @EAI_OVERFLOW
store i32 %110, i32* %8
br label %116
111: ; preds = %99
%112 = load i8*, i8** %13
%113 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%114 = call i32 @strcpy(i8* %112, i8* %113)
br label %115
115: ; preds = %111, %96, %93
store i32 0, i32* %8
br label %116
116: ; preds = %115, %109, %87, %75, %46, %43, %32
%117 = load i32, i32* %8
ret i32 %117
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@EAI_FAMILY = global i32 0
@NI_NUMERICHOST = global i32 0
@NI_NAMEREQD = global i32 0
@EAI_NONAME = global i32 0
@EAI_OVERFLOW = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @getnameinfo(%struct.sockaddr* noalias %0, i32 %1, i8* noalias %2, i32 %3, i8* noalias %4, i32 %5, i32 %6) {
%8 = alloca [256 x i8]
%9 = alloca [512 x i8]
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%12 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%13 = load i32, i32* %12
switch i32 %13, label %25 [
i32 129, label %14
i32 128, label %20
]
14: ; preds = %7
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
%16 = bitcast i32* %15 to i8*
%17 = icmp eq i32 %1, 8
br i1 %17, label %27, label %18
18: ; preds = %14
%19 = load i32, i32* @EAI_FAMILY
br label %70
20: ; preds = %7
%21 = bitcast %struct.sockaddr* %0 to i8*
%22 = icmp eq i32 %1, 4
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i32, i32* @EAI_FAMILY
br label %70
25: ; preds = %7
%26 = load i32, i32* @EAI_FAMILY
br label %70
27: ; preds = %20, %14
%28 = phi i8* [ %21, %20 ], [ %16, %14 ]
%29 = icmp ne i8* %2, null
%30 = icmp ne i32 %3, 0
%31 = and i1 %29, %30
br i1 %31, label %32, label %57
32: ; preds = %27
%33 = load i32, i32* @NI_NUMERICHOST
%34 = and i32 %33, %6
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = call i64 @__dns_query(i8* %11, i8* %28, i32 %13, i32 1)
%38 = icmp slt i64 %37, 1
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = call i64 @__dns_get_rr(i8* %10, i32 0, i32 256, i32 1, i8* %11, i32 12, i32 1)
%41 = icmp slt i64 %40, 1
br i1 %41, label %42, label %50
42: ; preds = %39, %36, %32
%43 = load i32, i32* @NI_NAMEREQD
%44 = and i32 %43, %6
%45 = icmp eq i32 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = load i32, i32* @EAI_NONAME
br label %70
48: ; preds = %42
%49 = call i32 @inet_ntop(i32 %13, i8* %28, i8* %10, i32 256)
br label %50
50: ; preds = %48, %39
%51 = call i32 @strlen(i8* %10)
%52 = icmp slt i32 %51, %3
br i1 %52, label %55, label %53
53: ; preds = %50
%54 = load i32, i32* @EAI_OVERFLOW
br label %70
55: ; preds = %50
%56 = call i32 @strcpy(i8* %2, i8* %10)
br label %57
57: ; preds = %55, %27
%58 = icmp ne i8* %4, null
%59 = icmp ne i32 %5, 0
%60 = and i1 %58, %59
br i1 %60, label %61, label %70
61: ; preds = %57
%62 = load i32, i32* %12
%63 = call i32 @ntohs(i32 %62)
%64 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %63)
%65 = icmp slt i32 %64, %5
br i1 %65, label %68, label %66
66: ; preds = %61
%67 = load i32, i32* @EAI_OVERFLOW
br label %70
68: ; preds = %61
%69 = call i32 @strcpy(i8* %4, i8* %10)
br label %70
70: ; preds = %57, %68, %66, %53, %46, %25, %23, %18
%71 = phi i32 [ %26, %25 ], [ %24, %23 ], [ %47, %46 ], [ %54, %53 ], [ %67, %66 ], [ %19, %18 ], [ 0, %68 ], [ 0, %57 ]
ret i32 %71
}