#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 */
typedef int /*<<< orphan*/ SSL;

/* Variables and functions */
int SSL_read(int /*<<< orphan*/ *, char *, int);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
char *receive_message(SSL *ns, int *total_bytes_read) {
  int bytes_read;
  int total_read = 0;
  char *data = (char *)malloc(512);
  char *pos = data;
  while (1) {
    bytes_read = SSL_read(ns, pos, 512);
    if (!bytes_read || ((total_read += bytes_read) >= 50 * 512)) {
      break;
    }
    if (bytes_read != 512) {
      break;
    }
    data = (char *)realloc(data, total_read + 512 + 1);
    pos = data + total_read;
  }
  data[total_read] = '\0';
  *total_bytes_read = total_read;
  return data;
}


x86 assembly (clang, -O0):
<receive_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x200,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x200,%edx
call   1030 <SSL_read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1190 <receive_message+0x60>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmp    $0x6400,%eax
jl     1195 <receive_message+0x65>
jmp    11d4 <receive_message+0xa4>
cmpl   $0x200,-0x14(%rbp)
je     11a7 <receive_message+0x77>
jmp    11d4 <receive_message+0xa4>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x200,%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    115d <receive_message+0x2d>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_message>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x200,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0x200,%edx
call   1030 <SSL_read@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     11a9 <receive_message+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %eax,%ebx
cmp    $0x200,%eax
jne    11a9 <receive_message+0x79>
cmp    $0x63ff,%ebx
jg     11a9 <receive_message+0x79>
lea    0x201(%rbx),%esi
mov    %r12,%rdi
call   1050 <realloc@plt>
mov    %rax,%r12
movslq %ebx,%rsi
add    %rax,%rsi
mov    %r15,%rdi
mov    $0x200,%edx
call   1030 <SSL_read@plt>
test   %eax,%eax
jne    1170 <receive_message+0x40>
movslq %ebx,%rax
movb   $0x0,(%r12,%rax,1)
mov    %eax,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @receive_message(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %6
%9 = call i64 @malloc(i32 512)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %7
%11 = load i8*, i8** %7
store i8* %11, i8** %8
br label %12
12: ; preds = %2, %28
%13 = load i32*, i32** %3
%14 = load i8*, i8** %8
%15 = call i32 @SSL_read(i32* %13, i8* %14, i32 512)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = add nsw i32 %20, %19
store i32 %21, i32* %6
%22 = icmp sge i32 %21, 25600
br i1 %22, label %23, label %24
23: ; preds = %18, %12
br label %39
24: ; preds = %18
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 512
br i1 %26, label %27, label %28
27: ; preds = %24
br label %39
28: ; preds = %24
%29 = load i8*, i8** %7
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 512
%32 = add nsw i32 %31, 1
%33 = call i64 @realloc(i8* %29, i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8* %38, i8** %8
br label %12
39: ; preds = %27, %23
%40 = load i8*, i8** %7
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i32, i32* %6
%45 = load i32*, i32** %4
store i32 %44, i32* %45
%46 = load i8*, i8** %7
ret i8* %46
}

LLVM IR (-O2):
define i8* @receive_message(i32* %0, i32* nocapture writeonly %1) {
%3 = tail call i64 @malloc(i32 512)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @SSL_read(i32* %0, i8* %4, i32 512)
%6 = icmp eq i32 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %2, %15
%8 = phi i32 [ %21, %15 ], [ %5, %2 ]
%9 = phi i8* [ %18, %15 ], [ %4, %2 ]
%10 = phi i32 [ %11, %15 ], [ 0, %2 ]
%11 = add nsw i32 %8, %10
%12 = icmp sgt i32 %11, 25599
%13 = icmp ne i32 %8, 512
%14 = or i1 %13, %12
br i1 %14, label %25, label %15
15: ; preds = %7
%16 = add nsw i32 %11, 513
%17 = tail call i64 @realloc(i8* %9, i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = sext i32 %11 to i64
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = tail call i32 @SSL_read(i32* %0, i8* %20, i32 512)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %7
23: ; preds = %15
%24 = inttoptr i64 %17 to i8*
br label %25
25: ; preds = %7, %23, %2
%26 = phi i8* [ %4, %2 ], [ %24, %23 ], [ %9, %7 ]
%27 = phi i32 [ 0, %2 ], [ %11, %23 ], [ %11, %7 ]
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
store i32 %27, i32* %1
ret i8* %26
}