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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int ntohl(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int recv(int, ...);
int recstr(int sockfd, char **str) {
  ssize_t numbytes = 0;
  int total = 0, rv = 0, size;
  recv(sockfd, &size, sizeof(size), 0);
  size = ntohl(size);
  printf("recstr received string size: %d\n", size);
  char *s = malloc(size);
  if (!s) {
    perror("recstr (malloc)");
    goto exit;
  }
  do {
    numbytes = recv(sockfd, s, size - total, 0);
    if (numbytes == -1) {
      perror("recstr");
      goto memory_cleanup;
    } else if (numbytes == 0) {
      break;
    }
    total += numbytes;
  } while (total < size);
  rv = total;
  *str = s;
  goto exit;
memory_cleanup:
  free(s);
exit:
  return rv;
}


x86 assembly (clang, -O0):
<recstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <recv@plt>
mov    -0x20(%rbp),%edi
call   1080 <ntohl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11db <recstr+0x7b>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
jmp    125c <recstr+0xfc>
jmp    11e0 <recstr+0x80>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <recv@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1214 <recstr+0xb4>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
jmp    1253 <recstr+0xf3>
cmpl   $0x0,-0x14(%rbp)
jne    1223 <recstr+0xc3>
jmp    123d <recstr+0xdd>
jmp    1228 <recstr+0xc8>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jl     11e0 <recstr+0x80>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    125c <recstr+0xfc>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <recv@plt>
mov    0xc(%rsp),%edi
call   1080 <ntohl@plt>
mov    %eax,0xc(%rsp)
lea    0xe67(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f8 <recstr+0x98>
mov    %rax,%r12
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
sub    %ebp,%ebx
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <recv@plt>
test   %eax,%eax
je     11e6 <recstr+0x86>
cmp    $0xffffffff,%eax
je     1208 <recstr+0xa8>
add    %eax,%ebp
mov    0xc(%rsp),%ebx
cmp    %ebx,%ebp
jl     11c0 <recstr+0x60>
mov    %r12,(%r14)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
xor    %ebp,%ebp
jmp    11e9 <recstr+0x89>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    11e9 <recstr+0x89>

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

LLVM IR (-O2):
@.str = [33 x i8] c"recstr received string size: %d\0A\00"
@.str.1 = [16 x i8] c"recstr (malloc)\00"
@.str.2 = [7 x i8] c"recstr\00"
define i32 @recstr(i32 %0, i8** nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 (i32, ...) @recv(i32 %0, i32* %3, i64 4, i32 0)
%6 = load i32, i32* %3
%7 = call i32 @ntohl(i32 %6)
store i32 %7, i32* %3
%8 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %3
%10 = call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %28
14: ; preds = %2, %22
%15 = phi i32 [ %24, %22 ], [ %9, %2 ]
%16 = phi i32 [ %23, %22 ], [ 0, %2 ]
%17 = sub nsw i32 %15, %16
%18 = call i32 (i32, ...) @recv(i32 %0, i8* %10, i32 %17, i32 0)
switch i32 %18, label %22 [
i32 -1, label %19
i32 0, label %26
]
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @free(i8* %10)
br label %28
22: ; preds = %14
%23 = add nsw i32 %18, %16
%24 = load i32, i32* %3
%25 = icmp slt i32 %23, %24
br i1 %25, label %14, label %26
26: ; preds = %14, %22
%27 = phi i32 [ %23, %22 ], [ %16, %14 ]
store i8* %10, i8** %1
br label %28
28: ; preds = %19, %26, %12
%29 = phi i32 [ 0, %19 ], [ %27, %26 ], [ 0, %12 ]
ret i32 %29
}