#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*/ build_content(char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, int, int, int);
int rand();
int snprintf(char *, int, char *, ...);
char *build_chunked_response(int content_length, int *response_len) {
  char *response_buffer;
  char *content_buffer;
  int buffer_length;
  int i, n;
  buffer_length = 256 + content_length + (content_length >> 4);
  response_buffer = malloc(buffer_length);
  *response_len = snprintf(response_buffer, buffer_length,
                           "HTTP/1.1 200 OK\r\n"
                           "Content-Type: text/plain\r\n"
                           "Transfer-Encoding: chunked\r\n"
                           "\r\n");
  content_buffer = malloc(content_length);
  build_content(content_buffer, content_length);
  i = 0;
  while (i < content_length) {
    n = (rand() % 199) + 1;
    if (i + n > content_length) {
      n = content_length - i;
    }
    *response_len += snprintf(response_buffer + *response_len,
                              buffer_length - *response_len, "%x\r\n", n);
    memcpy(response_buffer + *response_len, content_buffer + i, n);
    *response_len += n;
    i += n;
    response_buffer[(*response_len)++] = '\r';
    response_buffer[(*response_len)++] = '\n';
  }
  memcpy(response_buffer + *response_len, "0\r\n", 3);
  *response_len += 3;
  free(content_buffer);
  printf("resp_length=%d buffer_length=%d content_length=%d\n", *response_len,
         buffer_length, content_length);
  return response_buffer;
}


