#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*/ assert(int);
int /*<<< orphan*/ isxdigit(char);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ sscanf(char *, char *, int *);
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void urldecode(char *pszDecodedOut, size_t nBufferSize,
               const char *pszEncodedIn) {
  memset(pszDecodedOut, 0, nBufferSize);
  int state = 0;
  unsigned int i;
  for (i = 0; i < strlen(pszEncodedIn) - 1; ++i) {
    switch (state) {
    case 0: {
      if (pszEncodedIn[i] != '%') {
        strncat(pszDecodedOut, &pszEncodedIn[i], 1);
        break;
      }
      state = 1;
    } break;
    case 1: {
      state = 0;
      char pszTempNumBuf[3] = {0};
      strncpy(pszTempNumBuf, &pszEncodedIn[i], 2);
      bool bBothDigits = true;
      int j;
      for (j = 0; j < 2; ++j) {
        if (!isxdigit(pszTempNumBuf[j]))
          bBothDigits = false;
      }
      if (!bBothDigits)
        break;
      int nAsciiCharacter;
      sscanf(pszTempNumBuf, "%x", &nAsciiCharacter);
      assert(strlen(pszDecodedOut) < nBufferSize);
      strncat(pszDecodedOut, (char *)&nAsciiCharacter, 1);
      i++;
    } break;
    }
  }
}


