#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 *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *, char const *);
scalar_t__ strchr(char *, char);
scalar_t__ strlen(char const *);
char *strrecat(char *, char *);
char *addparam(char *orig, const char *parname, const char *parvalue,
               int addq) {
  char *append = malloc(1 + strlen(parname) + 1 + strlen(parvalue) + 1);
  sprintf(
      append, "%s%s=%s",
      (strchr(orig, '?') ? (addq ? "?" : "") : ((strlen(orig) > 0) ? "&" : "")),
      parname, parvalue);
  return strrecat(orig, append);
}


x86 assembly (clang, -O0):
<addparam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x3f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11da <addparam+0x8a>
mov    -0x1c(%rbp),%edx
lea    0xe48(%rip),%rax
lea    0xe3e(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    1200 <addparam+0xb0>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdx
lea    0xe1e(%rip),%rax
lea    0xe16(%rip),%rcx
cmp    $0x0,%rdx
cmovg  %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xde9(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strrecat@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addparam>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x3,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x3f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11ae <addparam+0x5e>
test   %ebp,%ebp
lea    0xe6a(%rip),%rax
lea    0xe60(%rip),%rdx
cmove  %rax,%rdx
jmp    11cb <addparam+0x7b>
mov    %r12,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
lea    0xe4a(%rip),%rax
lea    0xe44(%rip),%rdx
cmovg  %rax,%rdx
lea    0xe2e(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rcx
mov    %r14,%r8
call   1070 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strrecat@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"%s%s=%s\00"
@.str.1 = [2 x i8] c"?\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"&\00"
define i8* @addparam(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%10 = load i8*, i8** %6
%11 = call i64 @strlen(i8* %10)
%12 = add nsw i64 1, %11
%13 = add nsw i64 %12, 1
%14 = load i8*, i8** %7
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %13, %15
%17 = add nsw i64 %16, 1
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %9
%19 = load i8*, i8** %9
%20 = load i8*, i8** %5
%21 = call i64 @strchr(i8* %20, i8 signext 63)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %4
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
%26 = zext i1 %25 to i64
%27 = select i1 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
br label %34
28: ; preds = %4
%29 = load i8*, i8** %5
%30 = call i64 @strlen(i8* %29)
%31 = icmp sgt i64 %30, 0
%32 = zext i1 %31 to i64
%33 = select i1 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
br label %34
34: ; preds = %28, %23
%35 = phi i8* [ %27, %23 ], [ %33, %28 ]
%36 = load i8*, i8** %6
%37 = load i8*, i8** %7
%38 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %36, i8* %37)
%39 = load i8*, i8** %5
%40 = load i8*, i8** %9
%41 = call i8* @strrecat(i8* %39, i8* %40)
ret i8* %41
}

LLVM IR (-O2):
@.str = [8 x i8] c"%s%s=%s\00"
@.str.1 = [2 x i8] c"?\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"&\00"
define i8* @addparam(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = add i64 %5, 3
%8 = add i64 %7, %6
%9 = tail call i8* @malloc(i64 %8)
%10 = tail call i64 @strchr(i8* %0, i8 signext 63)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = icmp eq i32 %3, 0
%14 = select i1 %13, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
br label %19
15: ; preds = %4
%16 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%17 = icmp sgt i64 %16, 0
%18 = select i1 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
br label %19
19: ; preds = %15, %12
%20 = phi i8* [ %14, %12 ], [ %18, %15 ]
%21 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %1, i8* %2)
%22 = tail call i8* @strrecat(i8* %0, i8* %9)
ret i8* %22
}