#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 */
typedef scalar_t__ off_t;

/* Variables and functions */
char *content_length_line(scalar_t__);
int /*<<< orphan*/ free(char *);
char *malloc(long);
scalar_t__ numlength(scalar_t__);
int send(int, char *, scalar_t__, int /*<<< orphan*/);
int sprintf(char *, char *, char *, char *, char *);
scalar_t__ strlen(char *);
int respond_with_string(int connfd, char *string) {
  int rval = 0;
  long message_size;
  char *message;
  char *status;
  char *content_length;
  off_t filesize;
  message_size = 0;
  status = "HTTP/1.1 200 OK\n";
  message_size += strlen(status);
  filesize = strlen(string);
  message_size += filesize;
  message_size += numlength(filesize);
  if ((content_length = content_length_line(filesize)) == NULL) {
    return -1;
  }
  message_size += strlen(content_length);
  message = malloc(message_size * sizeof(char));
  if (sprintf(message, "%s%s\n%s", status, content_length, string) == -1) {
    rval = -1;
    goto out;
  }
  if (send(connfd, message, strlen(message), 0) == -1) {
    rval = -1;
    goto out;
  }
out:
  free(message);
  free(content_length);
  return rval;
}


x86 assembly (clang, -O0):
<respond_with_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe6b(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <numlength@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x40(%rbp),%rdi
call   1090 <content_length_line@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11f7 <respond_with_string+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <respond_with_string+0x134>
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xde1(%rip),%rsi
call   1080 <sprintf@plt>
cmp    $0xffffffff,%eax
jne    124a <respond_with_string+0xda>
movl   $0xffffffff,-0x14(%rbp)
jmp    128c <respond_with_string+0x11c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    1287 <respond_with_string+0x117>
movl   $0xffffffff,-0x14(%rbp)
jmp    128c <respond_with_string+0x11c>
jmp    128c <respond_with_string+0x11c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<respond_with_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <numlength@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1090 <content_length_line@plt>
test   %rax,%rax
je     1208 <respond_with_string+0x98>
mov    %rax,%r13
add    %rbx,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x10,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
lea    0xe48(%rip),%rsi
lea    0xe30(%rip),%rdx
mov    %rax,%rdi
mov    %r13,%rcx
mov    %r12,%r8
call   1080 <sprintf@plt>
cmp    $0xffffffff,%eax
je     120f <respond_with_string+0x9f>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
jmp    1214 <respond_with_string+0xa4>
mov    $0xffffffff,%ebp
jmp    1224 <respond_with_string+0xb4>
mov    $0xffffffff,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
@.str.1 = [8 x i8] c"%s%s\0A%s\00"
define i32 @respond_with_string(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i64 0, i64* %7
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %9
%12 = load i8*, i8** %9
%13 = call i64 @strlen(i8* %12)
%14 = load i64, i64* %7
%15 = add nsw i64 %14, %13
store i64 %15, i64* %7
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %11
%18 = load i64, i64* %11
%19 = load i64, i64* %7
%20 = add nsw i64 %19, %18
store i64 %20, i64* %7
%21 = load i64, i64* %11
%22 = call i64 @numlength(i64 %21)
%23 = load i64, i64* %7
%24 = add nsw i64 %23, %22
store i64 %24, i64* %7
%25 = load i64, i64* %11
%26 = call i8* @content_length_line(i64 %25)
store i8* %26, i8** %10
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %2
store i32 -1, i32* %3
br label %59
29: ; preds = %2
%30 = load i8*, i8** %10
%31 = call i64 @strlen(i8* %30)
%32 = load i64, i64* %7
%33 = add nsw i64 %32, %31
store i64 %33, i64* %7
%34 = load i64, i64* %7
%35 = mul i64 %34, 1
%36 = call i8* @malloc(i64 %35)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = load i8*, i8** %9
%39 = load i8*, i8** %10
%40 = load i8*, i8** %5
%41 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %39, i8* %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %29
store i32 -1, i32* %6
br label %53
44: ; preds = %29
%45 = load i32, i32* %4
%46 = load i8*, i8** %8
%47 = load i8*, i8** %8
%48 = call i64 @strlen(i8* %47)
%49 = call i32 @send(i32 %45, i8* %46, i64 %48, i32 0)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %52
51: ; preds = %44
store i32 -1, i32* %6
br label %53
52: ; preds = %44
br label %53
53: ; preds = %52, %51, %43
%54 = load i8*, i8** %8
%55 = call i32 @free(i8* %54)
%56 = load i8*, i8** %10
%57 = call i32 @free(i8* %56)
%58 = load i32, i32* %6
store i32 %58, i32* %3
br label %59
59: ; preds = %53, %28
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
@.str.1 = [8 x i8] c"%s%s\0A%s\00"
define i32 @respond_with_string(i32 %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i64 @numlength(i64 %3)
%5 = tail call i8* @content_length_line(i64 %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = add nsw i64 %3, 16
%9 = add nsw i64 %8, %4
%10 = tail call i64 @strlen(i8* %5)
%11 = add nsw i64 %9, %10
%12 = tail call i8* @malloc(i64 %11)
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %20, label %15
15: ; preds = %7
%16 = tail call i64 @strlen(i8* dereferenceable(1) %12)
%17 = tail call i32 @send(i32 %0, i8* %12, i64 %16, i32 0)
%18 = icmp eq i32 %17, -1
%19 = sext i1 %18 to i32
br label %20
20: ; preds = %15, %7
%21 = phi i32 [ -1, %7 ], [ %19, %15 ]
%22 = tail call i32 @free(i8* %12)
%23 = tail call i32 @free(i8* %5)
br label %24
24: ; preds = %2, %20
%25 = phi i32 [ %21, %20 ], [ -1, %2 ]
ret i32 %25
}