#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 */
struct pptcapi_connection {
  int dummy;
};

/* Variables and functions */
int pptcapi_doreceive(struct pptcapi_connection *, char *, int, char **);
int pptcapi_receive_chunk(struct pptcapi_connection *connection, char *buffer,
                          int len, char **error) {
  int bytes_read = pptcapi_doreceive(connection, buffer, len, error);
  if (bytes_read < 0) {
    return bytes_read;
  }
  if (bytes_read < len) {
    char *new_buffer = buffer + bytes_read;
    int new_len = len - bytes_read;
    int new_bytes_read =
        pptcapi_receive_chunk(connection, new_buffer, new_len, error);
    if (new_bytes_read < 0) {
      return new_bytes_read;
    } else {
      bytes_read += new_bytes_read;
    }
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<pptcapi_receive_chunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
call   1030 <pptcapi_doreceive@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1163 <pptcapi_receive_chunk+0x43>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <pptcapi_receive_chunk+0xa7>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11c1 <pptcapi_receive_chunk+0xa1>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
mov    -0x28(%rbp),%rcx
call   1040 <pptcapi_receive_chunk@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    11b3 <pptcapi_receive_chunk+0x93>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <pptcapi_receive_chunk+0xa7>
mov    -0x40(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11c1 <pptcapi_receive_chunk+0xa1>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pptcapi_receive_chunk>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <pptcapi_doreceive@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1163 <pptcapi_receive_chunk+0x43>
cmp    %ebp,%ebx
jge    1163 <pptcapi_receive_chunk+0x43>
mov    %ebx,%eax
add    %rax,%r15
sub    %ebx,%ebp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %r14,%rcx
call   1040 <pptcapi_receive_chunk@plt>
test   %eax,%eax
js     1161 <pptcapi_receive_chunk+0x41>
add    %eax,%ebx
jmp    1163 <pptcapi_receive_chunk+0x43>
mov    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pptcapi_connection = type { i32 }
define i32 @pptcapi_receive_chunk(%struct.pptcapi_connection* %0, i8* %1, i32 %2, i8** %3) {
%5 = alloca i32
%6 = alloca %struct.pptcapi_connection*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store %struct.pptcapi_connection* %0, %struct.pptcapi_connection** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8** %3, i8*** %9
%14 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %6
%15 = load i8*, i8** %7
%16 = load i32, i32* %8
%17 = load i8**, i8*** %9
%18 = call i32 @pptcapi_doreceive(%struct.pptcapi_connection* %14, i8* %15, i32 %16, i8** %17)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = load i32, i32* %10
store i32 %22, i32* %5
br label %51
23: ; preds = %4
%24 = load i32, i32* %10
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %49
27: ; preds = %23
%28 = load i8*, i8** %7
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8* %31, i8** %11
%32 = load i32, i32* %8
%33 = load i32, i32* %10
%34 = sub nsw i32 %32, %33
store i32 %34, i32* %12
%35 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %6
%36 = load i8*, i8** %11
%37 = load i32, i32* %12
%38 = load i8**, i8*** %9
%39 = call i32 @pptcapi_receive_chunk(%struct.pptcapi_connection* %35, i8* %36, i32 %37, i8** %38)
store i32 %39, i32* %13
%40 = load i32, i32* %13
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %27
%43 = load i32, i32* %13
store i32 %43, i32* %5
br label %51
44: ; preds = %27
%45 = load i32, i32* %13
%46 = load i32, i32* %10
%47 = add nsw i32 %46, %45
store i32 %47, i32* %10
br label %48
48: ; preds = %44
br label %49
49: ; preds = %48, %23
%50 = load i32, i32* %10
store i32 %50, i32* %5
br label %51
51: ; preds = %49, %42, %21
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
%struct.pptcapi_connection = type { i32 }
define i32 @pptcapi_receive_chunk(%struct.pptcapi_connection* %0, i8* %1, i32 %2, i8** %3) {
%5 = tail call i32 @pptcapi_doreceive(%struct.pptcapi_connection* %0, i8* %1, i32 %2, i8** %3)
%6 = icmp slt i32 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %4
%8 = icmp slt i32 %5, %2
br i1 %8, label %9, label %16
9: ; preds = %7
%10 = zext i32 %5 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = sub nsw i32 %2, %5
%13 = tail call i32 @pptcapi_receive_chunk(%struct.pptcapi_connection* %0, i8* %11, i32 %12, i8** %3)
%14 = icmp slt i32 %13, 0
%15 = add nsw i32 %13, %5
br i1 %14, label %18, label %16
16: ; preds = %9, %7
%17 = phi i32 [ %15, %9 ], [ %5, %7 ]
br label %18
18: ; preds = %4, %9, %16
%19 = phi i32 [ %17, %16 ], [ %13, %9 ], [ %5, %4 ]
ret i32 %19
}