#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 /*<<< orphan*/ sa_family;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  struct sockaddr *ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ hints;
typedef int /*<<< orphan*/ h2;
typedef int /*<<< orphan*/ h1;

/* Variables and functions */
int NI_MAXHOST;
int NI_NUMERICHOST;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
scalar_t__ getnameinfo(struct sockaddr *, int /*<<< orphan*/, char *, int,
                       int /*<<< orphan*/ *, int /*<<< orphan*/, int const);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
scalar_t__ strcmp(char *, char *);
int __icheckhost(struct sockaddr *raddr, socklen_t salen, const char *lhost) {
  struct addrinfo hints, *res, *r;
  char h1[NI_MAXHOST], h2[NI_MAXHOST];
  int error;
  const int niflags = NI_NUMERICHOST;
  h1[0] = '\0';
  if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, niflags) != 0)
    return (0);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = raddr->sa_family;
  hints.ai_socktype = SOCK_DGRAM;
  res = NULL;
  error = getaddrinfo(lhost, "0", &hints, &res);
  if (error)
    return (0);
  for (r = res; r; r = r->ai_next) {
    h2[0] = '\0';
    if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), NULL, 0,
                    niflags) != 0)
      continue;
    if (strcmp(h1, h2) == 0) {
      freeaddrinfo(res);
      return (1);
    }
  }
  freeaddrinfo(res);
  return (0);
}


