#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 *convert_query_hexchar(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *);
char *strdup(char const *);
void stub1(char const *, char const *);
void stub2(char const *, char const *);
int http_parse_querystring(const char *txt_,
                           void (*fn)(const char *name, const char *value)) {
  char *o, *t, *txt, *value = NULL, c;
  if (!txt_)
    return 0;
  o = t = txt = strdup(txt_);
  if (t == NULL) {
    printf("Out of memory\n");
    exit(1);
  }
  while ((c = *t) != '\0') {
    if (c == '=') {
      *t = '\0';
      value = t + 1;
    } else if (c == '+') {
      *t = ' ';
    } else if (c == '%') {
      t = convert_query_hexchar(t);
    } else if (c == '&') {
      *t = '\0';
      (*fn)(txt, value);
      txt = t + 1;
      value = NULL;
    }
    t++;
  }
  if (t != txt)
    (*fn)(txt, value);
  free(o);
  return 0;
}


x86 assembly (clang, -O0):
<http_parse_querystring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117f <http_parse_querystring+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    12ae <http_parse_querystring+0x15e>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b5 <http_parse_querystring+0x65>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11ba <http_parse_querystring+0x6a>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x39(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     1282 <http_parse_querystring+0x132>
movsbl -0x39(%rbp),%eax
cmp    $0x3d,%eax
jne    11f4 <http_parse_querystring+0xa4>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1271 <http_parse_querystring+0x121>
movsbl -0x39(%rbp),%eax
cmp    $0x2b,%eax
jne    120d <http_parse_querystring+0xbd>
mov    -0x28(%rbp),%rax
movb   $0x20,(%rax)
jmp    126c <http_parse_querystring+0x11c>
movsbl -0x39(%rbp),%eax
cmp    $0x25,%eax
jne    122c <http_parse_querystring+0xdc>
mov    -0x28(%rbp),%rdi
call   1060 <convert_query_hexchar@plt>
mov    %rax,-0x28(%rbp)
jmp    1267 <http_parse_querystring+0x117>
movsbl -0x39(%rbp),%eax
cmp    $0x26,%eax
jne    1262 <http_parse_querystring+0x112>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   *%rax
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
jmp    1267 <http_parse_querystring+0x117>
jmp    126c <http_parse_querystring+0x11c>
jmp    1271 <http_parse_querystring+0x121>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ba <http_parse_querystring+0x6a>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     129e <http_parse_querystring+0x14e>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   *%rax
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_parse_querystring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11fa <http_parse_querystring+0xaa>
mov    %rsi,%r15
call   1070 <strdup@plt>
test   %rax,%rax
je     120b <http_parse_querystring+0xbb>
mov    %rax,%r14
xor    %r13d,%r13d
lea    0xe7e(%rip),%rbp
mov    %rax,%rbx
mov    %rax,%r12
jmp    11a1 <http_parse_querystring+0x51>
movb   $0x0,(%rbx)
mov    %r12,%rdi
mov    %r13,%rsi
call   *%r15
lea    0x1(%rbx),%r12
xor    %r13d,%r13d
add    $0x1,%rbx
movzbl (%rbx),%eax
lea    -0x25(%rax),%ecx
cmp    $0x18,%cl
ja     11e0 <http_parse_querystring+0x90>
movzbl %cl,%eax
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
mov    %rbx,%rdi
call   1060 <convert_query_hexchar@plt>
mov    %rax,%rbx
add    $0x1,%rbx
jmp    11a1 <http_parse_querystring+0x51>
movb   $0x20,(%rbx)
add    $0x1,%rbx
jmp    11a1 <http_parse_querystring+0x51>
movb   $0x0,(%rbx)
lea    0x1(%rbx),%r13
add    $0x1,%rbx
jmp    11a1 <http_parse_querystring+0x51>
test   %al,%al
jne    119d <http_parse_querystring+0x4d>
cmp    %r12,%rbx
je     11f2 <http_parse_querystring+0xa2>
mov    %r12,%rdi
mov    %r13,%rsi
call   *%r15
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Out of memory\0A\00"
define i32 @http_parse_querystring(i8* %0, void (i8*, i8*)* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca void (i8*, i8*)*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8
store i8* %0, i8** %4
store void (i8*, i8*)* %1, void (i8*, i8*)** %5
store i8* null, i8** %9
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 0, i32* %3
br label %77
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %8
store i8* %16, i8** %7
store i8* %16, i8** %6
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
br label %23
23: ; preds = %63, %22
%24 = load i8*, i8** %7
%25 = load i8, i8* %24
store i8 %25, i8* %10
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %66
28: ; preds = %23
%29 = load i8, i8* %10
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 61
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i8*, i8** %7
store i8 0, i8* %33
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i64 1
store i8* %35, i8** %9
br label %63
36: ; preds = %28
%37 = load i8, i8* %10
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 43
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i8*, i8** %7
store i8 32, i8* %41
br label %62
42: ; preds = %36
%43 = load i8, i8* %10
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 37
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i8*, i8** %7
%48 = call i8* @convert_query_hexchar(i8* %47)
store i8* %48, i8** %7
br label %61
49: ; preds = %42
%50 = load i8, i8* %10
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 38
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i8*, i8** %7
store i8 0, i8* %54
%55 = load void (i8*, i8*)*, void (i8*, i8*)** %5
%56 = load i8*, i8** %8
%57 = load i8*, i8** %9
%58 = load i8*, i8** %7
%59 = getelementptr inbounds i8, i8* %58, i64 1
store i8* %59, i8** %8
store i8* null, i8** %9
br label %60
60: ; preds = %53, %49
br label %61
61: ; preds = %60, %46
br label %62
62: ; preds = %61, %40
br label %63
63: ; preds = %62, %32
%64 = load i8*, i8** %7
%65 = getelementptr inbounds i8, i8* %64, i32 1
store i8* %65, i8** %7
br label %23
66: ; preds = %23
%67 = load i8*, i8** %7
%68 = load i8*, i8** %8
%69 = icmp ne i8* %67, %68
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load void (i8*, i8*)*, void (i8*, i8*)** %5
%72 = load i8*, i8** %8
%73 = load i8*, i8** %9
br label %74
74: ; preds = %70, %66
%75 = load i8*, i8** %6
%76 = call i32 @free(i8* %75)
store i32 0, i32* %3
br label %77
77: ; preds = %74, %13
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@str = [14 x i8] c"Out of memory\00"
define i32 @http_parse_querystring(i8* %0, void (i8*, i8*)* nocapture %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %32, label %4
4: ; preds = %2
%5 = tail call i8* @strdup(i8* %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %4, %22
%11 = phi i8* [ %26, %22 ], [ %5, %4 ]
%12 = phi i8* [ %24, %22 ], [ %5, %4 ]
%13 = phi i8* [ %25, %22 ], [ null, %4 ]
%14 = load i8, i8* %11
switch i8 %14, label %22 [
i8 0, label %27
i8 61, label %15
i8 43, label %17
i8 37, label %18
i8 38, label %20
]
15: ; preds = %10
store i8 0, i8* %11
%16 = getelementptr inbounds i8, i8* %11, i64 1
br label %22
17: ; preds = %10
store i8 32, i8* %11
br label %22
18: ; preds = %10
%19 = tail call i8* @convert_query_hexchar(i8* %11)
br label %22
20: ; preds = %10
store i8 0, i8* %11
tail call void %1(i8* %12, i8* %13)
%21 = getelementptr inbounds i8, i8* %11, i64 1
br label %22
22: ; preds = %10, %17, %20, %18, %15
%23 = phi i8* [ %11, %15 ], [ %11, %17 ], [ %19, %18 ], [ %11, %20 ], [ %11, %10 ]
%24 = phi i8* [ %12, %15 ], [ %12, %17 ], [ %12, %18 ], [ %21, %20 ], [ %12, %10 ]
%25 = phi i8* [ %16, %15 ], [ %13, %17 ], [ %13, %18 ], [ null, %20 ], [ %13, %10 ]
%26 = getelementptr inbounds i8, i8* %23, i64 1
br label %10
27: ; preds = %10
%28 = icmp eq i8* %11, %12
br i1 %28, label %30, label %29
29: ; preds = %27
tail call void %1(i8* %12, i8* %13)
br label %30
30: ; preds = %29, %27
%31 = tail call i32 @free(i8* %5)
br label %32
32: ; preds = %2, %30
ret i32 0
}