#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(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *);
scalar_t__ strlen(char *);
char *build_get_query(char *host, char *page) {
  char *query;
  char *getpage = page;
  char *tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n";
  if (getpage[0] == '/') {
    getpage = getpage + 1;
  }
  query = (char *)malloc(strlen(host) + strlen(getpage) +
                         strlen("HTMLGET 1.0") + strlen(tpl) - 5);
  sprintf(query, tpl, getpage, host, "HTMLGET 1.0");
  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    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0xeb1(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    116f <build_get_query+0x3f>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
lea    0xe95(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
sub    $0x5,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe4e(%rip),%r8
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_get_query>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
cmpb   $0x2f,(%rsi)
sete   %bl
add    %rsi,%rbx
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%rdi
add    $0x34,%rdi
call   1040 <malloc@plt>
mov    %rax,%r15
lea    0xe96(%rip),%rsi
lea    0xebe(%rip),%r8
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [47 x i8] c"GET /%s HTTP/1.0\0D\0AHost: %s\0D\0AUser-Agent: %s\0D\0A\0D\0A\00"
@.str.1 = [12 x i8] c"HTMLGET 1.0\00"
define i8* @build_get_query(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 47
%5 = zext i1 %4 to i64
%6 = getelementptr i8, i8* %1, i64 %5
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = tail call i64 @strlen(i8* %6)
%9 = add i64 %7, 52
%10 = add i64 %9, %8
%11 = tail call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
ret i8* %12
}