#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 */
int /*<<< orphan*/ debugPrint(char *);
int /*<<< orphan*/ debugPrintStr(char *, char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
char *strcat(char *, char *);
int strlen(char *);
char *generateHTTPHeader(char *page, char *host, int port) {
  char *request = 0;
  int request_len;
  char *reqType = "GET ";
  char *reqHeader = " HTTP/1.0\r\nUser-Agent:EKRClient\r\nHost: %s:%d\r\n\r\n";
  char *requestTemplate =
      malloc(strlen(reqType) + strlen(page) + strlen(reqHeader));
  requestTemplate = strcat(requestTemplate, reqType);
  requestTemplate = strcat(requestTemplate, page);
  requestTemplate = strcat(requestTemplate, reqHeader);
  request_len = strlen(requestTemplate) + strlen(page) + 6;
  if (!(request = (char *)malloc(request_len))) {
    debugPrint("connection : generateHTTPHeader : memory allocation error ");
  }
  memset(request, 0, request_len);
  snprintf(request, request_len, requestTemplate, host, port);
  debugPrintStr("connection : generateHTTPHeader : request : ", request);
  return request;
}


x86 assembly (clang, -O0):
<generateHTTPHeader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe6e(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe68(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%edi
add    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <strcat@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1090 <strcat@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <strcat@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
add    $0x6,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    124e <generateHTTPHeader+0xde>
lea    0xded(%rip),%rdi
call   1080 <debugPrint@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdf2(%rip),%rdi
call   1030 <debugPrintStr@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generateHTTPHeader>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe78(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
lea    0xe65(%rip),%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
movl   $0x20544547,0x0(%rbp,%rax,1)
20
movb   $0x0,0x4(%rbp,%rax,1)
mov    %rbp,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
movups 0xe25(%rip),%xmm0
movups %xmm0,0x0(%rbp,%rax,1)
movups 0xe29(%rip),%xmm0
movups %xmm0,0x10(%rbp,%rax,1)
movups 0xe2d(%rip),%xmm0
movups %xmm0,0x20(%rbp,%rax,1)
movb   $0x0,0x30(%rbp,%rax,1)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%r12d
add    $0x6,%r12d
mov    %r12d,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1238 <generateHTTPHeader+0xc8>
lea    0xe03(%rip),%rdi
call   1080 <debugPrint@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %rbp,%rdx
mov    %r15,%rcx
mov    %r14d,%r8d
call   1050 <snprintf@plt>
lea    0xe11(%rip),%rdi
mov    %rbx,%rsi
call   1030 <debugPrintStr@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"GET \00"
@.str.1 = [49 x i8] c" HTTP/1.0\0D\0AUser-Agent:EKRClient\0D\0AHost: %s:%d\0D\0A\0D\0A\00"
@.str.2 = [59 x i8] c"connection : generateHTTPHeader : memory allocation error \00"
@.str.3 = [45 x i8] c"connection : generateHTTPHeader : request : \00"
define i8* @generateHTTPHeader(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i8* null, i8** %7
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %9
store i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8** %10
%12 = load i8*, i8** %9
%13 = call i32 @strlen(i8* %12)
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %13, %15
%17 = load i8*, i8** %10
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %16, %18
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %11
%21 = load i8*, i8** %11
%22 = load i8*, i8** %9
%23 = call i8* @strcat(i8* %21, i8* %22)
store i8* %23, i8** %11
%24 = load i8*, i8** %11
%25 = load i8*, i8** %4
%26 = call i8* @strcat(i8* %24, i8* %25)
store i8* %26, i8** %11
%27 = load i8*, i8** %11
%28 = load i8*, i8** %10
%29 = call i8* @strcat(i8* %27, i8* %28)
store i8* %29, i8** %11
%30 = load i8*, i8** %11
%31 = call i32 @strlen(i8* %30)
%32 = load i8*, i8** %4
%33 = call i32 @strlen(i8* %32)
%34 = add nsw i32 %31, %33
%35 = add nsw i32 %34, 6
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = call i8* @malloc(i32 %36)
store i8* %37, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %41, label %39
39: ; preds = %3
%40 = call i32 @debugPrint(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %3
%42 = load i8*, i8** %7
%43 = load i32, i32* %8
%44 = call i32 @memset(i8* %42, i32 0, i32 %43)
%45 = load i8*, i8** %7
%46 = load i32, i32* %8
%47 = load i8*, i8** %11
%48 = load i8*, i8** %5
%49 = load i32, i32* %6
%50 = call i32 @snprintf(i8* %45, i32 %46, i8* %47, i8* %48, i32 %49)
%51 = load i8*, i8** %7
%52 = call i32 @debugPrintStr(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = load i8*, i8** %7
ret i8* %53
}

LLVM IR (-O2):
@.str = [5 x i8] c"GET \00"
@.str.1 = [49 x i8] c" HTTP/1.0\0D\0AUser-Agent:EKRClient\0D\0AHost: %s:%d\0D\0A\0D\0A\00"
@.str.2 = [59 x i8] c"connection : generateHTTPHeader : memory allocation error \00"
@.str.3 = [45 x i8] c"connection : generateHTTPHeader : request : \00"
define i8* @generateHTTPHeader(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, %4
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%8 = add nsw i32 %6, %7
%9 = tail call i8* @malloc(i32 %8)
%10 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %9)
%11 = getelementptr i8, i8* %9, i64 %10
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(5) %11, i8* align 1 dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 5, i1 false)
%12 = tail call i8* @strcat(i8* dereferenceable(1) %9, i8* dereferenceable(1) %0)
%13 = tail call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %9)
%14 = getelementptr i8, i8* %9, i64 %13
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(49) %14, i8* align 1 dereferenceable(49) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i64 49, i1 false)
%15 = tail call i32 @strlen(i8* %9)
%16 = tail call i32 @strlen(i8* %0)
%17 = add i32 %15, 6
%18 = add i32 %17, %16
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = tail call i32 @debugPrint(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %3
%24 = tail call i32 @memset(i8* %19, i32 0, i32 %18)
%25 = tail call i32 @snprintf(i8* %19, i32 %18, i8* %9, i8* %1, i32 %2)
%26 = tail call i32 @debugPrintStr(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %19)
ret i8* %19
}