#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*/ printf(char *);
char *strdup(char *);
void stub1(char const *, char const *);
void stub2(char const *, char const *);
int http_parse_querystring(char *txt,
                           void (*fn)(const char *name, const char *value)) {
  char *t, *value = NULL, c;
  if (!txt)
    return 0;
  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);
  return 0;
}


x86 assembly (clang, -O0):
<http_parse_querystring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116f <http_parse_querystring+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    1291 <http_parse_querystring+0x151>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a1 <http_parse_querystring+0x61>
lea    0xe6e(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
jmp    11a6 <http_parse_querystring+0x66>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     126e <http_parse_querystring+0x12e>
movsbl -0x29(%rbp),%eax
cmp    $0x3d,%eax
jne    11e0 <http_parse_querystring+0xa0>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    125d <http_parse_querystring+0x11d>
movsbl -0x29(%rbp),%eax
cmp    $0x2b,%eax
jne    11f9 <http_parse_querystring+0xb9>
mov    -0x20(%rbp),%rax
movb   $0x20,(%rax)
jmp    1258 <http_parse_querystring+0x118>
movsbl -0x29(%rbp),%eax
cmp    $0x25,%eax
jne    1218 <http_parse_querystring+0xd8>
mov    -0x20(%rbp),%rdi
call   1050 <convert_query_hexchar@plt>
mov    %rax,-0x20(%rbp)
jmp    1253 <http_parse_querystring+0x113>
movsbl -0x29(%rbp),%eax
cmp    $0x26,%eax
jne    124e <http_parse_querystring+0x10e>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
jmp    1253 <http_parse_querystring+0x113>
jmp    1258 <http_parse_querystring+0x118>
jmp    125d <http_parse_querystring+0x11d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11a6 <http_parse_querystring+0x66>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     128a <http_parse_querystring+0x14a>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_parse_querystring>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
je     11dd <http_parse_querystring+0x9d>
mov    %rsi,%r14
call   1060 <strdup@plt>
test   %rax,%rax
je     11e9 <http_parse_querystring+0xa9>
mov    %rax,%r15
xor    %r12d,%r12d
lea    0xe90(%rip),%r13
mov    %rax,%rbx
jmp    118c <http_parse_querystring+0x4c>
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    %r12,%rsi
call   *%r14
lea    0x1(%rbx),%r15
xor    %r12d,%r12d
add    $0x1,%rbx
movzbl (%rbx),%eax
lea    -0x25(%rax),%ecx
cmp    $0x18,%cl
ja     11cb <http_parse_querystring+0x8b>
movzbl %cl,%eax
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
mov    %rbx,%rdi
call   1050 <convert_query_hexchar@plt>
mov    %rax,%rbx
add    $0x1,%rbx
jmp    118c <http_parse_querystring+0x4c>
movb   $0x20,(%rbx)
add    $0x1,%rbx
jmp    118c <http_parse_querystring+0x4c>
movb   $0x0,(%rbx)
lea    0x1(%rbx),%r12
add    $0x1,%rbx
jmp    118c <http_parse_querystring+0x4c>
test   %al,%al
jne    1188 <http_parse_querystring+0x48>
cmp    %r15,%rbx
je     11dd <http_parse_querystring+0x9d>
mov    %r15,%rdi
mov    %r12,%rsi
call   *%r14
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1040 <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
store i8* %0, i8** %4
store void (i8*, i8*)* %1, void (i8*, i8*)** %5
store i8* null, i8** %7
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 0, i32* %3
br label %73
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %4
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %12
br label %21
21: ; preds = %61, %20
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
store i8 %23, i8* %8
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %64
26: ; preds = %21
%27 = load i8, i8* %8
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 61
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i8*, i8** %6
store i8 0, i8* %31
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8* %33, i8** %7
br label %61
34: ; preds = %26
%35 = load i8, i8* %8
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 43
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i8*, i8** %6
store i8 32, i8* %39
br label %60
40: ; preds = %34
%41 = load i8, i8* %8
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 37
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i8*, i8** %6
%46 = call i8* @convert_query_hexchar(i8* %45)
store i8* %46, i8** %6
br label %59
47: ; preds = %40
%48 = load i8, i8* %8
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 38
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load i8*, i8** %6
store i8 0, i8* %52
%53 = load void (i8*, i8*)*, void (i8*, i8*)** %5
%54 = load i8*, i8** %4
%55 = load i8*, i8** %7
%56 = load i8*, i8** %6
%57 = getelementptr inbounds i8, i8* %56, i64 1
store i8* %57, i8** %4
store i8* null, i8** %7
br label %58
58: ; preds = %51, %47
br label %59
59: ; preds = %58, %44
br label %60
60: ; preds = %59, %38
br label %61
61: ; preds = %60, %30
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %6
br label %21
64: ; preds = %21
%65 = load i8*, i8** %6
%66 = load i8*, i8** %4
%67 = icmp ne i8* %65, %66
br i1 %67, label %68, label %72
68: ; preds = %64
%69 = load void (i8*, i8*)*, void (i8*, i8*)** %5
%70 = load i8*, i8** %4
%71 = load i8*, i8** %7
br label %72
72: ; preds = %68, %64
store i32 0, i32* %3
br label %73
73: ; preds = %72, %11
%74 = load i32, i32* %3
ret i32 %74
}

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 %30, 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* [ %23, %22 ], [ %5, %4 ]
%12 = phi i8* [ %26, %22 ], [ %5, %4 ]
%13 = phi i8* [ %25, %22 ], [ null, %4 ]
%14 = load i8, i8* %12
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* %12
%16 = getelementptr inbounds i8, i8* %12, i64 1
br label %22
17: ; preds = %10
store i8 32, i8* %12
br label %22
18: ; preds = %10
%19 = tail call i8* @convert_query_hexchar(i8* %12)
br label %22
20: ; preds = %10
store i8 0, i8* %12
tail call void %1(i8* %11, i8* %13)
%21 = getelementptr inbounds i8, i8* %12, i64 1
br label %22
22: ; preds = %10, %17, %20, %18, %15
%23 = phi i8* [ %11, %15 ], [ %11, %17 ], [ %11, %18 ], [ %21, %20 ], [ %11, %10 ]
%24 = phi i8* [ %12, %15 ], [ %12, %17 ], [ %19, %18 ], [ %12, %20 ], [ %12, %10 ]
%25 = phi i8* [ %16, %15 ], [ %13, %17 ], [ %13, %18 ], [ null, %20 ], [ %13, %10 ]
%26 = getelementptr inbounds i8, i8* %24, i64 1
br label %10
27: ; preds = %10
%28 = icmp eq i8* %12, %11
br i1 %28, label %30, label %29
29: ; preds = %27
tail call void %1(i8* %11, i8* %13)
br label %30
30: ; preds = %27, %29, %2
ret i32 0
}