x86 assembly (clang, -O0):
<build_chunked_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%eax
mov    -0x4(%rbp),%ecx
sar    $0x4,%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    0xe54(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1040 <build_content@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12af <build_chunked_response+0x13f>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xc7,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    1213 <build_chunked_response+0xa3>
mov    -0x4(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rax
sub    (%rax),%esi
mov    -0x2c(%rbp),%ecx
lea    0xe16(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdx
mov    (%rdx),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,(%rdx)
movslq %ecx,%rcx
movb   $0xd,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdx
mov    (%rdx),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,(%rdx)
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
jmp    11da <build_chunked_response+0x6a>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
lea    0xd8b(%rip),%rsi
mov    $0x3,%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x3,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    -0x4(%rbp),%ecx
lea    0xd5e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_chunked_response>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%ebx
mov    %edi,%eax
sar    $0x4,%eax
lea    (%rbx,%rax,1),%ebp
add    $0x100,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
lea    0xe5e(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %eax,(%r15)
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %ebx,%esi
call   1040 <build_content@plt>
test   %ebx,%ebx
mov    %r13,0x10(%rsp)
jle    1291 <build_chunked_response+0x121>
xor    %r14d,%r14d
mov    %ebp,0xc(%rsp)
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <rand@plt>
cltq
imul   $0x5254e78f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0xc7,%ecx,%ecx
mov    %eax,%edx
sub    %ecx,%edx
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
lea    (%r14,%rdx,1),%ecx
add    $0x1,%ecx
mov    %ebx,%r13d
sub    %r14d,%r13d
cmp    %ebx,%ecx
cmovle %eax,%r13d
movslq (%r15),%rdi
sub    %edi,%ebp
add    %r12,%rdi
mov    %ebp,%esi
lea    0xe18(%rip),%rdx
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
movslq (%r15),%rcx
movslq %eax,%rdi
add    %rcx,%rdi
mov    %edi,(%r15)
add    %r12,%rdi
mov    %r12,%rbp
movslq %r14d,%r12
mov    0x10(%rsp),%rax
lea    (%rax,%r12,1),%rsi
mov    %r13d,%edx
call   1070 <memcpy@plt>
movslq (%r15),%rcx
movslq %r13d,%r14
add    %r14,%rcx
add    %r12d,%r14d
mov    %rbp,%r12
mov    0xc(%rsp),%ebp
movb   $0xd,(%r12,%rcx,1)
lea    0x2(%rcx),%eax
mov    %eax,(%r15)
movb   $0xa,0x1(%r12,%rcx,1)
cmp    %ebx,%r14d
jl     11e0 <build_chunked_response+0x70>
jmp    1294 <build_chunked_response+0x124>
mov    (%r15),%eax
cltq
mov    %r12,%rdi
add    %rax,%rdi
lea    0xdac(%rip),%rsi
mov    $0x3,%edx
call   1070 <memcpy@plt>
mov    (%r15),%r13d
add    $0x3,%r13d
mov    %r13d,(%r15)
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
lea    0xd8b(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1050 <printf@plt>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [74 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: text/plain\0D\0ATransfer-Encoding: chunked\0D\0A\0D\0A\00"
@.str.1 = [5 x i8] c"%x\0D\0A\00"
@.str.2 = [4 x i8] c"0\0D\0A\00"
@.str.3 = [51 x i8] c"resp_length=%d buffer_length=%d content_length=%d\0A\00"
define i8* @build_chunked_response(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%10 = load i32, i32* %3
%11 = add nsw i32 256, %10
%12 = load i32, i32* %3
%13 = ashr i32 %12, 4
%14 = add nsw i32 %11, %13
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %17, i32 %18, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0))
%20 = load i32*, i32** %4
store i32 %19, i32* %20
%21 = load i32, i32* %3
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = load i32, i32* %3
%25 = call i32 @build_content(i8* %23, i32 %24)
store i32 0, i32* %8
br label %26
26: ; preds = %43, %2
%27 = load i32, i32* %8
%28 = load i32, i32* %3
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %88
30: ; preds = %26
%31 = call i32 (...) @rand()
%32 = srem i32 %31, 199
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
%34 = load i32, i32* %8
%35 = load i32, i32* %9
%36 = add nsw i32 %34, %35
%37 = load i32, i32* %3
%38 = icmp sgt i32 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %30
%40 = load i32, i32* %3
%41 = load i32, i32* %8
%42 = sub nsw i32 %40, %41
store i32 %42, i32* %9
br label %43
43: ; preds = %39, %30
%44 = load i8*, i8** %5
%45 = load i32*, i32** %4
%46 = load i32, i32* %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
%49 = load i32, i32* %7
%50 = load i32*, i32** %4
%51 = load i32, i32* %50
%52 = sub nsw i32 %49, %51
%53 = load i32, i32* %9
%54 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %48, i32 %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %53)
%55 = load i32*, i32** %4
%56 = load i32, i32* %55
%57 = add nsw i32 %56, %54
store i32 %57, i32* %55
%58 = load i8*, i8** %5
%59 = load i32*, i32** %4
%60 = load i32, i32* %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %58, i64 %61
%63 = load i8*, i8** %6
%64 = load i32, i32* %8
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i32, i32* %9
%68 = call i32 @memcpy(i8* %62, i8* %66, i32 %67)
%69 = load i32, i32* %9
%70 = load i32*, i32** %4
%71 = load i32, i32* %70
%72 = add nsw i32 %71, %69
store i32 %72, i32* %70
%73 = load i32, i32* %9
%74 = load i32, i32* %8
%75 = add nsw i32 %74, %73
store i32 %75, i32* %8
%76 = load i8*, i8** %5
%77 = load i32*, i32** %4
%78 = load i32, i32* %77
%79 = add nsw i32 %78, 1
store i32 %79, i32* %77
%80 = sext i32 %78 to i64
%81 = getelementptr inbounds i8, i8* %76, i64 %80
store i8 13, i8* %81
%82 = load i8*, i8** %5
%83 = load i32*, i32** %4
%84 = load i32, i32* %83
%85 = add nsw i32 %84, 1
store i32 %85, i32* %83
%86 = sext i32 %84 to i64
%87 = getelementptr inbounds i8, i8* %82, i64 %86
store i8 10, i8* %87
br label %26
88: ; preds = %26
%89 = load i8*, i8** %5
%90 = load i32*, i32** %4
%91 = load i32, i32* %90
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %89, i64 %92
%94 = call i32 @memcpy(i8* %93, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%95 = load i32*, i32** %4
%96 = load i32, i32* %95
%97 = add nsw i32 %96, 3
store i32 %97, i32* %95
%98 = load i8*, i8** %6
%99 = call i32 @free(i8* %98)
%100 = load i32*, i32** %4
%101 = load i32, i32* %100
%102 = load i32, i32* %7
%103 = load i32, i32* %3
%104 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %101, i32 %102, i32 %103)
%105 = load i8*, i8** %5
ret i8* %105
}

LLVM IR (-O2):
@.str = [74 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: text/plain\0D\0ATransfer-Encoding: chunked\0D\0A\0D\0A\00"
@.str.1 = [5 x i8] c"%x\0D\0A\00"
@.str.2 = [4 x i8] c"0\0D\0A\00"
@.str.3 = [51 x i8] c"resp_length=%d buffer_length=%d content_length=%d\0A\00"
define i8* @build_chunked_response(i32 %0, i32* nocapture %1) {
%3 = add nsw i32 %0, 256
%4 = ashr i32 %0, 4
%5 = add nsw i32 %3, %4
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 %5, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0))
store i32 %7, i32* %1
%8 = tail call i8* @malloc(i32 %0)
%9 = tail call i32 @build_content(i8* %8, i32 %0)
%10 = icmp sgt i32 %0, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = load i32, i32* %1
br label %44
13: ; preds = %2, %13
%14 = phi i32 [ %36, %13 ], [ 0, %2 ]
%15 = tail call i32 (...) @rand()
%16 = srem i32 %15, 199
%17 = add nsw i32 %16, 1
%18 = add nsw i32 %17, %14
%19 = icmp sgt i32 %18, %0
%20 = sub nsw i32 %0, %14
%21 = select i1 %19, i32 %20, i32 %17
%22 = load i32, i32* %1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %6, i64 %23
%25 = sub nsw i32 %5, %22
%26 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %24, i32 %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%27 = load i32, i32* %1
%28 = add nsw i32 %27, %26
store i32 %28, i32* %1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %6, i64 %29
%31 = sext i32 %14 to i64
%32 = getelementptr inbounds i8, i8* %8, i64 %31
%33 = tail call i32 @memcpy(i8* %30, i8* %32, i32 %21)
%34 = load i32, i32* %1
%35 = add nsw i32 %34, %21
%36 = add nsw i32 %21, %14
%37 = add nsw i32 %35, 1
%38 = sext i32 %35 to i64
%39 = getelementptr inbounds i8, i8* %6, i64 %38
store i8 13, i8* %39
%40 = add nsw i32 %35, 2
store i32 %40, i32* %1
%41 = sext i32 %37 to i64
%42 = getelementptr inbounds i8, i8* %6, i64 %41
store i8 10, i8* %42
%43 = icmp slt i32 %36, %0
br i1 %43, label %13, label %44
44: ; preds = %13, %11
%45 = phi i32 [ %12, %11 ], [ %40, %13 ]
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %6, i64 %46
%48 = tail call i32 @memcpy(i8* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%49 = load i32, i32* %1
%50 = add nsw i32 %49, 3
store i32 %50, i32* %1
%51 = tail call i32 @free(i8* %8)
%52 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %50, i32 %5, i32 %0)
ret i8* %6
}