#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strlen(char *);
int whois_query(char *server, char *query, char **response) {
  char message[100], buffer[1500];
  int read_size, total_size = 0;
  int error;
  int querySocket;
  struct addrinfo hints, *res;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  error = getaddrinfo(server, "43", &hints, &res);
  if (error) {
    return error;
  }
  querySocket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (connect(querySocket, res->ai_addr, res->ai_addrlen) < 0) {
    perror("connect failed when trying to do whois query");
  }
  sprintf(message, "%s\r\n", query);
  if (send(querySocket, message, strlen(message), 0) < 0) {
    perror("send failed");
  }
  while ((read_size = recv(querySocket, buffer, sizeof(buffer), 0))) {
    *response = realloc(*response, read_size + total_size);
    if (*response == NULL) {
      printf("realloc failed");
    }
    memcpy(*response + total_size, buffer, read_size);
    total_size += read_size;
  }
  *response = realloc(*response, total_size + 1);
  *(*response + total_size) = '\0';
  close(querySocket);
  return 0;
}


x86 assembly (clang, -O0):
<whois_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x6b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x678(%rbp)
lea    -0x698(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x688(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdd3(%rip),%rsi
lea    -0x698(%rbp),%rdx
lea    -0x6a0(%rbp),%rcx
call   10e0 <getaddrinfo@plt>
mov    %eax,-0x67c(%rbp)
cmpl   $0x0,-0x67c(%rbp)
je     1261 <whois_query+0x91>
mov    -0x67c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    140d <whois_query+0x23d>
mov    -0x6a0(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x6a0(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x6a0(%rbp),%rax
mov    0x8(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x680(%rbp)
mov    -0x680(%rbp),%edi
mov    -0x6a0(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x6a0(%rbp),%rax
mov    (%rax),%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    12be <whois_query+0xee>
lea    0xd4a(%rip),%rdi
call   10b0 <perror@plt>
lea    -0x90(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd60(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x680(%rbp),%eax
mov    %eax,-0x6ac(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x6a8(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6ac(%rbp),%edi
mov    -0x6a8(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0x0,%rax
jge    1327 <whois_query+0x157>
lea    0xd13(%rip),%rdi
call   10b0 <perror@plt>
jmp    132c <whois_query+0x15c>
mov    -0x680(%rbp),%edi
lea    -0x670(%rbp),%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x674(%rbp)
cmp    $0x0,%eax
je     13ca <whois_query+0x1fa>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x674(%rbp),%esi
add    -0x678(%rbp),%esi
call   10a0 <realloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1390 <whois_query+0x1c0>
lea    0xcb6(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
movslq -0x678(%rbp),%rax
add    %rax,%rdi
lea    -0x670(%rbp),%rsi
mov    -0x674(%rbp),%edx
call   1090 <memcpy@plt>
mov    -0x674(%rbp),%eax
add    -0x678(%rbp),%eax
mov    %eax,-0x678(%rbp)
jmp    132c <whois_query+0x15c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x678(%rbp),%esi
add    $0x1,%esi
call   10a0 <realloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movslq -0x678(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x680(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x6b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<whois_query>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x678,%rsp
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xde1(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %rbp,%rdx
call   10e0 <getaddrinfo@plt>
test   %eax,%eax
jne    1348 <whois_query+0x178>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10f0 <socket@plt>
mov    %eax,%r14d
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r14d,%edi
call   10d0 <connect@plt>
test   %rax,%rax
js     135a <whois_query+0x18a>
lea    0xdc5(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rdx
call   10c0 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
xor    %r15d,%r15d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %rax,%rax
js     136b <whois_query+0x19b>
lea    0x90(%rsp),%rsi
mov    %r14d,%edi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     1326 <whois_query+0x156>
mov    %eax,%ebx
xor    %ebp,%ebp
lea    0x90(%rsp),%r13
jmp    12fb <whois_query+0x12b>
nopw   0x0(%rax,%rax,1)
movslq %ebp,%rdi
add    %rax,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1090 <memcpy@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
mov    %r15d,%ebp
test   %eax,%eax
je     1326 <whois_query+0x156>
mov    (%r12),%rdi
lea    (%rbx,%rbp,1),%r15d
mov    %r15d,%esi
call   10a0 <realloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
jne    12d0 <whois_query+0x100>
lea    0xd26(%rip),%rdi
call   1060 <printf@plt>
mov    (%r12),%rax
jmp    12d0 <whois_query+0x100>
mov    (%r12),%rdi
lea    0x1(%r15),%esi
call   10a0 <realloc@plt>
mov    %rax,(%r12)
movslq %r15d,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x678,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xca2(%rip),%rdi
call   10b0 <perror@plt>
jmp    1264 <whois_query+0x94>
lea    0xcc3(%rip),%rdi
call   10b0 <perror@plt>
jmp    129e <whois_query+0xce>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"43\00"
@.str.1 = [45 x i8] c"connect failed when trying to do whois query\00"
@.str.2 = [5 x i8] c"%s\0D\0A\00"
@.str.3 = [12 x i8] c"send failed\00"
@.str.4 = [15 x i8] c"realloc failed\00"
define i32 @whois_query(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca [100 x i8]
%9 = alloca [1500 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.addrinfo
%15 = alloca %struct.addrinfo*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i32 0, i32* %11
%16 = call i32 @memset(%struct.addrinfo* %14, i32 0, i32 20)
%17 = load i32, i32* @AF_UNSPEC
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 4
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_STREAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 3
store i32 %19, i32* %20
%21 = load i8*, i8** %5
%22 = call i32 @getaddrinfo(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %14, %struct.addrinfo** %15)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = load i32, i32* %12
store i32 %26, i32* %4
br label %106
27: ; preds = %3
%28 = load %struct.addrinfo*, %struct.addrinfo** %15
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 4
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %15
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 3
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %15
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %30, i32 %33, i32 %36)
store i32 %37, i32* %13
%38 = load i32, i32* %13
%39 = load %struct.addrinfo*, %struct.addrinfo** %15
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = load %struct.addrinfo*, %struct.addrinfo** %15
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @connect(i32 %38, i32 %41, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %27
%48 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %47, %27
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%51 = load i8*, i8** %6
%52 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %51)
%53 = load i32, i32* %13
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%55 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%56 = call i32 @strlen(i8* %55)
%57 = call i64 @send(i32 %53, i8* %54, i32 %56, i32 0)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %49
%60 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %49
br label %62
62: ; preds = %80, %61
%63 = load i32, i32* %13
%64 = getelementptr inbounds [1500 x i8], [1500 x i8]* %9, i64 0, i64 0
%65 = call i32 @recv(i32 %63, i8* %64, i32 1500, i32 0)
store i32 %65, i32* %10
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %92
67: ; preds = %62
%68 = load i8**, i8*** %7
%69 = load i8*, i8** %68
%70 = load i32, i32* %10
%71 = load i32, i32* %11
%72 = add nsw i32 %70, %71
%73 = call i8* @realloc(i8* %69, i32 %72)
%74 = load i8**, i8*** %7
store i8* %73, i8** %74
%75 = load i8**, i8*** %7
%76 = load i8*, i8** %75
%77 = icmp eq i8* %76, null
br i1 %77, label %78, label %80
78: ; preds = %67
%79 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %80
80: ; preds = %78, %67
%81 = load i8**, i8*** %7
%82 = load i8*, i8** %81
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
%86 = getelementptr inbounds [1500 x i8], [1500 x i8]* %9, i64 0, i64 0
%87 = load i32, i32* %10
%88 = call i32 @memcpy(i8* %85, i8* %86, i32 %87)
%89 = load i32, i32* %10
%90 = load i32, i32* %11
%91 = add nsw i32 %90, %89
store i32 %91, i32* %11
br label %62
92: ; preds = %62
%93 = load i8**, i8*** %7
%94 = load i8*, i8** %93
%95 = load i32, i32* %11
%96 = add nsw i32 %95, 1
%97 = call i8* @realloc(i8* %94, i32 %96)
%98 = load i8**, i8*** %7
store i8* %97, i8** %98
%99 = load i8**, i8*** %7
%100 = load i8*, i8** %99
%101 = load i32, i32* %11
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %100, i64 %102
store i8 0, i8* %103
%104 = load i32, i32* %13
%105 = call i32 @close(i32 %104)
store i32 0, i32* %4
br label %106
106: ; preds = %92, %25
%107 = load i32, i32* %4
ret i32 %107
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"43\00"
@.str.1 = [45 x i8] c"connect failed when trying to do whois query\00"
@.str.2 = [5 x i8] c"%s\0D\0A\00"
@.str.3 = [12 x i8] c"send failed\00"
@.str.4 = [15 x i8] c"realloc failed\00"
define i32 @whois_query(i8* %0, i8* %1, i8** nocapture %2) {
%4 = alloca [100 x i8]
%5 = alloca [1500 x i8]
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [1500 x i8], [1500 x i8]* %5, i64 0, i64 0
%10 = bitcast %struct.addrinfo* %6 to i8*
%11 = bitcast %struct.addrinfo** %7 to i8*
%12 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 20)
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 4
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 3
store i32 %15, i32* %16
%17 = call i32 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %6, %struct.addrinfo** %7)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %72
19: ; preds = %3
%20 = load %struct.addrinfo*, %struct.addrinfo** %7
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 3
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 2
%26 = load i32, i32* %25
%27 = call i32 @socket(i32 %22, i32 %24, i32 %26)
%28 = load %struct.addrinfo*, %struct.addrinfo** %7
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @connect(i32 %27, i32 %30, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %19
%36 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %19
%38 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%39 = call i32 @strlen(i8* %8)
%40 = call i64 @send(i32 %27, i8* %8, i32 %39, i32 0)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = call i32 @recv(i32 %27, i8* %9, i32 1500, i32 0)
%46 = icmp eq i32 %45, 0
br i1 %46, label %64, label %47
47: ; preds = %44, %57
%48 = phi i32 [ %62, %57 ], [ %45, %44 ]
%49 = phi i32 [ %51, %57 ], [ 0, %44 ]
%50 = load i8*, i8** %2
%51 = add nsw i32 %48, %49
%52 = call i8* @realloc(i8* %50, i32 %51)
store i8* %52, i8** %2
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%56 = load i8*, i8** %2
br label %57
57: ; preds = %54, %47
%58 = phi i8* [ %56, %54 ], [ %52, %47 ]
%59 = sext i32 %49 to i64
%60 = getelementptr inbounds i8, i8* %58, i64 %59
%61 = call i32 @memcpy(i8* %60, i8* %9, i32 %48)
%62 = call i32 @recv(i32 %27, i8* %9, i32 1500, i32 0)
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %47
64: ; preds = %57, %44
%65 = phi i32 [ 0, %44 ], [ %51, %57 ]
%66 = load i8*, i8** %2
%67 = add nsw i32 %65, 1
%68 = call i8* @realloc(i8* %66, i32 %67)
store i8* %68, i8** %2
%69 = sext i32 %65 to i64
%70 = getelementptr inbounds i8, i8* %68, i64 %69
store i8 0, i8* %70
%71 = call i32 @close(i32 %27)
br label %72
72: ; preds = %3, %64
%73 = phi i32 [ 0, %64 ], [ %17, %3 ]
ret i32 %73
}