#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 ssize_t;

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ realloc(char *, int);
int recv(int, char *, int, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
ssize_t socket_recv(int sockfd, char **buffer, int flag) {
  if (!buffer) {
    return 0;
  }
  ssize_t total_read_size = 0;
  ssize_t nread = 0;
  ssize_t buffer_default_size = 1024 * sizeof(char);
  *buffer = (char *)malloc(buffer_default_size + sizeof(char));
  int iteration = 0;
  while (nread >= 0) {
    if (total_read_size > 0) {
      *buffer = (char *)realloc(*buffer, total_read_size + buffer_default_size +
                                             sizeof(char));
    }
    nread = recv(sockfd, *buffer + total_read_size, 1024, flag);
    if (1)
      printf("socket_recv iteration #%d, nread = %zd\n", iteration++, nread);
    total_read_size += nread;
    if (1)
      printf("Read %ld bytes from socket.\nTotal bytes read so far: %ld.\n",
             nread, total_read_size);
    if (1)
      printf("socket_recv buffer (%lu): %s\n\n\n", strlen(*buffer), *buffer);
    if (nread < 1024) {
      break;
    }
  }
  char *str_result = (char *)malloc(total_read_size + sizeof(char));
  strncpy(str_result, *buffer, total_read_size);
  str_result[total_read_size] = '\0';
  if (1)
    printf("End of socket_recv iteration, total_read_size = %ld\n",
           total_read_size);
  if (1)
    printf("socket_recv string output result (%lu): %s\n\n\n",
           strlen(str_result), str_result);
  return total_read_size;
}


x86 assembly (clang, -O0):
<socket_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1189 <socket_recv+0x29>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <socket_recv+0x190>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x400,-0x20(%rbp)
movslq -0x20(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     128a <socket_recv+0x12a>
cmpl   $0x0,-0x18(%rbp)
jle    11f6 <socket_recv+0x96>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
cltq
add    $0x1,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%ecx
mov    $0x400,%edx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edx
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x400,-0x1c(%rbp)
jge    1285 <socket_recv+0x125>
jmp    128a <socket_recv+0x12a>
jmp    11be <socket_recv+0x5e>
movslq -0x18(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x30(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%esi
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%esi
mov    -0x30(%rbp),%rdx
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,0x4(%rsp)
test   %rsi,%rsi
je     1278 <socket_recv+0x118>
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x401,%edi
call   1070 <malloc@plt>
mov    %rax,(%r14)
xor    %ebx,%ebx
xor    %r13d,%r13d
jmp    120d <socket_recv+0xad>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rsi
add    %rax,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
mov    0x4(%rsp),%ecx
call   1030 <recv@plt>
mov    %eax,%r12d
lea    0x1(%r13),%r15d
lea    0xe3c(%rip),%rdi
mov    %r13d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
add    %r12d,%ebx
lea    0xe4e(%rip),%rdi
mov    %r12d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r14),%rdi
call   1050 <strlen@plt>
mov    (%r14),%rdx
lea    0xe6b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%r13d
cmp    $0x3ff,%r12d
jle    122a <socket_recv+0xca>
mov    (%r14),%rax
test   %ebx,%ebx
jle    11a0 <socket_recv+0x40>
lea    0x401(%rbx),%esi
mov    %rax,%rdi
call   1080 <realloc@plt>
mov    %rax,(%r14)
jmp    11a0 <socket_recv+0x40>
movslq %ebx,%r15
lea    0x1(%rbx),%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    (%r14),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x0(%rbp,%r15,1)
lea    0xe31(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0xe4e(%rip),%rdi
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    127a <socket_recv+0x11a>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"socket_recv iteration #%d, nread = %zd\0A\00"
@.str.1 = [59 x i8] c"Read %ld bytes from socket.\0ATotal bytes read so far: %ld.\0A\00"
@.str.2 = [32 x i8] c"socket_recv buffer (%lu): %s\0A\0A\0A\00"
@.str.3 = [53 x i8] c"End of socket_recv iteration, total_read_size = %ld\0A\00"
@.str.4 = [46 x i8] c"socket_recv string output result (%lu): %s\0A\0A\0A\00"
define i32 @socket_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 i32
%12 = alloca i8*
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%13 = load i8**, i8*** %6
%14 = icmp ne i8** %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store i32 0, i32* %4
br label %94
16: ; preds = %3
store i32 0, i32* %8
store i32 0, i32* %9
store i32 1024, i32* %10
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = add i64 %18, 1
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
%23 = load i8**, i8*** %6
store i8* %22, i8** %23
store i32 0, i32* %11
br label %24
24: ; preds = %70, %16
%25 = load i32, i32* %9
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %71
27: ; preds = %24
%28 = load i32, i32* %8
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = load i8**, i8*** %6
%32 = load i8*, i8** %31
%33 = load i32, i32* %8
%34 = load i32, i32* %10
%35 = add nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = add i64 %36, 1
%38 = trunc i64 %37 to i32
%39 = call i64 @realloc(i8* %32, i32 %38)
%40 = inttoptr i64 %39 to i8*
%41 = load i8**, i8*** %6
store i8* %40, i8** %41
br label %42
42: ; preds = %30, %27
%43 = load i32, i32* %5
%44 = load i8**, i8*** %6
%45 = load i8*, i8** %44
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32, i32* %7
%50 = call i32 @recv(i32 %43, i8* %48, i32 1024, i32 %49)
store i32 %50, i32* %9
%51 = load i32, i32* %11
%52 = add nsw i32 %51, 1
store i32 %52, i32* %11
%53 = load i32, i32* %9
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %51, i32 %53)
%55 = load i32, i32* %9
%56 = load i32, i32* %8
%57 = add nsw i32 %56, %55
store i32 %57, i32* %8
%58 = load i32, i32* %9
%59 = load i32, i32* %8
%60 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %58, i32 %59)
%61 = load i8**, i8*** %6
%62 = load i8*, i8** %61
%63 = call i32 @strlen(i8* %62)
%64 = load i8**, i8*** %6
%65 = load i8*, i8** %64
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %63, i8* %65)
%67 = load i32, i32* %9
%68 = icmp slt i32 %67, 1024
br i1 %68, label %69, label %70
69: ; preds = %42
br label %71
70: ; preds = %42
br label %24
71: ; preds = %69, %24
%72 = load i32, i32* %8
%73 = sext i32 %72 to i64
%74 = add i64 %73, 1
%75 = trunc i64 %74 to i32
%76 = call i64 @malloc(i32 %75)
%77 = inttoptr i64 %76 to i8*
store i8* %77, i8** %12
%78 = load i8*, i8** %12
%79 = load i8**, i8*** %6
%80 = load i8*, i8** %79
%81 = load i32, i32* %8
%82 = call i32 @strncpy(i8* %78, i8* %80, i32 %81)
%83 = load i8*, i8** %12
%84 = load i32, i32* %8
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %83, i64 %85
store i8 0, i8* %86
%87 = load i32, i32* %8
%88 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %87)
%89 = load i8*, i8** %12
%90 = call i32 @strlen(i8* %89)
%91 = load i8*, i8** %12
%92 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %90, i8* %91)
%93 = load i32, i32* %8
store i32 %93, i32* %4
br label %94
94: ; preds = %71, %15
%95 = load i32, i32* %4
ret i32 %95
}

