#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_send_chunk(struct pptcapi_connection *, char, char *, int, char **);
int pptcapi_send(struct pptcapi_connection *connection, char *buffer, int len,
                 char **error) {
  if (len <= 65535) {
    int res = pptcapi_send_chunk(connection, 'd', buffer, len, error);
    if (res != 1) {
      return res;
    }
    return pptcapi_send_chunk(connection, 'd', "", 0, error);
  }
  int send_result = pptcapi_send_chunk(connection, 'd', buffer, 65535, error);
  if (send_result != 1) {
    return send_result;
  }
  char *next_buffer = buffer + 65535;
  int next_len = len - 65535;
  return pptcapi_send(connection, next_buffer, next_len, error);
}


x86 assembly (clang, -O0):
<pptcapi_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
cmpl   $0xffff,-0x1c(%rbp)
jg     1198 <pptcapi_send+0x78>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    $0x64,%esi
call   1040 <pptcapi_send_chunk@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
je     1175 <pptcapi_send+0x55>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <pptcapi_send+0xdb>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%r8
mov    $0x64,%esi
lea    0xe77(%rip),%rdx
xor    %ecx,%ecx
call   1040 <pptcapi_send_chunk@plt>
mov    %eax,-0x4(%rbp)
jmp    11fb <pptcapi_send+0xdb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%r8
mov    $0x64,%esi
mov    $0xffff,%ecx
call   1040 <pptcapi_send_chunk@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x1,-0x30(%rbp)
je     11cb <pptcapi_send+0xab>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <pptcapi_send+0xdb>
mov    -0x18(%rbp),%rax
add    $0xffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0xffff,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
mov    -0x28(%rbp),%rcx
call   1030 <pptcapi_send@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pptcapi_send>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r15
cmp    $0x10000,%edx
jge    1140 <pptcapi_send+0x30>
mov    %r12d,%ecx
jmp    1178 <pptcapi_send+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
mov    $0xffff,%ecx
mov    %r14,%r8
call   1030 <pptcapi_send_chunk@plt>
cmp    $0x1,%eax
jne    11b4 <pptcapi_send+0xa4>
add    $0xffff,%rbx
lea    -0xffff(%r12),%ecx
ff
cmp    $0x1ffff,%r12d
mov    %ecx,%r12d
jge    1140 <pptcapi_send+0x30>
mov    %r15,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
mov    %r14,%r8
call   1030 <pptcapi_send_chunk@plt>
cmp    $0x1,%eax
jne    11b4 <pptcapi_send+0xa4>
lea    0xe69(%rip),%rdx
mov    %r15,%rdi
mov    $0x64,%esi
xor    %ecx,%ecx
mov    %r14,%r8
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <pptcapi_send_chunk@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.pptcapi_connection = type { i32 }
@.str = [1 x i8] zeroinitializer
define i32 @pptcapi_send(%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 i32
%12 = alloca i8*
%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 i32, i32* %8
%15 = icmp sle i32 %14, 65535
br i1 %15, label %16, label %30
16: ; preds = %4
%17 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %6
%18 = load i8*, i8** %7
%19 = load i32, i32* %8
%20 = load i8**, i8*** %9
%21 = call i32 @pptcapi_send_chunk(%struct.pptcapi_connection* %17, i8 signext 100, i8* %18, i32 %19, i8** %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = load i32, i32* %10
store i32 %25, i32* %5
br label %49
26: ; preds = %16
%27 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %6
%28 = load i8**, i8*** %9
%29 = call i32 @pptcapi_send_chunk(%struct.pptcapi_connection* %27, i8 signext 100, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 0, i8** %28)
store i32 %29, i32* %5
br label %49
30: ; preds = %4
%31 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %6
%32 = load i8*, i8** %7
%33 = load i8**, i8*** %9
%34 = call i32 @pptcapi_send_chunk(%struct.pptcapi_connection* %31, i8 signext 100, i8* %32, i32 65535, i8** %33)
store i32 %34, i32* %11
%35 = load i32, i32* %11
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = load i32, i32* %11
store i32 %38, i32* %5
br label %49
39: ; preds = %30
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 65535
store i8* %41, i8** %12
%42 = load i32, i32* %8
%43 = sub nsw i32 %42, 65535
store i32 %43, i32* %13
%44 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %6
%45 = load i8*, i8** %12
%46 = load i32, i32* %13
%47 = load i8**, i8*** %9
%48 = call i32 @pptcapi_send(%struct.pptcapi_connection* %44, i8* %45, i32 %46, i8** %47)
store i32 %48, i32* %5
br label %49
49: ; preds = %39, %37, %26, %24
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
%struct.pptcapi_connection = type { i32 }
@.str = [1 x i8] zeroinitializer
define i32 @pptcapi_send(%struct.pptcapi_connection* %0, i8* %1, i32 %2, i8** %3) {
%5 = icmp slt i32 %2, 65536
br i1 %5, label %6, label %13
6: ; preds = %18, %4
%7 = phi i8* [ %1, %4 ], [ %19, %18 ]
%8 = phi i32 [ %2, %4 ], [ %20, %18 ]
%9 = tail call i32 @pptcapi_send_chunk(%struct.pptcapi_connection* %0, i8 signext 100, i8* %7, i32 %8, i8** %3)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %22
11: ; preds = %6
%12 = tail call i32 @pptcapi_send_chunk(%struct.pptcapi_connection* %0, i8 signext 100, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 0, i8** %3)
br label %22
13: ; preds = %4, %18
%14 = phi i32 [ %20, %18 ], [ %2, %4 ]
%15 = phi i8* [ %19, %18 ], [ %1, %4 ]
%16 = tail call i32 @pptcapi_send_chunk(%struct.pptcapi_connection* %0, i8 signext 100, i8* %15, i32 65535, i8** %3)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %15, i64 65535
%20 = add nsw i32 %14, -65535
%21 = icmp slt i32 %14, 131071
br i1 %21, label %6, label %13
22: ; preds = %13, %11, %6
%23 = phi i32 [ %12, %11 ], [ %9, %6 ], [ %16, %13 ]
ret i32 %23
}