#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 scalar_t__ ssize_t;

/* Variables and functions */
char *calloc(scalar_t__, int);
int /*<<< orphan*/ free(char *);
scalar_t__ strlen(char const *);
char *strndup(char const *, int);
char strtol(char *, int /*<<< orphan*/ *, int);
char *flu_urldecode(const char *s, ssize_t n) {
  if (n < 0)
    n = strlen(s);
  char *r = calloc(n + 1, sizeof(char));
  for (size_t i = 0, j = 0; i < n; ++j) {
    if (s[i] != '%') {
      r[j] = s[i++];
      continue;
    }
    char *code = strndup(s + i + 1, 2);
    char c = strtol(code, NULL, 16);
    free(code);
    i = i + 3;
    r[j] = c;
  }
  return r;
}


x86 assembly (clang, -O0):
<flu_urldecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1178 <flu_urldecode+0x28>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    124a <flu_urldecode+0xfa>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x25,%eax
je     11e7 <flu_urldecode+0x97>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1239 <flu_urldecode+0xe9>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2,%esi
call   1050 <strndup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %al,-0x31(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x31(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    119e <flu_urldecode+0x4e>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flu_urldecode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
test   %rsi,%rsi
jns    1171 <flu_urldecode+0x21>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
lea    0x1(%r14),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,(%rsp)
test   %r14,%r14
je     11e3 <flu_urldecode+0x93>
xor    %ebp,%ebp
mov    (%rsp),%r15
jmp    119f <flu_urldecode+0x4f>
add    %rax,%rbp
mov    %bl,(%r15)
add    $0x1,%r15
cmp    %r14,%rbp
jae    11e3 <flu_urldecode+0x93>
movzbl (%r12,%rbp,1),%ebx
mov    $0x1,%eax
cmp    $0x25,%bl
jne    1190 <flu_urldecode+0x40>
lea    (%r12,%rbp,1),%rdi
add    $0x1,%rdi
mov    $0x2,%esi
call   1050 <strndup@plt>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x3,%eax
jmp    1190 <flu_urldecode+0x40>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @flu_urldecode(i8* nocapture %0, i64 %1) {
%3 = icmp slt i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
br label %6
6: ; preds = %4, %2
%7 = phi i64 [ %5, %4 ], [ %1, %2 ]
%8 = add nsw i64 %7, 1
%9 = tail call i8* @calloc(i64 %8, i32 1)
%10 = icmp eq i64 %7, 0
br i1 %10, label %11, label %12
11: ; preds = %23, %6
ret i8* %9
12: ; preds = %6, %23
%13 = phi i64 [ %26, %23 ], [ 0, %6 ]
%14 = phi i64 [ %28, %23 ], [ 0, %6 ]
%15 = getelementptr inbounds i8, i8* %0, i64 %13
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 37
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %15, i64 1
%20 = tail call i8* @strndup(i8* %19, i32 2)
%21 = tail call signext i8 @strtol(i8* nocapture %20, i32* null, i32 16)
%22 = tail call i32 @free(i8* %20)
br label %23
23: ; preds = %12, %18
%24 = phi i64 [ 3, %18 ], [ 1, %12 ]
%25 = phi i8 [ %21, %18 ], [ %16, %12 ]
%26 = add i64 %13, %24
%27 = getelementptr inbounds i8, i8* %9, i64 %14
store i8 %25, i8* %27
%28 = add i64 %14, 1
%29 = icmp ult i64 %26, %7
br i1 %29, label %12, label %11
}