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

/* Variables and functions */
scalar_t__ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int recv(int, char **, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int receiveFileInChunks(int CSock, FILE *output, int datalen) {
  char buffer[1024 + 1];
  int bytecount = 0;
  int recvcount = 1;
  0 && fprintf(stderr, "DBG: Expecting %d bytes\n", datalen);
  while (bytecount < datalen && recvcount != 0) {
    memset(buffer, 0, sizeof(buffer));
    recvcount = recv(CSock, &buffer, 1024, 0);
    bytecount += recvcount;
    0 && fprintf(stderr, "Library received a %d byte data chunk |%s|\n",
                 recvcount, buffer);
    0 && fprintf(stderr, "**** bytecount is %d, expecting %d\n", bytecount,
                 datalen);
    fprintf(output, "%s", buffer);
  }
  0 && fprintf(stderr, "Returning\n");
  return (bytecount);
}


x86 assembly (clang, -O0):
<receiveFileInChunks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x424(%rbp)
movl   $0x1,-0x428(%rbp)
mov    -0x424(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x429(%rbp)
jge    1180 <receiveFileInChunks+0x50>
cmpl   $0x0,-0x428(%rbp)
setne  %al
mov    %al,-0x429(%rbp)
mov    -0x429(%rbp),%al
test   $0x1,%al
jne    1193 <receiveFileInChunks+0x63>
jmp    11f4 <receiveFileInChunks+0xc4>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x401,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%eax
add    -0x424(%rbp),%eax
mov    %eax,-0x424(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
lea    -0x420(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1159 <receiveFileInChunks+0x29>
mov    -0x424(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receiveFileInChunks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
test   %edx,%edx
jle    11a6 <receiveFileInChunks+0x76>
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsp,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x401,%edx
call   1040 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r13d
add    %eax,%ebp
mov    %r14d,%edi
lea    0xe70(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
cmp    %r12d,%ebp
jge    11a8 <receiveFileInChunks+0x78>
test   %r13d,%r13d
jne    1160 <receiveFileInChunks+0x30>
jmp    11a8 <receiveFileInChunks+0x78>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
define i32 @receiveFileInChunks(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [1025 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %8
store i32 1, i32* %9
br label %10
10: ; preds = %19, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* %9
%16 = icmp ne i32 %15, 0
br label %17
17: ; preds = %14, %10
%18 = phi i1 [ false, %10 ], [ %16, %14 ]
br i1 %18, label %19, label %32
19: ; preds = %17
%20 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 1025)
%22 = load i32, i32* %4
%23 = bitcast [1025 x i8]* %7 to i8**
%24 = call i32 @recv(i32 %22, i8** %23, i32 1024, i32 0)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = load i32, i32* %8
%27 = add nsw i32 %26, %25
store i32 %27, i32* %8
%28 = load i32*, i32** %5
%29 = ptrtoint i32* %28 to i32
%30 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%31 = call i64 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %30)
br label %10
32: ; preds = %17
%33 = load i32, i32* %8
ret i32 %33
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
define i32 @receiveFileInChunks(i32 %0, i32* %1, i32 %2) {
%4 = alloca [1025 x i8]
%5 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %20
7: ; preds = %3
%8 = bitcast [1025 x i8]* %4 to i8**
%9 = ptrtoint i32* %1 to i64
%10 = trunc i64 %9 to i32
br label %11
11: ; preds = %7, %11
%12 = phi i32 [ 0, %7 ], [ %15, %11 ]
%13 = call i32 @memset(i8* %5, i32 0, i32 1025)
%14 = call i32 @recv(i32 %0, i8** %8, i32 1024, i32 0)
%15 = add nsw i32 %14, %12
%16 = call i64 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %5)
%17 = icmp slt i32 %15, %2
%18 = icmp ne i32 %14, 0
%19 = and i1 %17, %18
br i1 %19, label %11, label %20
20: ; preds = %11, %3
%21 = phi i32 [ 0, %3 ], [ %15, %11 ]
ret i32 %21
}