#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Die(char *);
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ nslkup(char const *);
int /*<<< orphan*/ perror(char *);
scalar_t__ realloc(char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char const *, scalar_t__, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char const *);
char *send_to(const char *host, int port, const char *data) {
  int sock;
  struct sockaddr_in server;
  if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    Die("Failed to create socket");
  }
  memset(&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = inet_addr(nslkup(host));
  server.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
    Die("Failed to connect with server");
  }
  ssize_t datalen = strlen(data);
  if (send(sock, data, datalen, 0) != datalen) {
    Die("Mismatch in number of sent bytes");
  }
  char *buffer = (char *)malloc(1024 * 1024 * 8 * sizeof(char));
  if (!buffer)
    Die("malloc failed!");
  char *ptr = buffer;
  int received = 0, bytes = -1, current_size = 1024 * 1024 * 8;
  if ((bytes = recv(sock, ptr, current_size - received, 0)) > 0) {
    received += bytes;
    ptr += bytes;
    if (received >= current_size) {
      buffer = (char *)realloc(buffer, current_size * 2);
    }
  } else if (bytes < 0) {
    perror("recv() returns neg number!\n");
    exit(-1);
  } else if (bytes == 0) {
  }
  buffer[received] = '\0';
  close(sock);
  return buffer;
}


