#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 iptrie {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ Log(int, char *);
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ process_buffer(struct iptrie *, char *, int, int *,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int recv(int, char *, int, int /*<<< orphan*/);
struct iptrie *amp_asn_fetch_results_direct(int whois_fd,
                                            struct iptrie *result) {
  int bytes;
  char *buffer = NULL;
  int offset;
  int buflen = 1024;
  Log(7, "Fetching ASN results (standalone)");
  buffer = calloc(1, buflen);
  offset = 0;
  while ((bytes = recv(whois_fd, buffer + offset, buflen - offset - 1, 0)) >
         0) {
    offset += bytes;
    buffer[offset] = '\0';
    process_buffer(result, buffer, buflen, &offset, NULL, NULL);
  }
  free(buffer);
  return result;
}


x86 assembly (clang, -O0):
<amp_asn_fetch_results_direct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x400,-0x28(%rbp)
mov    $0x7,%edi
lea    0xe86(%rip),%rsi
call   1070 <Log@plt>
mov    -0x28(%rbp),%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
sub    -0x24(%rbp),%edx
sub    $0x1,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11f7 <amp_asn_fetch_results_direct+0xa7>
mov    -0x14(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
lea    -0x24(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1060 <process_buffer@plt>
jmp    1197 <amp_asn_fetch_results_direct+0x47>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<amp_asn_fetch_results_direct>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xe97(%rip),%rsi
mov    $0x7,%edi
call   1070 <Log@plt>
mov    $0x1,%edi
mov    $0x400,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    %r12d,%edi
mov    %rax,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    11f9 <amp_asn_fetch_results_direct+0xa9>
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %eax,%ebp
mov    %ebp,0xc(%rsp)
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
mov    %r15,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <process_buffer@plt>
movslq 0xc(%rsp),%rsi
mov    $0x3ff,%edx
sub    %esi,%edx
add    %rbx,%rsi
mov    %r12d,%edi
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    11f9 <amp_asn_fetch_results_direct+0xa9>
mov    0xc(%rsp),%ebp
jmp    11b0 <amp_asn_fetch_results_direct+0x60>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iptrie = type { i32 }
@.str = [34 x i8] c"Fetching ASN results (standalone)\00"
define %struct.iptrie* @amp_asn_fetch_results_direct(i32 %0, %struct.iptrie* %1) {
%3 = alloca i32
%4 = alloca %struct.iptrie*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store %struct.iptrie* %1, %struct.iptrie** %4
store i8* null, i8** %6
store i32 1024, i32* %8
%9 = call i32 @Log(i32 7, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %8
%11 = call i8* @calloc(i32 1, i32 %10)
store i8* %11, i8** %6
store i32 0, i32* %7
br label %12
12: ; preds = %24, %2
%13 = load i32, i32* %3
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i32, i32* %8
%19 = load i32, i32* %7
%20 = sub nsw i32 %18, %19
%21 = sub nsw i32 %20, 1
%22 = call i32 @recv(i32 %13, i8* %17, i32 %21, i32 0)
store i32 %22, i32* %5
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %12
%25 = load i32, i32* %5
%26 = load i32, i32* %7
%27 = add nsw i32 %26, %25
store i32 %27, i32* %7
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load %struct.iptrie*, %struct.iptrie** %4
%33 = load i8*, i8** %6
%34 = load i32, i32* %8
%35 = call i32 @process_buffer(%struct.iptrie* %32, i8* %33, i32 %34, i32* %7, i32* null, i32* null)
br label %12
36: ; preds = %12
%37 = load i8*, i8** %6
%38 = call i32 @free(i8* %37)
%39 = load %struct.iptrie*, %struct.iptrie** %4
ret %struct.iptrie* %39
}

LLVM IR (-O2):
%struct.iptrie = type { i32 }
@.str = [34 x i8] c"Fetching ASN results (standalone)\00"
define %struct.iptrie* @amp_asn_fetch_results_direct(i32 %0, %struct.iptrie* returned %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @Log(i32 7, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%6 = tail call dereferenceable_or_null(1024) i8* @calloc(i32 1, i32 1024)
%7 = call i32 @recv(i32 %0, i8* %6, i32 1023, i32 0)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %2, %22
%10 = phi i32 [ %23, %22 ], [ 0, %2 ]
%11 = phi i32 [ %20, %22 ], [ %7, %2 ]
%12 = add nsw i32 %10, %11
store i32 %12, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %6, i64 %13
store i8 0, i8* %14
%15 = call i32 @process_buffer(%struct.iptrie* %1, i8* %6, i32 1024, i32* %3, i32* null, i32* null)
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %6, i64 %17
%19 = sub i32 1023, %16
%20 = call i32 @recv(i32 %0, i8* %18, i32 %19, i32 0)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %9
%23 = load i32, i32* %3
br label %9
24: ; preds = %9, %2
%25 = call i32 @free(i8* %6)
ret %struct.iptrie* %1
}