#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);
  if (response_buffer == NULL)
    return NULL;
  *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);
  if (content_buffer == NULL) {
    free(response_buffer);
    return NULL;
  }
  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\r\n", 5);
  *response_len += 5;
  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    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x100,%eax
mov    -0xc(%rbp),%ecx
sar    $0x4,%ecx
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b6 <build_chunked_response+0x46>
movq   $0x0,-0x8(%rbp)
jmp    133b <build_chunked_response+0x1cb>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
lea    0xe3c(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1200 <build_chunked_response+0x90>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    133b <build_chunked_response+0x1cb>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <build_content@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12e8 <build_chunked_response+0x178>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xc7,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x34(%rbp)
mov    -0x30(%rbp),%eax
add    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    124c <build_chunked_response+0xdc>
mov    -0xc(%rbp),%eax
sub    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rax
sub    (%rax),%esi
mov    -0x34(%rbp),%ecx
lea    0xddd(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x34(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rdx
mov    (%rdx),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,(%rdx)
movslq %ecx,%rcx
movb   $0xd,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rdx
mov    (%rdx),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,(%rdx)
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
jmp    1213 <build_chunked_response+0xa3>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
lea    0xd52(%rip),%rsi
mov    $0x5,%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x5,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xd27(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%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,%r14
mov    %edi,%ebp
mov    %edi,%eax
sar    $0x4,%eax
lea    (%rax,%rbp,1),%ebx
add    $0x100,%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12a1 <build_chunked_response+0x131>
mov    %rax,%r12
lea    0xe55(%rip),%rdx
mov    %rax,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %eax,(%r14)
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12a6 <build_chunked_response+0x136>
mov    %rax,%r13
mov    %rax,%rdi
mov    %ebp,%esi
call   1040 <build_content@plt>
test   %ebp,%ebp
mov    %r13,0x10(%rsp)
jle    12b3 <build_chunked_response+0x143>
xor    %r15d,%r15d
mov    %ebx,0xc(%rsp)
nopl   0x0(%rax,%rax,1)
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    (%r15,%rdx,1),%ecx
add    $0x1,%ecx
mov    %ebp,%r13d
sub    %r15d,%r13d
cmp    %ebp,%ecx
cmovle %eax,%r13d
movslq (%r14),%rdi
sub    %edi,%ebx
add    %r12,%rdi
mov    %ebx,%esi
lea    0xe08(%rip),%rdx
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
movslq (%r14),%rcx
movslq %eax,%rdi
add    %rcx,%rdi
mov    %edi,(%r14)
add    %r12,%rdi
mov    %r12,%rbx
movslq %r15d,%r12
mov    0x10(%rsp),%rax
lea    (%rax,%r12,1),%rsi
mov    %r13d,%edx
call   1070 <memcpy@plt>
movslq (%r14),%rcx
movslq %r13d,%r15
add    %r15,%rcx
add    %r12d,%r15d
mov    %rbx,%r12
mov    0xc(%rsp),%ebx
movb   $0xd,(%r12,%rcx,1)
lea    0x2(%rcx),%eax
mov    %eax,(%r14)
movb   $0xa,0x1(%r12,%rcx,1)
cmp    %ebp,%r15d
jl     11f0 <build_chunked_response+0x80>
jmp    12b6 <build_chunked_response+0x146>
xor    %r12d,%r12d
jmp    12f6 <build_chunked_response+0x186>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
jmp    12f6 <build_chunked_response+0x186>
mov    (%r14),%eax
cltq
mov    %r12,%rdi
add    %rax,%rdi
lea    0xd8a(%rip),%rsi
mov    $0x5,%edx
call   1070 <memcpy@plt>
mov    (%r14),%r13d
add    $0x5,%r13d
mov    %r13d,(%r14)
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
lea    0xd6b(%rip),%rdi
mov    %r13d,%esi
mov    %ebx,%edx
mov    %ebp,%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 = [6 x i8] c"0\0D\0A\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 i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%11 = load i32, i32* %4
%12 = add nsw i32 256, %11
%13 = load i32, i32* %4
%14 = ashr i32 %13, 4
%15 = add nsw i32 %12, %14
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %2
store i8* null, i8** %3
br label %117
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0))
%25 = load i32*, i32** %5
store i32 %24, i32* %25
%26 = load i32, i32* %4
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
store i8* null, i8** %3
br label %117
33: ; preds = %21
%34 = load i8*, i8** %7
%35 = load i32, i32* %4
%36 = call i32 @build_content(i8* %34, i32 %35)
store i32 0, i32* %9
br label %37
37: ; preds = %54, %33
%38 = load i32, i32* %9
%39 = load i32, i32* %4
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %99
41: ; preds = %37
%42 = call i32 (...) @rand()
%43 = srem i32 %42, 199
%44 = add nsw i32 %43, 1
store i32 %44, i32* %10
%45 = load i32, i32* %9
%46 = load i32, i32* %10
%47 = add nsw i32 %45, %46
%48 = load i32, i32* %4
%49 = icmp sgt i32 %47, %48
br i1 %49, label %50, label %54
50: ; preds = %41
%51 = load i32, i32* %4
%52 = load i32, i32* %9
%53 = sub nsw i32 %51, %52
store i32 %53, i32* %10
br label %54
54: ; preds = %50, %41
%55 = load i8*, i8** %6
%56 = load i32*, i32** %5
%57 = load i32, i32* %56
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
%60 = load i32, i32* %8
%61 = load i32*, i32** %5
%62 = load i32, i32* %61
%63 = sub nsw i32 %60, %62
%64 = load i32, i32* %10
%65 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %59, i32 %63, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %64)
%66 = load i32*, i32** %5
%67 = load i32, i32* %66
%68 = add nsw i32 %67, %65
store i32 %68, i32* %66
%69 = load i8*, i8** %6
%70 = load i32*, i32** %5
%71 = load i32, i32* %70
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %69, i64 %72
%74 = load i8*, i8** %7
%75 = load i32, i32* %9
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = load i32, i32* %10
%79 = call i32 @memcpy(i8* %73, i8* %77, i32 %78)
%80 = load i32, i32* %10
%81 = load i32*, i32** %5
%82 = load i32, i32* %81
%83 = add nsw i32 %82, %80
store i32 %83, i32* %81
%84 = load i32, i32* %10
%85 = load i32, i32* %9
%86 = add nsw i32 %85, %84
store i32 %86, i32* %9
%87 = load i8*, i8** %6
%88 = load i32*, i32** %5
%89 = load i32, i32* %88
%90 = add nsw i32 %89, 1
store i32 %90, i32* %88
%91 = sext i32 %89 to i64
%92 = getelementptr inbounds i8, i8* %87, i64 %91
store i8 13, i8* %92
%93 = load i8*, i8** %6
%94 = load i32*, i32** %5
%95 = load i32, i32* %94
%96 = add nsw i32 %95, 1
store i32 %96, i32* %94
%97 = sext i32 %95 to i64
%98 = getelementptr inbounds i8, i8* %93, i64 %97
store i8 10, i8* %98
br label %37
99: ; preds = %37
%100 = load i8*, i8** %6
%101 = load i32*, i32** %5
%102 = load i32, i32* %101
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds i8, i8* %100, i64 %103
%105 = call i32 @memcpy(i8* %104, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%106 = load i32*, i32** %5
%107 = load i32, i32* %106
%108 = add nsw i32 %107, 5
store i32 %108, i32* %106
%109 = load i8*, i8** %7
%110 = call i32 @free(i8* %109)
%111 = load i32*, i32** %5
%112 = load i32, i32* %111
%113 = load i32, i32* %8
%114 = load i32, i32* %4
%115 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %112, i32 %113, i32 %114)
%116 = load i8*, i8** %6
store i8* %116, i8** %3
br label %117
117: ; preds = %99, %30, %20
%118 = load i8*, i8** %3
ret i8* %118
}

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 = [6 x i8] c"0\0D\0A\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 = icmp eq i8* %6, null
br i1 %7, label %59, label %8
8: ; preds = %2
%9 = 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 %9, i32* %1
%10 = tail call i8* @malloc(i32 %0)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @free(i8* %6)
br label %59
14: ; preds = %8
%15 = tail call i32 @build_content(i8* %10, i32 %0)
%16 = icmp sgt i32 %0, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = load i32, i32* %1
br label %50
19: ; preds = %14, %19
%20 = phi i32 [ %42, %19 ], [ 0, %14 ]
%21 = tail call i32 (...) @rand()
%22 = srem i32 %21, 199
%23 = add nsw i32 %22, 1
%24 = add nsw i32 %23, %20
%25 = icmp sgt i32 %24, %0
%26 = sub nsw i32 %0, %20
%27 = select i1 %25, i32 %26, i32 %23
%28 = load i32, i32* %1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %6, i64 %29
%31 = sub nsw i32 %5, %28
%32 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %30, i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%33 = load i32, i32* %1
%34 = add nsw i32 %33, %32
store i32 %34, i32* %1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %6, i64 %35
%37 = sext i32 %20 to i64
%38 = getelementptr inbounds i8, i8* %10, i64 %37
%39 = tail call i32 @memcpy(i8* %36, i8* %38, i32 %27)
%40 = load i32, i32* %1
%41 = add nsw i32 %40, %27
%42 = add nsw i32 %27, %20
%43 = add nsw i32 %41, 1
%44 = sext i32 %41 to i64
%45 = getelementptr inbounds i8, i8* %6, i64 %44
store i8 13, i8* %45
%46 = add nsw i32 %41, 2
store i32 %46, i32* %1
%47 = sext i32 %43 to i64
%48 = getelementptr inbounds i8, i8* %6, i64 %47
store i8 10, i8* %48
%49 = icmp slt i32 %42, %0
br i1 %49, label %19, label %50
50: ; preds = %19, %17
%51 = phi i32 [ %18, %17 ], [ %46, %19 ]
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %6, i64 %52
%54 = tail call i32 @memcpy(i8* %53, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%55 = load i32, i32* %1
%56 = add nsw i32 %55, 5
store i32 %56, i32* %1
%57 = tail call i32 @free(i8* %10)
%58 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %56, i32 %5, i32 %0)
br label %59
59: ; preds = %2, %50, %12
%60 = phi i8* [ null, %12 ], [ %6, %50 ], [ null, %2 ]
ret i8* %60
}