#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 */
scalar_t__ isxdigit(char);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
char *uri_decode(char *buf) {
  char *head = buf;
  char *tail = buf;
  while (*head != '\0') {
    if (*head == '%') {
      if (isxdigit(head[1]) && isxdigit(head[2])) {
        unsigned int val;
        sscanf(head + 1, "%2x", &val);
        *(tail++) = (char)val;
        head += 3;
        continue;
      }
    }
    *(tail++) = *(head++);
  }
  *tail = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<uri_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f5 <uri_decode+0xd5>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    11ce <uri_decode+0xae>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%edi
call   1040 <isxdigit@plt>
cmp    $0x0,%rax
je     11c9 <uri_decode+0xa9>
mov    -0x10(%rbp),%rax
movsbl 0x2(%rax),%edi
call   1040 <isxdigit@plt>
cmp    $0x0,%rax
je     11c9 <uri_decode+0xa9>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    0xe67(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1030 <sscanf@plt>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
jmp    113c <uri_decode+0x1c>
jmp    11ce <uri_decode+0xae>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    113c <uri_decode+0x1c>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uri_decode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xec9(%rip),%r15
lea    0xc(%rsp),%r12
mov    %rdi,%rbx
mov    %rdi,%r13
jmp    1163 <uri_decode+0x43>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %al,%al
je     11a8 <uri_decode+0x88>
movzbl (%rbx),%eax
add    $0x1,%rbx
mov    %al,0x0(%r13)
add    $0x1,%r13
movzbl (%rbx),%eax
cmp    $0x25,%al
jne    1150 <uri_decode+0x30>
movsbl 0x1(%rbx),%edi
call   1040 <isxdigit@plt>
test   %rax,%rax
je     1154 <uri_decode+0x34>
movsbl 0x2(%rbx),%edi
call   1040 <isxdigit@plt>
test   %rax,%rax
je     1154 <uri_decode+0x34>
lea    0x1(%rbx),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <sscanf@plt>
movzbl 0xc(%rsp),%eax
mov    %al,0x0(%r13)
add    $0x3,%rbx
add    $0x1,%r13
jmp    1163 <uri_decode+0x43>
movb   $0x0,0x0(%r13)
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%2x\00"
define i8* @uri_decode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
store i8* %6, i8** %3
%7 = load i8*, i8** %2
store i8* %7, i8** %4
br label %8
8: ; preds = %41, %30, %1
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %47
13: ; preds = %8
%14 = load i8*, i8** %3
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 37
br i1 %17, label %18, label %41
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = call i64 @isxdigit(i8 signext %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %18
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 2
%27 = load i8, i8* %26
%28 = call i64 @isxdigit(i8 signext %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %24
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %5)
%34 = load i32, i32* %5
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %4
store i8 %35, i8* %36
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i64 3
store i8* %39, i8** %3
br label %8
40: ; preds = %24, %18
br label %41
41: ; preds = %40, %13
%42 = load i8*, i8** %3
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %3
%44 = load i8, i8* %42
%45 = load i8*, i8** %4
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %4
store i8 %44, i8* %45
br label %8
47: ; preds = %8
%48 = load i8*, i8** %4
store i8 0, i8* %48
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
@.str = [4 x i8] c"%2x\00"
define i8* @uri_decode(i8* returned %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %23, %1
%5 = phi i8* [ %0, %1 ], [ %24, %23 ]
%6 = phi i8* [ %0, %1 ], [ %25, %23 ]
%7 = load i8, i8* %5
switch i8 %7, label %26 [
i8 0, label %29
i8 37, label %8
]
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %5, i64 1
%10 = load i8, i8* %9
%11 = call i64 @isxdigit(i8 signext %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %8
%14 = getelementptr inbounds i8, i8* %5, i64 2
%15 = load i8, i8* %14
%16 = call i64 @isxdigit(i8 signext %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %13
%19 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%20 = load i32, i32* %2
%21 = trunc i32 %20 to i8
store i8 %21, i8* %6
%22 = getelementptr inbounds i8, i8* %5, i64 3
br label %23
23: ; preds = %18, %26
%24 = phi i8* [ %22, %18 ], [ %27, %26 ]
%25 = getelementptr inbounds i8, i8* %6, i64 1
br label %4
26: ; preds = %4, %8, %13
%27 = getelementptr inbounds i8, i8* %5, i64 1
%28 = load i8, i8* %5
store i8 %28, i8* %6
br label %23
29: ; preds = %4
store i8 0, i8* %6
ret i8* %0
}