#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 *, size_t);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, size_t, size_t, size_t);
int rand();
size_t snprintf(char *, size_t, char *, ...);
char *build_chunked_response(size_t content_length, size_t *response_len) {
  char *response_buffer;
  char *content_buffer;
  size_t buffer_length;
  size_t i;
  unsigned int 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=%lu buffer_length=%lu content_length=%lu\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    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x100,%rax
mov    -0x10(%rbp),%rcx
shr    $0x4,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11be <build_chunked_response+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    1359 <build_chunked_response+0x1e9>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    0xe33(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120c <build_chunked_response+0x9c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1359 <build_chunked_response+0x1e9>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <build_content@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1303 <build_chunked_response+0x193>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xc7,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x3c(%rbp),%ecx
add    %rcx,%rax
cmp    -0x10(%rbp),%rax
jbe    1263 <build_chunked_response+0xf3>
mov    -0x10(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rax
sub    (%rax),%rsi
mov    -0x3c(%rbp),%ecx
lea    0xdc7(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    (%rax),%rdi
mov    -0x28(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%eax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rdx
mov    (%rdx),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
movb   $0xd,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rdx
mov    (%rdx),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
movb   $0xa,(%rax,%rcx,1)
jmp    1221 <build_chunked_response+0xb1>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    (%rax),%rdi
lea    0xd3a(%rip),%rsi
mov    $0x5,%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x5,%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd09(%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
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rdi,%rax
shr    $0x4,%rax
lea    (%rdi,%rax,1),%r14
add    $0x100,%r14
mov    %r14,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     128a <build_chunked_response+0x11a>
mov    %rax,%r12
lea    0xe52(%rip),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rax,(%rbx)
mov    %rbp,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     128f <build_chunked_response+0x11f>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <build_content@plt>
test   %rbp,%rbp
mov    %r14,(%rsp)
je     129c <build_chunked_response+0x12c>
xor    %r15d,%r15d
nopl   (%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
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
lea    (%rax,%r15,1),%rcx
mov    %ebp,%r14d
sub    %r15d,%r14d
cmp    %rbp,%rcx
cmovbe %eax,%r14d
mov    (%rbx),%rax
lea    (%r12,%rax,1),%rdi
mov    (%rsp),%rsi
sub    %rax,%rsi
lea    0xe0a(%rip),%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
add    (%rbx),%rax
mov    %rax,(%rbx)
lea    (%rax,%r12,1),%rdi
lea    (%r15,%r13,1),%rsi
mov    %r14d,%edx
call   1070 <memcpy@plt>
mov    (%rbx),%rax
lea    (%rax,%r14,1),%rcx
add    %r14,%r15
movb   $0xd,(%r12,%rcx,1)
add    %r14,%rax
add    $0x2,%rax
mov    %rax,(%rbx)
movb   $0xa,0x1(%r12,%rcx,1)
cmp    %rbp,%r15
jb     11f0 <build_chunked_response+0x80>
jmp    129f <build_chunked_response+0x12f>
xor    %r12d,%r12d
jmp    12de <build_chunked_response+0x16e>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
jmp    12de <build_chunked_response+0x16e>
mov    (%rbx),%rax
mov    %r12,%rdi
add    %rax,%rdi
lea    0xda3(%rip),%rsi
mov    $0x5,%edx
call   1070 <memcpy@plt>
mov    (%rbx),%r14
add    $0x5,%r14
mov    %r14,(%rbx)
mov    %r13,%rdi
call   1030 <free@plt>
lea    0xd86(%rip),%rdi
mov    %r14,%rsi
mov    (%rsp),%rdx
mov    %rbp,%rcx
call   1050 <printf@plt>
mov    %r12,%rax
add    $0x8,%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 = [54 x i8] c"resp_length=%lu buffer_length=%lu content_length=%lu\0A\00"
define i8* @build_chunked_response(i64 %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i64 %0, i64* %4
store i64* %1, i64** %5
%11 = load i64, i64* %4
%12 = add i64 256, %11
%13 = load i64, i64* %4
%14 = lshr i64 %13, 4
%15 = add i64 %12, %14
store i64 %15, i64* %8
%16 = load i64, i64* %8
%17 = call i8* @malloc(i64 %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 %115
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = load i64, i64* %8
%24 = call i64 (i8*, i64, i8*, ...) @snprintf(i8* %22, i64 %23, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0))
%25 = load i64*, i64** %5
store i64 %24, i64* %25
%26 = load i64, i64* %4
%27 = call i8* @malloc(i64 %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 %115
33: ; preds = %21
%34 = load i8*, i8** %7
%35 = load i64, i64* %4
%36 = call i32 @build_content(i8* %34, i64 %35)
store i64 0, i64* %9
br label %37
37: ; preds = %56, %33
%38 = load i64, i64* %9
%39 = load i64, i64* %4
%40 = icmp ult i64 %38, %39
br i1 %40, label %41, label %98
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 i64, i64* %9
%46 = load i32, i32* %10
%47 = zext i32 %46 to i64
%48 = add i64 %45, %47
%49 = load i64, i64* %4
%50 = icmp ugt i64 %48, %49
br i1 %50, label %51, label %56
51: ; preds = %41
%52 = load i64, i64* %4
%53 = load i64, i64* %9
%54 = sub i64 %52, %53
%55 = trunc i64 %54 to i32
store i32 %55, i32* %10
br label %56
56: ; preds = %51, %41
%57 = load i8*, i8** %6
%58 = load i64*, i64** %5
%59 = load i64, i64* %58
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i64, i64* %8
%62 = load i64*, i64** %5
%63 = load i64, i64* %62
%64 = sub i64 %61, %63
%65 = load i32, i32* %10
%66 = call i64 (i8*, i64, i8*, ...) @snprintf(i8* %60, i64 %64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %65)
%67 = load i64*, i64** %5
%68 = load i64, i64* %67
%69 = add i64 %68, %66
store i64 %69, i64* %67
%70 = load i8*, i8** %6
%71 = load i64*, i64** %5
%72 = load i64, i64* %71
%73 = getelementptr inbounds i8, i8* %70, i64 %72
%74 = load i8*, i8** %7
%75 = load i64, i64* %9
%76 = getelementptr inbounds i8, i8* %74, i64 %75
%77 = load i32, i32* %10
%78 = call i32 @memcpy(i8* %73, i8* %76, i32 %77)
%79 = load i32, i32* %10
%80 = zext i32 %79 to i64
%81 = load i64*, i64** %5
%82 = load i64, i64* %81
%83 = add i64 %82, %80
store i64 %83, i64* %81
%84 = load i32, i32* %10
%85 = zext i32 %84 to i64
%86 = load i64, i64* %9
%87 = add i64 %86, %85
store i64 %87, i64* %9
%88 = load i8*, i8** %6
%89 = load i64*, i64** %5
%90 = load i64, i64* %89
%91 = add i64 %90, 1
store i64 %91, i64* %89
%92 = getelementptr inbounds i8, i8* %88, i64 %90
store i8 13, i8* %92
%93 = load i8*, i8** %6
%94 = load i64*, i64** %5
%95 = load i64, i64* %94
%96 = add i64 %95, 1
store i64 %96, i64* %94
%97 = getelementptr inbounds i8, i8* %93, i64 %95
store i8 10, i8* %97
br label %37
98: ; preds = %37
%99 = load i8*, i8** %6
%100 = load i64*, i64** %5
%101 = load i64, i64* %100
%102 = getelementptr inbounds i8, i8* %99, i64 %101
%103 = call i32 @memcpy(i8* %102, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%104 = load i64*, i64** %5
%105 = load i64, i64* %104
%106 = add i64 %105, 5
store i64 %106, i64* %104
%107 = load i8*, i8** %7
%108 = call i32 @free(i8* %107)
%109 = load i64*, i64** %5
%110 = load i64, i64* %109
%111 = load i64, i64* %8
%112 = load i64, i64* %4
%113 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %110, i64 %111, i64 %112)
%114 = load i8*, i8** %6
store i8* %114, i8** %3
br label %115
115: ; preds = %98, %30, %20
%116 = load i8*, i8** %3
ret i8* %116
}

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