#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 protoent {
  int /*<<< orphan*/ p_proto;
};
struct addrinfo {
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
bool checkaddr(char *ip, char *port, struct addrinfo **results) {
  struct addrinfo hints;
  struct protoent *protocol;
  int res;
  if ((protocol = getprotobyname("TCP")) == NULL) {
    fprintf(stderr, "Error: could not lookup protocol %s\n", "TCP");
    return true;
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = protocol->p_proto;
  if ((res = getaddrinfo(ip, port, &hints, results))) {
    fprintf(stderr, "Error: %s\n", gai_strerror(res));
    return true;
  }
  return false;
}


x86 assembly (clang, -O0):
<checkaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe95(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11a6 <checkaddr+0x56>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
lea    0xe6b(%rip),%rdx
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1231 <checkaddr+0xe1>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rdx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     122a <checkaddr+0xda>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xe10(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1231 <checkaddr+0xe1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkaddr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe92(%rip),%rdi
call   1060 <getprotobyname@plt>
test   %rax,%rax
je     11e4 <checkaddr+0x94>
mov    %rax,%rbx
lea    0x8(%rsp),%r13
xor    %ebp,%ebp
mov    %r13,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     1205 <checkaddr+0xb5>
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe4c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
jmp    11fb <checkaddr+0xab>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
lea    0xe05(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
%struct.protoent = type { i32 }
@.str = [4 x i8] c"TCP\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Error: could not lookup protocol %s\0A\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [11 x i8] c"Error: %s\0A\00"
define i32 @checkaddr(i8* %0, i8* %1, %struct.addrinfo** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.addrinfo**
%8 = alloca %struct.addrinfo
%9 = alloca %struct.protoent*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.addrinfo** %2, %struct.addrinfo*** %7
%11 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.protoent* %11, %struct.protoent** %9
%12 = icmp eq %struct.protoent* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %37
16: ; preds = %3
%17 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 12)
%18 = load i32, i32* @AF_UNSPEC
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @SOCK_STREAM
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i32 %20, i32* %21
%22 = load %struct.protoent*, %struct.protoent** %9
%23 = getelementptr inbounds %struct.protoent, %struct.protoent* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i8*, i8** %5
%27 = load i8*, i8** %6
%28 = load %struct.addrinfo**, %struct.addrinfo*** %7
%29 = call i32 @getaddrinfo(i8* %26, i8* %27, %struct.addrinfo* %8, %struct.addrinfo** %28)
store i32 %29, i32* %10
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %16
%32 = load i32, i32* @stderr
%33 = load i32, i32* %10
%34 = call i8* @gai_strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 1, i32* %4
br label %37
36: ; preds = %16
store i32 0, i32* %4
br label %37
37: ; preds = %36, %31, %13
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
%struct.protoent = type { i32 }
@.str = [4 x i8] c"TCP\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Error: could not lookup protocol %s\0A\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [11 x i8] c"Error: %s\0A\00"
define i32 @checkaddr(i8* %0, i8* %1, %struct.addrinfo** %2) {
%4 = alloca %struct.addrinfo
%5 = bitcast %struct.addrinfo* %4 to i8*
%6 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq %struct.protoent* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %26
11: ; preds = %3
%12 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 12)
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.protoent, %struct.protoent* %6, i64 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %18, i32* %19
%20 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %11
%23 = load i32, i32* @stderr
%24 = call i8* @gai_strerror(i32 %20)
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %11, %22, %8
%27 = phi i32 [ 1, %8 ], [ 1, %22 ], [ 0, %11 ]
ret i32 %27
}