#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*/ SHUT_WR;
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int, ...);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
char *receiveMsgFromServer(int sockFD) {
  int numPacketsToReceive = 0;
  int n = read(sockFD, &numPacketsToReceive, sizeof(int));
  if (n <= 0) {
    shutdown(sockFD, SHUT_WR);
    return NULL;
  }
  char *str = (char *)malloc(numPacketsToReceive * 512);
  memset(str, 0, numPacketsToReceive * 512);
  char *str_p = str;
  int i;
  for (i = 0; i < numPacketsToReceive; ++i) {
    int n = read(sockFD, str, 512);
    str = str + 512;
  }
  return str_p;
}


x86 assembly (clang, -O0):
<receiveMsgFromServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1190 <receiveMsgFromServer+0x50>
mov    -0xc(%rbp),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
movq   $0x0,-0x8(%rbp)
jmp    1205 <receiveMsgFromServer+0xc5>
mov    -0x10(%rbp),%edi
shl    $0x9,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%edx
shl    $0x9,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11fd <receiveMsgFromServer+0xbd>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x200,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11bf <receiveMsgFromServer+0x7f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receiveMsgFromServer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <read@plt>
test   %eax,%eax
jle    11c4 <receiveMsgFromServer+0x84>
mov    0x4(%rsp),%edi
shl    $0x9,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edx
shl    $0x9,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
cmpl   $0x0,0x4(%rsp)
jle    11d8 <receiveMsgFromServer+0x98>
xor    %ebp,%ebp
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %eax,%eax
call   1050 <read@plt>
add    $0x200,%rbx
add    $0x1,%ebp
cmp    0x4(%rsp),%ebp
jl     11a0 <receiveMsgFromServer+0x60>
jmp    11d8 <receiveMsgFromServer+0x98>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1030 <shutdown@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@SHUT_WR = global i32 0
define i8* @receiveMsgFromServer(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = call i32 (i32, ...) @read(i32 %0, i32* %2, i64 4)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @SHUT_WR
%8 = call i32 @shutdown(i32 %0, i32 %7)
br label %27
9: ; preds = %1
%10 = load i32, i32* %2
%11 = shl nsw i32 %10, 9
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = load i32, i32* %2
%15 = shl nsw i32 %14, 9
%16 = call i32 @memset(i8* %13, i32 0, i32 %15)
%17 = load i32, i32* %2
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %9, %19
%20 = phi i32 [ %24, %19 ], [ 0, %9 ]
%21 = phi i8* [ %23, %19 ], [ %13, %9 ]
%22 = call i32 (i32, ...) @read(i32 %0, i8* %21, i32 512)
%23 = getelementptr inbounds i8, i8* %21, i64 512
%24 = add nuw nsw i32 %20, 1
%25 = load i32, i32* %2
%26 = icmp slt i32 %24, %25
br i1 %26, label %19, label %27
27: ; preds = %19, %9, %6
%28 = phi i8* [ null, %6 ], [ %13, %9 ], [ %13, %19 ]
ret i8* %28
}