#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 */
struct fd_state {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char const *, char const *, size_t,
                            char const *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write_to_buf(struct fd_state *, char *, int /*<<< orphan*/);
int write_response_header(struct fd_state *state, const char *mime,
                          size_t body_len, const char *user_header) {
  assert(state && user_header && mime);
  char buf[2048] = {0};
  const char *fmt = "HTTP/1.1 200 OK\r\n"
                    "Content-Type: %s\r\n"
                    "Content-Length: %u\r\n"
                    "%s\r\n";
  snprintf(buf, sizeof(buf), fmt, mime, body_len, user_header);
  write_to_buf(state, buf, strlen(buf));
  return 0;
}


x86 assembly (clang, -O0):
<write_response_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x829(%rbp)
je     119f <write_response_header+0x4f>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x829(%rbp)
je     119f <write_response_header+0x4f>
cmpq   $0x0,-0x10(%rbp)
setne  %al
mov    %al,-0x829(%rbp)
mov    -0x829(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
lea    0xe37(%rip),%rax
mov    %rax,-0x828(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x800,%esi
call   1040 <snprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x840(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x820(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x840(%rbp),%rdi
mov    -0x838(%rbp),%rsi
mov    %eax,%edx
call   1070 <write_to_buf@plt>
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_response_header>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rdi,%rdi
setne  %al
test   %rcx,%rcx
setne  %cl
and    %al,%cl
test   %rsi,%rsi
setne  %al
and    %cl,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    %rsp,%r13
mov    $0x800,%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe5d(%rip),%rdx
mov    %r13,%rdi
mov    $0x800,%esi
mov    %rbx,%rcx
mov    %r15,%r8
mov    %r12,%r9
call   1040 <snprintf@plt>
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1070 <write_to_buf@plt>
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.fd_state = type { i32 }
@.str = [60 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: %s\0D\0AContent-Length: %u\0D\0A%s\0D\0A\00"
define i32 @write_response_header(%struct.fd_state* %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca %struct.fd_state*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca [2048 x i8]
%10 = alloca i8*
store %struct.fd_state* %0, %struct.fd_state** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8* %3, i8** %8
%11 = load %struct.fd_state*, %struct.fd_state** %5
%12 = icmp ne %struct.fd_state* %11, null
br i1 %12, label %13, label %19
13: ; preds = %4
%14 = load i8*, i8** %8
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br label %19
19: ; preds = %16, %13, %4
%20 = phi i1 [ false, %13 ], [ false, %4 ], [ %18, %16 ]
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = bitcast [2048 x i8]* %9 to i8*
store i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8** %10
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%25 = load i8*, i8** %10
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = load i8*, i8** %8
%29 = call i32 @snprintf(i8* %24, i32 2048, i8* %25, i8* %26, i64 %27, i8* %28)
%30 = load %struct.fd_state*, %struct.fd_state** %5
%31 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @write_to_buf(%struct.fd_state* %30, i8* %31, i32 %33)
ret i32 0
}

LLVM IR (-O2):
%struct.fd_state = type { i32 }
@.str = [60 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: %s\0D\0AContent-Length: %u\0D\0A%s\0D\0A\00"
define i32 @write_response_header(%struct.fd_state* %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca [2048 x i8]
%6 = icmp ne %struct.fd_state* %0, null
%7 = icmp ne i8* %3, null
%8 = and i1 %6, %7
%9 = icmp ne i8* %1, null
%10 = and i1 %9, %8
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%14 = call i32 @snprintf(i8* %13, i32 2048, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %1, i64 %2, i8* %3)
%15 = call i32 @strlen(i8* %13)
%16 = call i32 @write_to_buf(%struct.fd_state* %0, i8* %13, i32 %15)
ret i32 0
}