#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 */
scalar_t__ malloc(int);
int pptcapi_dosend(struct pptcapi_connection *, char *, int, char **);
int /*<<< orphan*/ snprintf(char *, int, char *, int, ...);
int pptcapi_send_chunk(struct pptcapi_connection *connection, char type,
                       char *buffer, int len, char **error) {
  if (len > 65535) {
    *error = (char *)malloc(512);
    if (*error) {
      snprintf(*error, 512, "length of chunk to send %d is too big", len);
    }
    return 2;
  }
  char header[7 + 1 + 1];
  snprintf(header, 7 + 1 + 1, "%07x%c", len, type);
  int header_send = pptcapi_dosend(connection, header, 7 + 1, error);
  if (header_send != 1) {
    return header_send;
  }
  if (len > 0) {
    int dosend = pptcapi_dosend(connection, buffer, len, error);
    return dosend;
  }
  return 1;
}


x86 assembly (clang, -O0):
<pptcapi_send_chunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
cmpl   $0xffff,-0x24(%rbp)
jle    11a5 <pptcapi_send_chunk+0x75>
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     1199 <pptcapi_send_chunk+0x69>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%ecx
mov    $0x200,%esi
lea    0xe6e(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1225 <pptcapi_send_chunk+0xf5>
lea    -0x39(%rbp),%rdi
mov    -0x24(%rbp),%ecx
movsbl -0x11(%rbp),%r8d
mov    $0x9,%esi
lea    0xe69(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x39(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x8,%edx
call   1030 <pptcapi_dosend@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x1,-0x40(%rbp)
je     11f2 <pptcapi_send_chunk+0xc2>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <pptcapi_send_chunk+0xf5>
cmpl   $0x0,-0x24(%rbp)
jle    121e <pptcapi_send_chunk+0xee>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rcx
call   1030 <pptcapi_dosend@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <pptcapi_send_chunk+0xf5>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pptcapi_send_chunk>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r14
mov    %ecx,%ebx
cmp    $0x10000,%ecx
jl     117a <pptcapi_send_chunk+0x4a>
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
mov    $0x2,%ebp
test   %rax,%rax
je     11d8 <pptcapi_send_chunk+0xa8>
lea    0xe99(%rip),%rdx
mov    %rax,%rdi
mov    $0x200,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    11d8 <pptcapi_send_chunk+0xa8>
mov    %rdx,%r15
mov    %rdi,%r12
movsbl %sil,%r8d
lea    0xe9b(%rip),%rdx
lea    0x7(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x9,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
mov    %r14,%rcx
call   1030 <pptcapi_dosend@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    11d8 <pptcapi_send_chunk+0xa8>
test   %ebx,%ebx
jle    11d3 <pptcapi_send_chunk+0xa3>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %r14,%rcx
call   1030 <pptcapi_dosend@plt>
mov    %eax,%ebp
jmp    11d8 <pptcapi_send_chunk+0xa8>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pptcapi_connection = type { i32 }
@.str = [38 x i8] c"length of chunk to send %d is too big\00"
@.str.1 = [7 x i8] c"%07x%c\00"
define i32 @pptcapi_send_chunk(%struct.pptcapi_connection* %0, i8 signext %1, i8* %2, i32 %3, i8** %4) {
%6 = alloca i32
%7 = alloca %struct.pptcapi_connection*
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8**
%12 = alloca [9 x i8]
%13 = alloca i32
%14 = alloca i32
store %struct.pptcapi_connection* %0, %struct.pptcapi_connection** %7
store i8 %1, i8* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8** %4, i8*** %11
%15 = load i32, i32* %10
%16 = icmp sgt i32 %15, 65535
br i1 %16, label %17, label %30
17: ; preds = %5
%18 = call i64 @malloc(i32 512)
%19 = inttoptr i64 %18 to i8*
%20 = load i8**, i8*** %11
store i8* %19, i8** %20
%21 = load i8**, i8*** %11
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = load i8**, i8*** %11
%26 = load i8*, i8** %25
%27 = load i32, i32* %10
%28 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %26, i32 512, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %24, %17
store i32 2, i32* %6
br label %55
30: ; preds = %5
%31 = getelementptr inbounds [9 x i8], [9 x i8]* %12, i64 0, i64 0
%32 = load i32, i32* %10
%33 = load i8, i8* %8
%34 = sext i8 %33 to i32
%35 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %31, i32 9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %34)
%36 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %7
%37 = getelementptr inbounds [9 x i8], [9 x i8]* %12, i64 0, i64 0
%38 = load i8**, i8*** %11
%39 = call i32 @pptcapi_dosend(%struct.pptcapi_connection* %36, i8* %37, i32 8, i8** %38)
store i32 %39, i32* %13
%40 = load i32, i32* %13
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %44
42: ; preds = %30
%43 = load i32, i32* %13
store i32 %43, i32* %6
br label %55
44: ; preds = %30
%45 = load i32, i32* %10
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %44
%48 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %7
%49 = load i8*, i8** %9
%50 = load i32, i32* %10
%51 = load i8**, i8*** %11
%52 = call i32 @pptcapi_dosend(%struct.pptcapi_connection* %48, i8* %49, i32 %50, i8** %51)
store i32 %52, i32* %14
%53 = load i32, i32* %14
store i32 %53, i32* %6
br label %55
54: ; preds = %44
store i32 1, i32* %6
br label %55
55: ; preds = %54, %47, %42, %29
%56 = load i32, i32* %6
ret i32 %56
}

LLVM IR (-O2):
%struct.pptcapi_connection = type { i32 }
@.str = [38 x i8] c"length of chunk to send %d is too big\00"
@.str.1 = [7 x i8] c"%07x%c\00"
define i32 @pptcapi_send_chunk(%struct.pptcapi_connection* %0, i8 signext %1, i8* %2, i32 %3, i8** %4) {
%6 = alloca [9 x i8]
%7 = icmp sgt i32 %3, 65535
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = tail call i64 @malloc(i32 512)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %4
%11 = icmp eq i64 %9, 0
br i1 %11, label %26, label %12
12: ; preds = %8
%13 = tail call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %10, i32 512, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %26
14: ; preds = %5
%15 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%16 = sext i8 %1 to i32
%17 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %15, i32 9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %16)
%18 = call i32 @pptcapi_dosend(%struct.pptcapi_connection* %0, i8* %15, i32 8, i8** %4)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = icmp sgt i32 %3, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = call i32 @pptcapi_dosend(%struct.pptcapi_connection* %0, i8* %2, i32 %3, i8** %4)
br label %24
24: ; preds = %20, %14, %22
%25 = phi i32 [ %23, %22 ], [ %18, %14 ], [ 1, %20 ]
br label %26
26: ; preds = %8, %12, %24
%27 = phi i32 [ %25, %24 ], [ 2, %12 ], [ 2, %8 ]
ret i32 %27
}