x86 assembly (clang, -O0):
<__icheckhost>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e63(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
mov    %rdx,-0x58(%rbp)
mov    %rax,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x90(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x60(%rbp)
mov    (%rcx),%ecx
mov    %rcx,-0x88(%rbp)
mov    %rcx,%rdi
add    $0xf,%rdi
and    $0xfffffffffffffff0,%rdi
mov    %rsp,%rsi
sub    %rdi,%rsi
mov    %rsi,-0x80(%rbp)
mov    %rsi,%rsp
mov    %rcx,-0x68(%rbp)
mov    0x2e17(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,-0x70(%rbp)
movb   $0x0,(%rdx)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    %eax,%ecx
mov    -0x70(%rbp),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
je     1208 <__icheckhost+0xb8>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1329 <__icheckhost+0x1d9>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdc0(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
je     126d <__icheckhost+0x11d>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1329 <__icheckhost+0x1d9>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1312 <__icheckhost+0x1c2>
mov    -0x80(%rbp),%rdx
mov    -0x88(%rbp),%rax
movb   $0x0,(%rdx)
mov    -0x50(%rbp),%rcx
mov    0x8(%rcx),%rdi
mov    -0x50(%rbp),%rcx
mov    (%rcx),%esi
mov    %eax,%ecx
mov    -0x70(%rbp),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
je     12c6 <__icheckhost+0x176>
jmp    1301 <__icheckhost+0x1b1>
mov    -0x80(%rbp),%rsi
mov    -0x90(%rbp),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12fc <__icheckhost+0x1ac>
mov    -0x48(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1329 <__icheckhost+0x1d9>
jmp    1301 <__icheckhost+0x1b1>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1275 <__icheckhost+0x125>
mov    -0x48(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__icheckhost>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,-0x40(%rbp)
mov    %rsp,-0x48(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rcx,%rbx
add    $0xf,%rbx
and    $0xfffffffffffffff0,%rbx
mov    %rdx,%r15
sub    %rbx,%r15
mov    %r15,%rsp
neg    %rbx
mov    (%rax),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2e31(%rip),%rax
mov    (%rax),%r12d
movb   $0x0,(%rdx,%rbx,1)
sub    $0x8,%rsp
xor    %ebx,%ebx
mov    %rdi,-0x38(%rbp)
mov    %r15,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %r12
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
test   %rax,%rax
jne    1286 <__icheckhost+0x136>
lea    -0x68(%rbp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xdf5(%rip),%rsi
lea    -0x30(%rbp),%rcx
mov    -0x40(%rbp),%rdi
lea    -0x68(%rbp),%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
jne    1286 <__icheckhost+0x136>
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
jne    1239 <__icheckhost+0xe9>
xor    %edi,%edi
xor    %ebx,%ebx
jmp    1281 <__icheckhost+0x131>
nop
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     127b <__icheckhost+0x12b>
movb   $0x0,0x0(%r13)
mov    0x8(%rbx),%rdi
mov    (%rbx),%esi
sub    $0x8,%rsp
mov    %r13,%rdx
mov    %r14d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %r12
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
test   %rax,%rax
jne    1230 <__icheckhost+0xe0>
mov    %r15,%rdi
mov    %r13,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1230 <__icheckhost+0xe0>
mov    $0x1,%ebx
jmp    127d <__icheckhost+0x12d>
xor    %ebx,%ebx
mov    -0x30(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x48(%rbp),%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, %struct.sockaddr*, %struct.addrinfo*, i32, i32 }
@NI_MAXHOST = global i32 0
@NI_NUMERICHOST = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [2 x i8] c"0\00"
define i32 @__icheckhost(%struct.sockaddr* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%17 = load i32, i32* @NI_MAXHOST
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %11
%20 = alloca i8, i64 %18
store i64 %18, i64* %12
%21 = load i32, i32* @NI_MAXHOST
%22 = zext i32 %21 to i64
%23 = alloca i8, i64 %22
store i64 %22, i64* %13
%24 = load i32, i32* @NI_NUMERICHOST
store i32 %24, i32* %15
%25 = getelementptr inbounds i8, i8* %20, i64 0
store i8 0, i8* %25
%26 = load %struct.sockaddr*, %struct.sockaddr** %5
%27 = load i32, i32* %6
%28 = trunc i64 %18 to i32
%29 = load i32, i32* %15
%30 = call i64 @getnameinfo(%struct.sockaddr* %26, i32 %27, i8* %20, i32 %28, i32* null, i32 0, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %3
store i32 0, i32* %4
store i32 1, i32* %16
br label %78
33: ; preds = %3
%34 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%35 = load %struct.sockaddr*, %struct.sockaddr** %5
%36 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %37, i32* %38
%39 = load i32, i32* @SOCK_DGRAM
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %39, i32* %40
store %struct.addrinfo* null, %struct.addrinfo** %9
%41 = load i8*, i8** %7
%42 = call i32 @getaddrinfo(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %42, i32* %14
%43 = load i32, i32* %14
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %33
store i32 0, i32* %4
store i32 1, i32* %16
br label %78
46: ; preds = %33
%47 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %47, %struct.addrinfo** %10
br label %48
48: ; preds = %71, %46
%49 = load %struct.addrinfo*, %struct.addrinfo** %10
%50 = icmp ne %struct.addrinfo* %49, null
br i1 %50, label %51, label %75
51: ; preds = %48
%52 = getelementptr inbounds i8, i8* %23, i64 0
store i8 0, i8* %52
%53 = load %struct.addrinfo*, %struct.addrinfo** %10
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load %struct.sockaddr*, %struct.sockaddr** %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %10
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = trunc i64 %22 to i32
%60 = load i32, i32* %15
%61 = call i64 @getnameinfo(%struct.sockaddr* %55, i32 %58, i8* %23, i32 %59, i32* null, i32 0, i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %51
br label %71
64: ; preds = %51
%65 = call i64 @strcmp(i8* %20, i8* %23)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load %struct.addrinfo*, %struct.addrinfo** %9
%69 = call i32 @freeaddrinfo(%struct.addrinfo* %68)
store i32 1, i32* %4
store i32 1, i32* %16
br label %78
70: ; preds = %64
br label %71
71: ; preds = %70, %63
%72 = load %struct.addrinfo*, %struct.addrinfo** %10
%73 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %72, i32 0, i32 2
%74 = load %struct.addrinfo*, %struct.addrinfo** %73
store %struct.addrinfo* %74, %struct.addrinfo** %10
br label %48
75: ; preds = %48
%76 = load %struct.addrinfo*, %struct.addrinfo** %9
%77 = call i32 @freeaddrinfo(%struct.addrinfo* %76)
store i32 0, i32* %4
store i32 1, i32* %16
br label %78
78: ; preds = %75, %67, %45, %32
%79 = load i8*, i8** %11
%80 = load i32, i32* %4
ret i32 %80
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, %struct.sockaddr*, %struct.addrinfo*, i32, i32 }
@NI_MAXHOST = global i32 0
@NI_NUMERICHOST = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [2 x i8] c"0\00"
define i32 @__icheckhost(%struct.sockaddr* %0, i32 %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = load i32, i32* @NI_MAXHOST
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = load i32, i32* @NI_MAXHOST
%13 = zext i32 %12 to i64
%14 = alloca i8, i64 %13
%15 = load i32, i32* @NI_NUMERICHOST
store i8 0, i8* %11
%16 = call i64 @getnameinfo(%struct.sockaddr* %0, i32 %1, i8* %11, i32 %8, i32* null, i32 0, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %53
18: ; preds = %3
%19 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%20 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %21, i32* %22
%23 = load i32, i32* @SOCK_DGRAM
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %23, i32* %24
store %struct.addrinfo* null, %struct.addrinfo** %5
%25 = call i32 @getaddrinfo(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %5)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %53
27: ; preds = %18
%28 = load %struct.addrinfo*, %struct.addrinfo** %5
%29 = icmp eq %struct.addrinfo* %28, null
br i1 %29, label %49, label %30
30: ; preds = %27, %43
%31 = phi %struct.addrinfo* [ %45, %43 ], [ %28, %27 ]
store i8 0, i8* %14
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 1
%33 = load %struct.sockaddr*, %struct.sockaddr** %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @getnameinfo(%struct.sockaddr* %33, i32 %35, i8* %14, i32 %12, i32* null, i32 0, i32 %15)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %30
%39 = call i64 @strcmp(i8* %11, i8* %14)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load %struct.addrinfo*, %struct.addrinfo** %5
br label %49
43: ; preds = %38, %30
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 2
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %47, label %30
47: ; preds = %43
%48 = load %struct.addrinfo*, %struct.addrinfo** %5
br label %49
49: ; preds = %27, %47, %41
%50 = phi %struct.addrinfo* [ %42, %41 ], [ %48, %47 ], [ null, %27 ]
%51 = phi i32 [ 1, %41 ], [ 0, %47 ], [ 0, %27 ]
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
br label %53
53: ; preds = %49, %18, %3
%54 = phi i32 [ 0, %3 ], [ 0, %18 ], [ %51, %49 ]
ret i32 %54
}