#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *);
int strlen(char *);
char *build_get_query(char *path, char *protocol, char *host) {
  char *tpl = "GET %s %s\r\nHost: %s\r\n\r\n";
  int query_len;
  char *query;
  query_len = strlen(tpl) - 6 + strlen(host) + strlen(path) + strlen(protocol);
  query = (char *)malloc(query_len);
  memset(query, 0, query_len);
  sprintf(query, tpl, path, protocol, host);
  return query;
}


x86 assembly (clang, -O0):
<build_get_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xea5(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x6,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
call   1060 <sprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_get_query>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
lea    0xea5(%rip),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
add    %ebp,%ebx
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
add    %ebx,%ebp
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rbp,1),%ebx
add    $0xfffffffa,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <memset@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1060 <sprintf@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"GET %s %s\0D\0AHost: %s\0D\0A\0D\0A\00"
define i8* @build_get_query(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8** %7
%10 = load i8*, i8** %7
%11 = call i32 @strlen(i8* %10)
%12 = sub nsw i32 %11, 6
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %12, %14
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %15, %17
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
%21 = add nsw i32 %18, %20
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i32, i32* %8
%27 = call i32 @memset(i8* %25, i32 0, i32 %26)
%28 = load i8*, i8** %9
%29 = load i8*, i8** %7
%30 = load i8*, i8** %4
%31 = load i8*, i8** %5
%32 = load i8*, i8** %6
%33 = call i32 @sprintf(i8* %28, i8* %29, i8* %30, i8* %31, i8* %32)
%34 = load i8*, i8** %9
ret i8* %34
}

LLVM IR (-O2):
@.str = [24 x i8] c"GET %s %s\0D\0AHost: %s\0D\0A\0D\0A\00"
define i8* @build_get_query(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%5 = add nsw i32 %4, -6
%6 = tail call i32 @strlen(i8* %2)
%7 = add nsw i32 %5, %6
%8 = tail call i32 @strlen(i8* %0)
%9 = add nsw i32 %7, %8
%10 = tail call i32 @strlen(i8* %1)
%11 = add nsw i32 %9, %10
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 @memset(i8* %13, i32 0, i32 %11)
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %2)
ret i8* %13
}