#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 */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ port;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
int build_socks4a_resolve_request(char **out, const char *username,
                                  const char *hostname) {
  size_t len;
  uint16_t port = htons(0);
  uint32_t addr = htonl(0x00000001u);
  len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
  *out = malloc(len);
  (*out)[0] = 4;
  (*out)[1] = '\xF0';
  memcpy((*out) + 2, &port, sizeof(port));
  memcpy((*out) + 4, &addr, sizeof(addr));
  strcpy((*out) + 8, username);
  strcpy((*out) + 8 + strlen(username) + 1, hostname);
  return (int)len;
}


x86 assembly (clang, -O0):
<build_socks4a_resolve_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %edi,%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    $0x1,%edi
call   1060 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x8,%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movb   $0x4,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movb   $0xf0,0x1(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    $0x2,%rdi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    $0x4,%rdi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    $0x8,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_socks4a_resolve_request>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
xor    %edi,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    $0x1,%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%r12
add    $0xa,%r12
mov    %r12,%rdi
call   1080 <malloc@plt>
mov    %rax,0x0(%r13)
movw   $0xf004,(%rax)
lea    0x2(%rax),%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x0(%r13),%rdi
add    $0x4,%rdi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x0(%r13),%rdi
add    $0x8,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x0(%r13),%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x9,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @build_socks4a_resolve_request(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = call i32 @htons(i32 0)
store i32 %10, i32* %8
%11 = call i32 @htonl(i32 1)
store i32 %11, i32* %9
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
%14 = add i64 8, %13
%15 = add i64 %14, 1
%16 = load i8*, i8** %6
%17 = call i64 @strlen(i8* %16)
%18 = add i64 %15, %17
%19 = add i64 %18, 1
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = call i8* @malloc(i64 %20)
%22 = load i8**, i8*** %4
store i8* %21, i8** %22
%23 = load i8**, i8*** %4
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8, i8* %24, i64 0
store i8 4, i8* %25
%26 = load i8**, i8*** %4
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8 -16, i8* %28
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 2
%32 = call i32 @memcpy(i8* %31, i32* %8, i32 4)
%33 = load i8**, i8*** %4
%34 = load i8*, i8** %33
%35 = getelementptr inbounds i8, i8* %34, i64 4
%36 = call i32 @memcpy(i8* %35, i32* %9, i32 4)
%37 = load i8**, i8*** %4
%38 = load i8*, i8** %37
%39 = getelementptr inbounds i8, i8* %38, i64 8
%40 = load i8*, i8** %5
%41 = call i32 @strcpy(i8* %39, i8* %40)
%42 = load i8**, i8*** %4
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i64 8
%45 = load i8*, i8** %5
%46 = call i64 @strlen(i8* %45)
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = load i8*, i8** %6
%50 = call i32 @strcpy(i8* %48, i8* %49)
%51 = load i64, i64* %7
%52 = trunc i64 %51 to i32
ret i32 %52
}

LLVM IR (-O2):
define i32 @build_socks4a_resolve_request(i8** nocapture %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 @htons(i32 0)
store i32 %7, i32* %4
%8 = bitcast i32* %5 to i8*
%9 = tail call i32 @htonl(i32 1)
store i32 %9, i32* %5
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%11 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%12 = add i64 %10, 10
%13 = add i64 %12, %11
%14 = tail call i8* @malloc(i64 %13)
store i8* %14, i8** %0
store i8 4, i8* %14
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8 -16, i8* %15
%16 = getelementptr inbounds i8, i8* %14, i64 2
%17 = call i32 @memcpy(i8* %16, i32* %4, i32 4)
%18 = load i8*, i8** %0
%19 = getelementptr inbounds i8, i8* %18, i64 4
%20 = call i32 @memcpy(i8* %19, i32* %5, i32 4)
%21 = load i8*, i8** %0
%22 = getelementptr inbounds i8, i8* %21, i64 8
%23 = call i32 @strcpy(i8* %22, i8* %1)
%24 = load i8*, i8** %0
%25 = getelementptr inbounds i8, i8* %24, i64 8
%26 = call i64 @strlen(i8* dereferenceable(1) %1)
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = call i32 @strcpy(i8* %28, i8* %2)
%30 = trunc i64 %13 to i32
ret i32 %30
}