x86 assembly (clang, -O0):
<urldecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    1309 <urldecode+0x199>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
test   %eax,%eax
je     11e9 <urldecode+0x79>
jmp    11d8 <urldecode+0x68>
mov    -0x3c(%rbp),%eax
sub    $0x1,%eax
je     1226 <urldecode+0xb6>
jmp    12f6 <urldecode+0x186>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x25,%eax
je     121a <urldecode+0xaa>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%eax
add    %rax,%rsi
mov    $0x1,%edx
call   1060 <strncat@plt>
jmp    12f6 <urldecode+0x186>
movl   $0x1,-0x1c(%rbp)
jmp    12f6 <urldecode+0x186>
movl   $0x0,-0x1c(%rbp)
lea    -0x23(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1050 <memset@plt>
lea    -0x23(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%eax
add    %rax,%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
movl   $0x1,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x2,-0x2c(%rbp)
jge    129e <urldecode+0x12e>
movslq -0x2c(%rbp),%rax
movsbl -0x23(%rbp,%rax,1),%edi
call   1090 <isxdigit@plt>
cmp    $0x0,%eax
jne    128b <urldecode+0x11b>
movl   $0x0,-0x28(%rbp)
jmp    1290 <urldecode+0x120>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1263 <urldecode+0xf3>
cmpl   $0x0,-0x28(%rbp)
jne    12ad <urldecode+0x13d>
jmp    12f6 <urldecode+0x186>
lea    -0x23(%rbp),%rdi
lea    0xd48(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x10(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1060 <strncat@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12fb <urldecode+0x18b>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a1 <urldecode+0x31>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urldecode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rdx
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %esi,%esi
mov    %rdx,0x8(%rsp)
call   1050 <memset@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0x1,%rax
jne    11b2 <urldecode+0x42>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x4(%rsp),%r12
xor    %esi,%esi
xor    %ebx,%ebx
jmp    11d9 <urldecode+0x69>
nopl   (%rax)
xor    %ebx,%ebx
add    $0x1,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0xffffffffffffffff,%rax
mov    %rbp,%rsi
cmp    %rbp,%rax
jbe    11a3 <urldecode+0x33>
cmp    $0x1,%ebx
je     1200 <urldecode+0x90>
test   %ebx,%ebx
jne    11c2 <urldecode+0x52>
mov    $0x1,%ebx
cmpb   $0x25,(%r14,%rsi,1)
je     11c2 <urldecode+0x52>
add    %r14,%rsi
mov    %r15,%rdi
mov    $0x1,%edx
call   1060 <strncat@plt>
jmp    11c0 <urldecode+0x50>
movb   $0x0,0x6(%rsp)
movw   $0x0,0x4(%rsp)
add    %r14,%rsi
mov    %r12,%rdi
mov    $0x2,%edx
call   1030 <strncpy@plt>
movsbl 0x4(%rsp),%edi
call   1090 <isxdigit@plt>
mov    %eax,%r13d
movsbl 0x5(%rsp),%edi
call   1090 <isxdigit@plt>
test   %eax,%eax
je     11c0 <urldecode+0x50>
test   %r13d,%r13d
je     11c0 <urldecode+0x50>
mov    %r12,%rdi
lea    0xdba(%rip),%rsi
lea    0x14(%rsp),%rbx
mov    %rbx,%rdx
call   1080 <sscanf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
cmp    0x8(%rsp),%rax
setb   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <strncat@plt>
add    $0x1,%ebp
jmp    11c0 <urldecode+0x50>

LLVM IR (-O0):
@.str = [3 x i8] c"%x\00"
define void @urldecode(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [3 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%13 = load i8*, i8** %4
%14 = load i64, i64* %5
%15 = call i32 @memset(i8* %13, i32 0, i64 %14)
store i32 0, i32* %7
store i32 0, i32* %8
br label %16
16: ; preds = %83, %3
%17 = load i32, i32* %8
%18 = zext i32 %17 to i64
%19 = load i8*, i8** %6
%20 = call i64 @strlen(i8* %19)
%21 = sub i64 %20, 1
%22 = icmp ult i64 %18, %21
br i1 %22, label %23, label %86
23: ; preds = %16
%24 = load i32, i32* %7
switch i32 %24, label %82 [
i32 0, label %25
i32 1, label %41
]
25: ; preds = %23
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 37
br i1 %32, label %33, label %40
33: ; preds = %25
%34 = load i8*, i8** %4
%35 = load i8*, i8** %6
%36 = load i32, i32* %8
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = call i32 @strncat(i8* %34, i8* %38, i32 1)
br label %82
40: ; preds = %25
store i32 1, i32* %7
br label %82
41: ; preds = %23
store i32 0, i32* %7
%42 = bitcast [3 x i8]* %9 to i8*
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%44 = load i8*, i8** %6
%45 = load i32, i32* %8
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = call i32 @strncpy(i8* %43, i8* %47, i32 2)
store i32 1, i32* %10
store i32 0, i32* %11
br label %49
49: ; preds = %61, %41
%50 = load i32, i32* %11
%51 = icmp slt i32 %50, 2
br i1 %51, label %52, label %64
52: ; preds = %49
%53 = load i32, i32* %11
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = call i32 @isxdigit(i8 signext %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %60, label %59
59: ; preds = %52
store i32 0, i32* %10
br label %60
60: ; preds = %59, %52
br label %61
61: ; preds = %60
%62 = load i32, i32* %11
%63 = add nsw i32 %62, 1
store i32 %63, i32* %11
br label %49
64: ; preds = %49
%65 = load i32, i32* %10
%66 = icmp ne i32 %65, 0
br i1 %66, label %68, label %67
67: ; preds = %64
br label %82
68: ; preds = %64
%69 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%70 = call i32 @sscanf(i8* %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %12)
%71 = load i8*, i8** %4
%72 = call i64 @strlen(i8* %71)
%73 = load i64, i64* %5
%74 = icmp ult i64 %72, %73
%75 = zext i1 %74 to i32
%76 = call i32 @assert(i32 %75)
%77 = load i8*, i8** %4
%78 = bitcast i32* %12 to i8*
%79 = call i32 @strncat(i8* %77, i8* %78, i32 1)
%80 = load i32, i32* %8
%81 = add i32 %80, 1
store i32 %81, i32* %8
br label %82
82: ; preds = %23, %68, %67, %40, %33
br label %83
83: ; preds = %82
%84 = load i32, i32* %8
%85 = add i32 %84, 1
store i32 %85, i32* %8
br label %16
86: ; preds = %16
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%x\00"
define void @urldecode(i8* %0, i64 %1, i8* %2) {
%4 = alloca [3 x i8]
%5 = alloca i32
%6 = tail call i32 @memset(i8* %0, i32 0, i64 %1)
%7 = call i64 @strlen(i8* dereferenceable(1) %2)
%8 = icmp eq i64 %7, 1
br i1 %8, label %51, label %9
9: ; preds = %3
%10 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%11 = bitcast i32* %5 to i8*
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
br label %13
13: ; preds = %9, %43
%14 = phi i64 [ 0, %9 ], [ %47, %43 ]
%15 = phi i32 [ 0, %9 ], [ %46, %43 ]
%16 = phi i32 [ 0, %9 ], [ %44, %43 ]
switch i32 %16, label %43 [
i32 0, label %17
i32 1, label %23
]
17: ; preds = %13
%18 = getelementptr inbounds i8, i8* %2, i64 %14
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 37
br i1 %20, label %43, label %21
21: ; preds = %17
%22 = call i32 @strncat(i8* %0, i8* %18, i32 1)
br label %43
23: ; preds = %13
%24 = getelementptr inbounds i8, i8* %2, i64 %14
%25 = call i32 @strncpy(i8* %10, i8* %24, i32 2)
%26 = load i8, i8* %10
%27 = call i32 @isxdigit(i8 signext %26)
%28 = icmp eq i32 %27, 0
%29 = load i8, i8* %12
%30 = call i32 @isxdigit(i8 signext %29)
%31 = icmp eq i32 %30, 0
%32 = select i1 %31, i1 true, i1 %28
br i1 %32, label %41, label %33
33: ; preds = %23
%34 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
%35 = call i64 @strlen(i8* dereferenceable(1) %0)
%36 = icmp ult i64 %35, %1
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = call i32 @strncat(i8* %0, i8* %11, i32 1)
%40 = add i32 %15, 1
br label %41
41: ; preds = %23, %33
%42 = phi i32 [ %40, %33 ], [ %15, %23 ]
br label %43
43: ; preds = %17, %41, %21, %13
%44 = phi i32 [ %16, %13 ], [ 0, %41 ], [ 0, %21 ], [ 1, %17 ]
%45 = phi i32 [ %15, %13 ], [ %42, %41 ], [ %15, %21 ], [ %15, %17 ]
%46 = add i32 %45, 1
%47 = zext i32 %46 to i64
%48 = call i64 @strlen(i8* dereferenceable(1) %2)
%49 = add i64 %48, -1
%50 = icmp ugt i64 %49, %47
br i1 %50, label %13, label %51
51: ; preds = %43, %3
ret void
}