#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*/ assert(int);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
char *create_large_chunked_message(int body_size_in_kb, const char *headers) {
  int i;
  size_t wrote = 0;
  size_t headers_len = strlen(headers);
  size_t bufsize = headers_len + (5 + 1024 + 2) * body_size_in_kb + 6;
  char *buf = malloc(bufsize);
  memcpy(buf, headers, headers_len);
  wrote += headers_len;
  for (i = 0; i < body_size_in_kb; i++) {
    memcpy(buf + wrote, "400\r\n", 5);
    wrote += 5;
    memset(buf + wrote, 'C', 1024);
    wrote += 1024;
    strcpy(buf + wrote, "\r\n");
    wrote += 2;
  }
  memcpy(buf + wrote, "0\r\n\r\n", 6);
  wrote += 6;
  assert(wrote == bufsize);
  return buf;
}


x86 assembly (clang, -O0):
<create_large_chunked_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
imul   $0x407,-0x4(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x6,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1254 <create_large_chunked_message+0xf4>
mov    -0x38(%rbp),%rdi
add    -0x20(%rbp),%rdi
lea    0xe15(%rip),%rsi
mov    $0x5,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    $0x43,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x20(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d0 <create_large_chunked_message+0x70>
mov    -0x38(%rbp),%rdi
add    -0x20(%rbp),%rdi
lea    0xda3(%rip),%rsi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_large_chunked_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
imul   $0x407,%ebp,%eax
cltq
lea    (%r15,%rax,1),%rcx
mov    %rcx,(%rsp)
lea    (%r15,%rax,1),%rdi
add    $0x6,%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1070 <memcpy@plt>
test   %ebp,%ebp
jle    1215 <create_large_chunked_message+0xb5>
mov    %r15,%rbx
add    %r14,%rbx
lea    0xe46(%rip),%r12
lea    0xe48(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x5,%edx
call   1070 <memcpy@plt>
lea    0x5(%rbx),%rdi
mov    $0x43,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    0x405(%rbx),%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
add    $0x407,%r15
add    $0x407,%rbx
add    $0xffffffff,%ebp
jne    11d0 <create_large_chunked_message+0x70>
mov    %r14,%rdi
add    %r15,%rdi
lea    0xde4(%rip),%rsi
mov    $0x6,%edx
call   1070 <memcpy@plt>
xor    %edi,%edi
cmp    (%rsp),%r15
sete   %dil
call   1060 <assert@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"400\0D\0A\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
@.str.2 = [6 x i8] c"0\0D\0A\0D\0A\00"
define i8* @create_large_chunked_message(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i64 0, i64* %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = load i32, i32* %3
%14 = mul nsw i32 1031, %13
%15 = sext i32 %14 to i64
%16 = add i64 %12, %15
%17 = add i64 %16, 6
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = load i8*, i8** %4
%22 = load i64, i64* %7
%23 = trunc i64 %22 to i32
%24 = call i32 @memcpy(i8* %20, i8* %21, i32 %23)
%25 = load i64, i64* %7
%26 = load i64, i64* %6
%27 = add i64 %26, %25
store i64 %27, i64* %6
store i32 0, i32* %5
br label %28
28: ; preds = %51, %2
%29 = load i32, i32* %5
%30 = load i32, i32* %3
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %54
32: ; preds = %28
%33 = load i8*, i8** %9
%34 = load i64, i64* %6
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = call i32 @memcpy(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%37 = load i64, i64* %6
%38 = add i64 %37, 5
store i64 %38, i64* %6
%39 = load i8*, i8** %9
%40 = load i64, i64* %6
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = call i32 @memset(i8* %41, i8 signext 67, i32 1024)
%43 = load i64, i64* %6
%44 = add i64 %43, 1024
store i64 %44, i64* %6
%45 = load i8*, i8** %9
%46 = load i64, i64* %6
%47 = getelementptr inbounds i8, i8* %45, i64 %46
%48 = call i32 @strcpy(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%49 = load i64, i64* %6
%50 = add i64 %49, 2
store i64 %50, i64* %6
br label %51
51: ; preds = %32
%52 = load i32, i32* %5
%53 = add nsw i32 %52, 1
store i32 %53, i32* %5
br label %28
54: ; preds = %28
%55 = load i8*, i8** %9
%56 = load i64, i64* %6
%57 = getelementptr inbounds i8, i8* %55, i64 %56
%58 = call i32 @memcpy(i8* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 6)
%59 = load i64, i64* %6
%60 = add i64 %59, 6
store i64 %60, i64* %6
%61 = load i64, i64* %6
%62 = load i64, i64* %8
%63 = icmp eq i64 %61, %62
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
%66 = load i8*, i8** %9
ret i8* %66
}

LLVM IR (-O2):
@.str = [6 x i8] c"400\0D\0A\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
@.str.2 = [6 x i8] c"0\0D\0A\0D\0A\00"
define i8* @create_large_chunked_message(i32 %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = mul nsw i32 %0, 1031
%5 = sext i32 %4 to i64
%6 = add i64 %3, %5
%7 = add i64 %6, 6
%8 = tail call i8* @malloc(i64 %7)
%9 = trunc i64 %3 to i32
%10 = tail call i32 @memcpy(i8* %8, i8* %1, i32 %9)
%11 = icmp sgt i32 %0, 0
br i1 %11, label %12, label %26
12: ; preds = %2, %12
%13 = phi i32 [ %24, %12 ], [ 0, %2 ]
%14 = phi i64 [ %23, %12 ], [ %3, %2 ]
%15 = getelementptr inbounds i8, i8* %8, i64 %14
%16 = tail call i32 @memcpy(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%17 = add i64 %14, 5
%18 = getelementptr inbounds i8, i8* %8, i64 %17
%19 = tail call i32 @memset(i8* %18, i8 signext 67, i32 1024)
%20 = add i64 %14, 1029
%21 = getelementptr inbounds i8, i8* %8, i64 %20
%22 = tail call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%23 = add i64 %14, 1031
%24 = add nuw nsw i32 %13, 1
%25 = icmp eq i32 %24, %0
br i1 %25, label %26, label %12
26: ; preds = %12, %2
%27 = phi i64 [ %3, %2 ], [ %23, %12 ]
%28 = getelementptr inbounds i8, i8* %8, i64 %27
%29 = tail call i32 @memcpy(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 6)
%30 = icmp eq i64 %27, %6
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
ret i8* %8
}