#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 *calloc(int, int);
int /*<<< orphan*/ printf(char *, int);
char *strcat(char *, char const *);
int strlen(char const *);
char *genrequest(const char *input, const char *domain, int request) {
  int size = sizeof(char) * (strlen(input) + strlen(domain));
  char *q;
  char *msg = calloc(size + 30, sizeof(char));
  printf("%d", request);
  switch (request) {
  case 10:
    q = "GET ";
    break;
  case 11:
    q = "HEAD ";
    break;
  }
  msg = strcat(msg, q);
  msg = strcat(msg, input);
  msg = strcat(msg, " HTTP/1.1\r\n");
  msg = strcat(msg, "Host: ");
  msg = strcat(msg, domain);
  msg = strcat(msg, "\r\n");
  msg = strcat(msg, "\r\n");
  return msg;
}


x86 assembly (clang, -O0):
<genrequest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
add    $0x1e,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xa,%eax
je     11ba <genrequest+0x7a>
jmp    11a9 <genrequest+0x69>
mov    -0x2c(%rbp),%eax
sub    $0xb,%eax
je     11ca <genrequest+0x8a>
jmp    11d5 <genrequest+0x95>
lea    0xe42(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11d5 <genrequest+0x95>
lea    0xe37(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genrequest>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1e,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
lea    0xe88(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
cmp    $0xa,%ebp
lea    0xe7a(%rip),%rax
lea    0xe78(%rip),%rsi
cmove  %rax,%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movabs $0x2e312f5054544820,%rcx
2f 31 2e
mov    %rcx,(%rbx,%rax,1)
movl   $0xa0d31,0x8(%rbx,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
movl   $0x74736f48,(%rbx,%rax,1)
movl   $0x203a74,0x3(%rbx,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movw   $0xa0d,(%rbx,%rax,1)
movb   $0x0,0x2(%rbx,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
movw   $0xa0d,(%rbx,%rax,1)
movb   $0x0,0x2(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"GET \00"
@.str.2 = [6 x i8] c"HEAD \00"
@.str.3 = [12 x i8] c" HTTP/1.1\0D\0A\00"
@.str.4 = [7 x i8] c"Host: \00"
@.str.5 = [3 x i8] c"\0D\0A\00"
define i8* @genrequest(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %11, %13
%15 = sext i32 %14 to i64
%16 = mul i64 1, %15
%17 = trunc i64 %16 to i32
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 30
%20 = call i8* @calloc(i32 %19, i32 1)
store i8* %20, i8** %9
%21 = load i32, i32* %6
%22 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %6
switch i32 %23, label %26 [
i32 10, label %24
i32 11, label %25
]
24: ; preds = %3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %8
br label %26
25: ; preds = %3
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %8
br label %26
26: ; preds = %3, %25, %24
%27 = load i8*, i8** %9
%28 = load i8*, i8** %8
%29 = call i8* @strcat(i8* %27, i8* %28)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = load i8*, i8** %4
%32 = call i8* @strcat(i8* %30, i8* %31)
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = call i8* @strcat(i8* %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = call i8* @strcat(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i8* %36, i8** %9
%37 = load i8*, i8** %9
%38 = load i8*, i8** %5
%39 = call i8* @strcat(i8* %37, i8* %38)
store i8* %39, i8** %9
%40 = load i8*, i8** %9
%41 = call i8* @strcat(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i8* %41, i8** %9
%42 = load i8*, i8** %9
%43 = call i8* @strcat(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i8* %43, i8** %9
%44 = load i8*, i8** %9
ret i8* %44
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"GET \00"
@.str.2 = [6 x i8] c"HEAD \00"
@.str.3 = [12 x i8] c" HTTP/1.1\0D\0A\00"
@.str.4 = [7 x i8] c"Host: \00"
@.str.5 = [3 x i8] c"\0D\0A\00"
define i8* @genrequest(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = add i32 %4, 30
%7 = add i32 %6, %5
%8 = tail call i8* @calloc(i32 %7, i32 1)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %2)
%10 = icmp eq i32 %2, 10
%11 = select i1 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0)
%12 = tail call i8* @strcat(i8* dereferenceable(1) %8, i8* dereferenceable(1) %11)
%13 = tail call i8* @strcat(i8* dereferenceable(1) %8, i8* dereferenceable(1) %0)
%14 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %8)
%15 = getelementptr i8, i8* %8, i64 %14
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(12) %15, i8* align 1 dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i64 12, i1 false)
%16 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %8)
%17 = getelementptr i8, i8* %8, i64 %16
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(7) %17, i8* align 1 dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i64 7, i1 false)
%18 = tail call i8* @strcat(i8* dereferenceable(1) %8, i8* dereferenceable(1) %1)
%19 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %8)
%20 = getelementptr i8, i8* %8, i64 %19
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %20, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 3, i1 false)
%21 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %8)
%22 = getelementptr i8, i8* %8, i64 %21
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %22, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 3, i1 false)
ret i8* %8
}