#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*/ bzero(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, ...);
unsigned short rand();
int strlen(char *);
char *strtok(char *, char *);
char *build_request_data(char *hostname, int *ret_size) {
  int size = 12 + strlen(hostname) + 1 + 1 + 4;
  char *buffer = (char *)malloc(size);
  char *pbuf = buffer;
  bzero(buffer, size);
  unsigned short seq = rand();
  char header[] = {0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  memcpy(pbuf, &seq, 2);
  pbuf += 2;
  memcpy(pbuf, header, 10);
  pbuf += 10;
  char *pstr = strtok(hostname, ".");
  while (pstr != NULL) {
    char len = strlen(pstr);
    memcpy(pbuf, &len, 1);
    pbuf += 1;
    memcpy(pbuf, pstr, len);
    pstr = strtok(NULL, ".");
    pbuf += len;
  }
  pbuf += 1;
  char extra_data[] = {0x00, 0x01, 0x00, 0x01};
  memcpy(pbuf, extra_data, 4);
  *ret_size = size;
  return buffer;
}


x86 assembly (clang, -O0):
<build_request_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0xc,%eax
add    $0x1,%eax
add    $0x1,%eax
add    $0x4,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <bzero@plt>
mov    $0x0,%al
call   1080 <rand@plt>
mov    %ax,-0x2a(%rbp)
mov    0xe46(%rip),%rax
mov    %rax,-0x34(%rbp)
mov    0xe43(%rip),%ax
mov    %ax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
lea    -0x2a(%rbp),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    $0xa,%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1292 <build_request_data+0x132>
mov    -0x40(%rbp),%rdi
call   1030 <strlen@plt>
mov    %al,-0x41(%rbp)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
movsbl -0x41(%rbp),%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd98(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x40(%rbp)
movsbl -0x41(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    121d <build_request_data+0xbd>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    0xd68(%rip),%eax
mov    %eax,-0x45(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x45(%rbp),%rsi
xor    %eax,%eax
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_request_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,0x20(%rsp)
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %eax,%r15d
add    $0x12,%r15d
mov    %r15d,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15d,%esi
call   1070 <bzero@plt>
xor    %eax,%eax
call   1080 <rand@plt>
mov    %ax,0xe(%rsp)
movw   $0x0,0x18(%rsp)
movq   $0x1000001,0x10(%rsp)
lea    0xe(%rsp),%rsi
mov    %r13,%rdi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
lea    0x2(%r13),%rdi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    %r13,%rbx
add    $0xc,%rbx
lea    0xe1f(%rip),%rsi
mov    %r12,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     1260 <build_request_data+0x100>
mov    %rax,%r14
lea    0x8(%rsp),%r12
lea    0xe03(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %al,0x8(%rsp)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
lea    0x1(%rbx),%rdi
movsbl 0x8(%rsp),%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <memcpy@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1060 <strtok@plt>
mov    %rax,%r14
movsbq 0x8(%rsp),%rax
add    %rax,%rbx
add    $0x1,%rbx
test   %r14,%r14
jne    1210 <build_request_data+0xb0>
add    $0x1,%rbx
movl   $0x1000100,0x8(%rsp)
01
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    0x20(%rsp),%rax
mov    %r15d,(%rax)
mov    %r13,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.build_request_data.header = [10 x i8] c"\01\00\00\01\00\00\00\00\00\00"
@.str = [2 x i8] c".\00"
@__const.build_request_data.extra_data = [4 x i8] c"\00\01\00\01"
define i8* @build_request_data(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i16
%9 = alloca [10 x i8]
%10 = alloca i8*
%11 = alloca i8
%12 = alloca [4 x i8]
store i8* %0, i8** %3
store i32* %1, i32** %4
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 12, %14
%16 = add nsw i32 %15, 1
%17 = add nsw i32 %16, 1
%18 = add nsw i32 %17, 4
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %6
%22 = load i8*, i8** %6
store i8* %22, i8** %7
%23 = load i8*, i8** %6
%24 = load i32, i32* %5
%25 = call i32 @bzero(i8* %23, i32 %24)
%26 = call zeroext i16 (...) @rand()
store i16 %26, i16* %8
%27 = bitcast [10 x i8]* %9 to i8*
%28 = load i8*, i8** %7
%29 = call i32 (i8*, ...) @memcpy(i8* %28, i16* %8, i32 2)
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 2
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%34 = call i32 (i8*, ...) @memcpy(i8* %32, i8* %33, i32 10)
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i64 10
store i8* %36, i8** %7
%37 = load i8*, i8** %3
%38 = call i8* @strtok(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %38, i8** %10
br label %39
39: ; preds = %42, %2
%40 = load i8*, i8** %10
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %61
42: ; preds = %39
%43 = load i8*, i8** %10
%44 = call i32 @strlen(i8* %43)
%45 = trunc i32 %44 to i8
store i8 %45, i8* %11
%46 = load i8*, i8** %7
%47 = call i32 (i8*, ...) @memcpy(i8* %46, i8* %11, i32 1)
%48 = load i8*, i8** %7
%49 = getelementptr inbounds i8, i8* %48, i64 1
store i8* %49, i8** %7
%50 = load i8*, i8** %7
%51 = load i8*, i8** %10
%52 = load i8, i8* %11
%53 = sext i8 %52 to i32
%54 = call i32 (i8*, ...) @memcpy(i8* %50, i8* %51, i32 %53)
%55 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %55, i8** %10
%56 = load i8, i8* %11
%57 = sext i8 %56 to i32
%58 = load i8*, i8** %7
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i8, i8* %58, i64 %59
store i8* %60, i8** %7
br label %39
61: ; preds = %39
%62 = load i8*, i8** %7
%63 = getelementptr inbounds i8, i8* %62, i64 1
store i8* %63, i8** %7
%64 = bitcast [4 x i8]* %12 to i8*
%65 = load i8*, i8** %7
%66 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%67 = call i32 (i8*, ...) @memcpy(i8* %65, i8* %66, i32 4)
%68 = load i32, i32* %5
%69 = load i32*, i32** %4
store i32 %68, i32* %69
%70 = load i8*, i8** %6
ret i8* %70
}

LLVM IR (-O2):
@__const.build_request_data.header = [10 x i8] c"\01\00\00\01\00\00\00\00\00\00"
@.str = [2 x i8] c".\00"
define i8* @build_request_data(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i16
%4 = alloca [10 x i8]
%5 = alloca i8
%6 = alloca i32
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, 18
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @bzero(i8* %10, i32 %8)
%12 = bitcast i16* %3 to i8*
%13 = tail call zeroext i16 (...) @rand()
store i16 %13, i16* %3
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%15 = call i32 (i8*, ...) @memcpy(i8* %10, i16* %3, i32 2)
%16 = getelementptr inbounds i8, i8* %10, i64 2
%17 = call i32 (i8*, ...) @memcpy(i8* %16, i8* %14, i32 10)
%18 = getelementptr inbounds i8, i8* %10, i64 12
%19 = call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %36, label %21
21: ; preds = %2, %21
%22 = phi i8* [ %31, %21 ], [ %19, %2 ]
%23 = phi i8* [ %34, %21 ], [ %18, %2 ]
%24 = call i32 @strlen(i8* %22)
%25 = trunc i32 %24 to i8
store i8 %25, i8* %5
%26 = call i32 (i8*, ...) @memcpy(i8* %23, i8* %5, i32 1)
%27 = getelementptr inbounds i8, i8* %23, i64 1
%28 = load i8, i8* %5
%29 = sext i8 %28 to i32
%30 = call i32 (i8*, ...) @memcpy(i8* %27, i8* %22, i32 %29)
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%32 = load i8, i8* %5
%33 = sext i8 %32 to i64
%34 = getelementptr inbounds i8, i8* %27, i64 %33
%35 = icmp eq i8* %31, null
br i1 %35, label %36, label %21
36: ; preds = %21, %2
%37 = phi i8* [ %18, %2 ], [ %34, %21 ]
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = bitcast i32* %6 to i8*
store i32 16777472, i32* %6
%40 = call i32 (i8*, ...) @memcpy(i8* %38, i32* %6, i32 4)
store i32 %8, i32* %1
ret i8* %10
}