#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 */
char *malloc(int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char const *, char *);
char *get_host(const char *buffer) {
  char *host_start = strstr(buffer, "\r\nHOST: ") + strlen("\r\nHOST: ");
  char *host_end = strstr(host_start, ":");
  char *host = malloc((host_end - host_start + 1));
  strncpy(host, host_start, host_end - host_start);
  host[host_end - host_start] = '\0';
  return host;
}


x86 assembly (clang, -O0):
<get_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
lea    0xe99(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_host>:
push   %r15
push   %r14
push   %rbx
lea    0xea4(%rip),%r14
mov    %r14,%rsi
call   1070 <strstr@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%r14
add    %r15,%r14
mov    %r14,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
sub    %r14,%rbx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"\0D\0AHOST: \00"
@.str.1 = [2 x i8] c":\00"
define i8* @get_host(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @strstr(i8* %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %7, i64 %9
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = call i8* @strstr(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = load i8*, i8** %3
%15 = ptrtoint i8* %13 to i64
%16 = ptrtoint i8* %14 to i64
%17 = sub i64 %15, %16
%18 = add nsw i64 %17, 1
%19 = trunc i64 %18 to i32
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = load i8*, i8** %3
%23 = load i8*, i8** %4
%24 = load i8*, i8** %3
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
%28 = trunc i64 %27 to i32
%29 = call i32 @strncpy(i8* %21, i8* %22, i32 %28)
%30 = load i8*, i8** %5
%31 = load i8*, i8** %4
%32 = load i8*, i8** %3
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = getelementptr inbounds i8, i8* %30, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %5
ret i8* %37
}

LLVM IR (-O2):
@.str = [9 x i8] c"\0D\0AHOST: \00"
define i8* @get_host(i8* %0) {
%2 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %2, i64 %4
%6 = tail call i8* @strchr(i8* dereferenceable(1) %5, i32 58)
%7 = ptrtoint i8* %6 to i64
%8 = ptrtoint i8* %5 to i64
%9 = sub i64 %7, %8
%10 = trunc i64 %9 to i32
%11 = add i32 %10, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i32 @strncpy(i8* %12, i8* %5, i32 %10)
%14 = getelementptr inbounds i8, i8* %12, i64 %9
store i8 0, i8* %14
ret i8* %12
}