x86 assembly (clang, -O0):
<send_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    123b <send_to+0x4b>
lea    0xdca(%rip),%rdi
call   10c0 <Die@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <nslkup@plt>
mov    %eax,%edi
call   1090 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0x0,%rax
jge    129c <send_to+0xac>
lea    0xd81(%rip),%rdi
call   10c0 <Die@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    -0x30(%rbp),%rax
je     12d1 <send_to+0xe1>
lea    0xd6a(%rip),%rdi
call   10c0 <Die@plt>
mov    $0x800000,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f6 <send_to+0x106>
lea    0xd66(%rip),%rdi
call   10c0 <Die@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0xffffffff,-0x48(%rbp)
movl   $0x800000,-0x4c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x4c(%rbp),%edx
sub    -0x44(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
jle    1371 <send_to+0x181>
mov    -0x48(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jl     136c <send_to+0x17c>
mov    -0x38(%rbp),%rdi
mov    -0x4c(%rbp),%esi
shl    $0x1,%esi
call   10d0 <realloc@plt>
mov    %rax,-0x38(%rbp)
jmp    13aa <send_to+0x1ba>
cmpl   $0x0,-0x48(%rbp)
jge    1391 <send_to+0x1a1>
lea    0xce4(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>
cmpl   $0x0,-0x48(%rbp)
jne    13a0 <send_to+0x1b0>
jmp    13a0 <send_to+0x1b0>
jmp    13a5 <send_to+0x1b5>
jmp    13aa <send_to+0x1ba>
mov    -0x38(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_to>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1238 <send_to+0x48>
lea    0xdcd(%rip),%rdi
call   10c0 <Die@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
call   10a0 <nslkup@plt>
mov    %eax,%edi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
test   %rax,%rax
jns    1296 <send_to+0xa6>
lea    0xd87(%rip),%rdi
call   10c0 <Die@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r12d,%edi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    %rbx,%rax
je     12c2 <send_to+0xd2>
lea    0xd79(%rip),%rdi
call   10c0 <Die@plt>
mov    $0x800000,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12e0 <send_to+0xf0>
lea    0xd7c(%rip),%rdi
call   10c0 <Die@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x800000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1312 <send_to+0x122>
cmp    $0x800000,%ebp
jb     1314 <send_to+0x124>
mov    %rbx,%rdi
mov    $0x1000000,%esi
call   10d0 <realloc@plt>
mov    %rax,%rbx
jmp    1314 <send_to+0x124>
js     1332 <send_to+0x142>
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r12d,%edi
call   1080 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2d(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@AF_INET = global i32 0
@.str.1 = [30 x i8] c"Failed to connect with server\00"
@.str.2 = [33 x i8] c"Mismatch in number of sent bytes\00"
@.str.3 = [15 x i8] c"malloc failed!\00"
@.str.4 = [28 x i8] c"recv() returns neg number!\0A\00"
define i8* @send_to(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = load i32, i32* @IPPROTO_TCP
%18 = call i32 @socket(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @Die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %3
%23 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i8*, i8** %4
%27 = call i32 @nslkup(i8* %26)
%28 = call i32 @inet_addr(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %5
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %7
%35 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%36 = call i64 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %22
%39 = call i32 @Die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %22
%41 = load i8*, i8** %6
%42 = call i64 @strlen(i8* %41)
store i64 %42, i64* %9
%43 = load i32, i32* %7
%44 = load i8*, i8** %6
%45 = load i64, i64* %9
%46 = call i64 @send(i32 %43, i8* %44, i64 %45, i32 0)
%47 = load i64, i64* %9
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %40
%50 = call i32 @Die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %40
%52 = call i64 @malloc(i32 8388608)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %10
%54 = load i8*, i8** %10
%55 = icmp ne i8* %54, null
br i1 %55, label %58, label %56
56: ; preds = %51
%57 = call i32 @Die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %51
%59 = load i8*, i8** %10
store i8* %59, i8** %11
store i32 0, i32* %12
store i32 -1, i32* %13
store i32 8388608, i32* %14
%60 = load i32, i32* %7
%61 = load i8*, i8** %11
%62 = load i32, i32* %14
%63 = load i32, i32* %12
%64 = sub nsw i32 %62, %63
%65 = call i32 @recv(i32 %60, i8* %61, i32 %64, i32 0)
store i32 %65, i32* %13
%66 = icmp sgt i32 %65, 0
br i1 %66, label %67, label %85
67: ; preds = %58
%68 = load i32, i32* %13
%69 = load i32, i32* %12
%70 = add nsw i32 %69, %68
store i32 %70, i32* %12
%71 = load i32, i32* %13
%72 = load i8*, i8** %11
%73 = sext i32 %71 to i64
%74 = getelementptr inbounds i8, i8* %72, i64 %73
store i8* %74, i8** %11
%75 = load i32, i32* %12
%76 = load i32, i32* %14
%77 = icmp sge i32 %75, %76
br i1 %77, label %78, label %84
78: ; preds = %67
%79 = load i8*, i8** %10
%80 = load i32, i32* %14
%81 = mul nsw i32 %80, 2
%82 = call i64 @realloc(i8* %79, i32 %81)
%83 = inttoptr i64 %82 to i8*
store i8* %83, i8** %10
br label %84
84: ; preds = %78, %67
br label %97
85: ; preds = %58
%86 = load i32, i32* %13
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%90 = call i32 @exit(i32 -1)
unreachable
91: ; preds = %85
%92 = load i32, i32* %13
%93 = icmp eq i32 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %91
br label %95
95: ; preds = %94, %91
br label %96
96: ; preds = %95
br label %97
97: ; preds = %96, %84
%98 = load i8*, i8** %10
%99 = load i32, i32* %12
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %98, i64 %100
store i8 0, i8* %101
%102 = load i32, i32* %7
%103 = call i32 @close(i32 %102)
%104 = load i8*, i8** %10
ret i8* %104
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@AF_INET = global i32 0
@.str.1 = [30 x i8] c"Failed to connect with server\00"
@.str.2 = [33 x i8] c"Mismatch in number of sent bytes\00"
@.str.3 = [15 x i8] c"malloc failed!\00"
@.str.4 = [28 x i8] c"recv() returns neg number!\0A\00"
define i8* @send_to(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @Die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @nslkup(i8* %0)
%18 = call i32 @inet_addr(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %1)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%23 = call i64 @connect(i32 %9, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = call i32 @Die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %13
%28 = call i64 @strlen(i8* dereferenceable(1) %2)
%29 = call i64 @send(i32 %9, i8* %2, i64 %28, i32 0)
%30 = icmp eq i64 %29, %28
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @Die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = call i64 @malloc(i32 8388608)
%35 = inttoptr i64 %34 to i8*
%36 = icmp eq i64 %34, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @Die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = call i32 @recv(i32 %9, i8* %35, i32 8388608, i32 0)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = icmp ugt i32 %40, 8388607
br i1 %43, label %44, label %52
44: ; preds = %42
%45 = call i64 @realloc(i8* %35, i32 16777216)
%46 = inttoptr i64 %45 to i8*
br label %52
47: ; preds = %39
%48 = icmp slt i32 %40, 0
br i1 %48, label %49, label %52
49: ; preds = %47
%50 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %47, %42, %44
%53 = phi i8* [ %46, %44 ], [ %35, %42 ], [ %35, %47 ]
%54 = zext i32 %40 to i64
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8 0, i8* %55
%56 = call i32 @close(i32 %9)
ret i8* %53
}