#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 */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int http_get_content_length(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int hls_recv(int sockfd, char *buf_recv, int *total_length) {
  int ret;
  int recvlen = 0;
  int downloadlen = 0;
  char *buf_recv_tmp = NULL;
  buf_recv_tmp = malloc(1024 * 10 + 1);
  if (buf_recv_tmp == NULL) {
    printf("HTTP_Recv: sk_mem_malloc fail\n");
    return -1;
  }
  memset(buf_recv_tmp, 0x0, 1024 * 10 + 1);
  ret = recv(sockfd, buf_recv_tmp, 1024 * 10, 0);
  if (ret <= 0) {
    free(buf_recv_tmp);
    return ret;
  }
  if ((*total_length) == 0) {
    downloadlen = http_get_content_length(buf_recv_tmp);
    if (downloadlen > 0) {
      (*total_length) = downloadlen;
    }
  }
  recvlen += ret;
  memcpy(buf_recv, buf_recv_tmp, recvlen);
  free(buf_recv_tmp);
  return recvlen;
}


x86 assembly (clang, -O0):
<hls_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x2801,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ca <hls_recv+0x5a>
lea    0xe47(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <hls_recv+0xf7>
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x2801,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x2800,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     120e <hls_recv+0x9e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1267 <hls_recv+0xf7>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    123f <hls_recv+0xcf>
mov    -0x30(%rbp),%rdi
call   1090 <http_get_content_length@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    123a <hls_recv+0xca>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    123f <hls_recv+0xcf>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hls_recv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x2801,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11e3 <hls_recv+0x73>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2801,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2800,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11d9 <hls_recv+0x69>
cmpl   $0x0,(%r15)
jne    11cc <hls_recv+0x5c>
mov    %rbx,%rdi
call   1090 <http_get_content_length@plt>
test   %eax,%eax
jle    11cc <hls_recv+0x5c>
mov    %eax,(%r15)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11f4 <hls_recv+0x84>
lea    0xe16(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"HTTP_Recv: sk_mem_malloc fail\0A\00"
define i32 @hls_recv(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 0, i32* %9
store i32 0, i32* %10
store i8* null, i8** %11
%12 = call i8* @malloc(i32 10241)
store i8* %12, i8** %11
%13 = load i8*, i8** %11
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
17: ; preds = %3
%18 = load i8*, i8** %11
%19 = call i32 @memset(i8* %18, i32 0, i32 10241)
%20 = load i32, i32* %5
%21 = load i8*, i8** %11
%22 = call i32 @recv(i32 %20, i8* %21, i32 10240, i32 0)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = load i8*, i8** %11
%27 = call i32 @free(i8* %26)
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %53
29: ; preds = %17
%30 = load i32*, i32** %7
%31 = load i32, i32* %30
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %29
%34 = load i8*, i8** %11
%35 = call i32 @http_get_content_length(i8* %34)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* %10
%40 = load i32*, i32** %7
store i32 %39, i32* %40
br label %41
41: ; preds = %38, %33
br label %42
42: ; preds = %41, %29
%43 = load i32, i32* %8
%44 = load i32, i32* %9
%45 = add nsw i32 %44, %43
store i32 %45, i32* %9
%46 = load i8*, i8** %6
%47 = load i8*, i8** %11
%48 = load i32, i32* %9
%49 = call i32 @memcpy(i8* %46, i8* %47, i32 %48)
%50 = load i8*, i8** %11
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %9
store i32 %52, i32* %4
br label %53
53: ; preds = %42, %25, %15
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@str = [30 x i8] c"HTTP_Recv: sk_mem_malloc fail\00"
define i32 @hls_recv(i32 %0, i8* %1, i32* nocapture %2) {
%4 = tail call dereferenceable_or_null(10241) i8* @malloc(i32 10241)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %24
8: ; preds = %3
%9 = tail call i32 @memset(i8* %4, i32 0, i32 10241)
%10 = tail call i32 @recv(i32 %0, i8* %4, i32 10240, i32 0)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @free(i8* %4)
br label %24
14: ; preds = %8
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i32 @http_get_content_length(i8* %4)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 %18, i32* %2
br label %21
21: ; preds = %17, %20, %14
%22 = tail call i32 @memcpy(i8* %1, i8* %4, i32 %10)
%23 = tail call i32 @free(i8* %4)
br label %24
24: ; preds = %21, %12, %6
%25 = phi i32 [ -1, %6 ], [ %10, %12 ], [ %10, %21 ]
ret i32 %25
}