LLVM IR (-O2):
@.str = [40 x i8] c"socket_recv iteration #%d, nread = %zd\0A\00"
@.str.1 = [59 x i8] c"Read %ld bytes from socket.\0ATotal bytes read so far: %ld.\0A\00"
@.str.2 = [32 x i8] c"socket_recv buffer (%lu): %s\0A\0A\0A\00"
@.str.3 = [53 x i8] c"End of socket_recv iteration, total_read_size = %ld\0A\00"
@.str.4 = [46 x i8] c"socket_recv string output result (%lu): %s\0A\0A\0A\00"
define i32 @socket_recv(i32 %0, i8** %1, i32 %2) {
%4 = icmp eq i8** %1, null
br i1 %4, label %42, label %5
5: ; preds = %3
%6 = tail call i64 @malloc(i32 1025)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %1
br label %8
8: ; preds = %5, %17
%9 = phi i32 [ 0, %5 ], [ %24, %17 ]
%10 = phi i32 [ 0, %5 ], [ %22, %17 ]
%11 = icmp sgt i32 %9, 0
%12 = load i8*, i8** %1
br i1 %11, label %13, label %17
13: ; preds = %8
%14 = add nuw i32 %9, 1025
%15 = tail call i64 @realloc(i8* %12, i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %1
br label %17
17: ; preds = %8, %13
%18 = phi i8* [ %16, %13 ], [ %12, %8 ]
%19 = sext i32 %9 to i64
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = tail call i32 @recv(i32 %0, i8* %20, i32 1024, i32 %2)
%22 = add nuw nsw i32 %10, 1
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %21)
%24 = add nsw i32 %21, %9
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %24)
%26 = load i8*, i8** %1
%27 = tail call i32 @strlen(i8* %26)
%28 = load i8*, i8** %1
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8* %28)
%30 = icmp sgt i32 %21, 1023
br i1 %30, label %8, label %31
31: ; preds = %17
%32 = sext i32 %24 to i64
%33 = add i32 %24, 1
%34 = tail call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
%36 = load i8*, i8** %1
%37 = tail call i32 @strncpy(i8* %35, i8* %36, i32 %24)
%38 = getelementptr inbounds i8, i8* %35, i64 %32
store i8 0, i8* %38
%39 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%40 = tail call i32 @strlen(i8* %35)
%41 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %40, i8* %35)
br label %42
42: ; preds = %3, %31
%43 = phi i32 [ %24, %31 ], [ 0, %3 ]
ret i32 %43
}