#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*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *xmalloc(size_t);
void append_query_string(char **req, const char *qstr) {
  char *r;
  size_t s1, s2, qm = 0;
  s1 = strlen(*req);
  s2 = strlen(qstr);
  if (*qstr != '?')
    qm = 1;
  r = xmalloc(s1 + s2 + qm + 1);
  memcpy(r, *req, s1);
  if (qm)
    r[s1] = '?';
  memcpy(r + s1 + qm, qstr, s2 + 1);
  free(*req);
  *req = r;
}


x86 assembly (clang, -O0):
<append_query_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3f,%eax
je     118d <append_query_string+0x4d>
movq   $0x1,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
cmpq   $0x0,-0x30(%rbp)
je     11d1 <append_query_string+0x91>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x3f,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_query_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
mov    (%rdi),%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %rbx,(%rsp)
mov    (%rbx),%r14b
xor    %ebp,%ebp
cmp    $0x3f,%r14b
setne  %bpl
lea    (%rax,%r12,1),%rdi
add    $0x1,%rdi
add    $0x1,%r13
add    %rbp,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%rbx
mov    (%r15),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
call   1060 <memcpy@plt>
cmp    $0x3f,%r14b
je     11a8 <append_query_string+0x68>
movb   $0x3f,(%rbx,%r12,1)
mov    %rbx,%rdi
add    %r12,%rdi
add    %rbp,%rdi
mov    (%rsp),%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %rbx,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @append_query_string(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 63
%8 = xor i1 %7, true
%9 = zext i1 %8 to i64
%10 = add i64 %5, 1
%11 = add i64 %10, %4
%12 = add i64 %11, %9
%13 = tail call i8* @xmalloc(i64 %12)
%14 = load i8*, i8** %0
%15 = tail call i32 @memcpy(i8* %13, i8* %14, i64 %4)
br i1 %7, label %18, label %16
16: ; preds = %2
%17 = getelementptr inbounds i8, i8* %13, i64 %4
store i8 63, i8* %17
br label %18
18: ; preds = %16, %2
%19 = getelementptr inbounds i8, i8* %13, i64 %4
%20 = getelementptr inbounds i8, i8* %19, i64 %9
%21 = tail call i32 @memcpy(i8* %20, i8* %1, i64 %10)
%22 = load i8*, i8** %0
%23 = tail call i32 @free(i8* %22)
store i8* %13, i8** %0
ret void
}