void prepare_header(char **header, int *header_length, int body_length) {
  char tmp[30];
  assert(header);
  assert(header_length);
  assert(body_length >= 0);
  *header_length = 23 + sprintf(tmp, "%d", body_length);
  *header = xmalloc(*header_length + 30);
  sprintf(*header, "%d:CONTENT_LENGTH\n%d\nSCGI\n1\n,", *header_length,
          body_length);
  *header_length += 2 + sprintf(tmp, "%d", *header_length);
  for (int i = 0; i < *header_length; ++i)
    if ((*header)[i] == '\n')
      (*header)[i] = '\x00';
}


x86 assembly (clang, -O0):
<prepare_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1040 <assert@plt>
cmpl   $0x0,-0x14(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    %eax,%ecx
add    $0x17,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
add    $0x1e,%edi
mov    $0x0,%al
call   1030 <xmalloc@plt>
movslq %eax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x14(%rbp),%ecx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    %eax,%ecx
add    $0x2,%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    1239 <prepare_header+0x109>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x44(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1226 <prepare_header+0xf6>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    122b <prepare_header+0xfb>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11f0 <prepare_header+0xc0>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_header>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <assert@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <assert@plt>
mov    %ebp,%edi
not    %edi
shr    $0x1f,%edi
xor    %eax,%eax
call   1040 <assert@plt>
lea    0xe96(%rip),%r15
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <sprintf@plt>
lea    0x17(%rax),%ecx
mov    %ecx,(%r14)
lea    0x35(%rax),%edi
xor    %eax,%eax
call   1030 <xmalloc@plt>
movslq %eax,%rdi
mov    %rdi,(%rbx)
mov    (%r14),%edx
lea    0xe67(%rip),%rsi
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    (%r14),%edx
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    (%r14),%ecx
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,(%r14)
test   %eax,%eax
jle    11ee <prepare_header+0xbe>
xor    %ecx,%ecx
jmp    11dc <prepare_header+0xac>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rcx
movslq %eax,%rdx
cmp    %rdx,%rcx
jge    11ee <prepare_header+0xbe>
mov    (%rbx),%rdx
cmpb   $0xa,(%rdx,%rcx,1)
jne    11d0 <prepare_header+0xa0>
movb   $0x0,(%rdx,%rcx,1)
mov    (%r14),%eax
jmp    11d0 <prepare_header+0xa0>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [30 x i8] c"%d:CONTENT_LENGTH\0A%d\0ASCGI\0A1\0A,\00"
define void @prepare_header(i8** %0, i32* %1, i32 %2) {
%4 = alloca [30 x i8]
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%6 = tail call i32 (i8**, ...) bitcast (i32 (...)* @assert to i32 (i8**, ...)*)(i8** %0)
%7 = tail call i32 (i32*, ...) bitcast (i32 (...)* @assert to i32 (i32*, ...)*)(i32* %1)
%8 = xor i32 %2, -1
%9 = lshr i32 %8, 31
%10 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %9)
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %2)
%12 = add nsw i32 %11, 23
store i32 %12, i32* %1
%13 = add nsw i32 %11, 53
%14 = tail call i32 (i32, ...) bitcast (i32 (...)* @xmalloc to i32 (i32, ...)*)(i32 %13)
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %0
%17 = load i32, i32* %1
%18 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %2)
%19 = load i32, i32* %1
%20 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = add nsw i32 %20, 2
%22 = load i32, i32* %1
%23 = add nsw i32 %21, %22
store i32 %23, i32* %1
%24 = icmp sgt i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %35, %3
ret void
26: ; preds = %3, %35
%27 = phi i32 [ %36, %35 ], [ %23, %3 ]
%28 = phi i64 [ %37, %35 ], [ 0, %3 ]
%29 = load i8*, i8** %0
%30 = getelementptr inbounds i8, i8* %29, i64 %28
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 10
br i1 %32, label %33, label %35
33: ; preds = %26
store i8 0, i8* %30
%34 = load i32, i32* %1
br label %35
35: ; preds = %26, %33
%36 = phi i32 [ %27, %26 ], [ %34, %33 ]
%37 = add nuw nsw i64 %28, 1
%38 = sext i32 %36 to i64
%39 = icmp slt i64 %37, %38
br i1 %39, label %26, label %25
}