#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 sscanf(char *, char *, char *);
int url_decode(char *buffer) {
  char *c = buffer, *o = buffer;
  int size = 0;
  for (; *c != '\0';) {
    if (*c == '%') {
      int count = sscanf(c + 1, "%2hhx", o);
      if (count != 1) {
        size = -1;
        break;
      }
      c += 3;
    } else {
      *o = *c;
      c += 1;
    }
    o += 1;
    size += 1;
  }
  if (size > 0)
    *o = '\0';
  return size;
}


x86 assembly (clang, -O0):
<url_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)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c7 <url_decode+0xb7>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    1195 <url_decode+0x85>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rdx
lea    0xe9a(%rip),%rsi
call   1030 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
je     1184 <url_decode+0x74>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11c7 <url_decode+0xb7>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
jmp    11ad <url_decode+0x9d>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1133 <url_decode+0x23>
cmpl   $0x0,-0x1c(%rbp)
jle    11d8 <url_decode+0xc8>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_decode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %r12d,%r12d
lea    0xedb(%rip),%r15
mov    %rdi,%rbx
nopl   0x0(%rax,%rax,1)
lea    (%r14,%r12,1),%rdx
movzbl (%rbx),%eax
cmp    $0x25,%al
je     1150 <url_decode+0x40>
test   %al,%al
je     116b <url_decode+0x5b>
mov    %al,(%rdx)
add    $0x1,%rbx
add    $0x1,%r12
jmp    1130 <url_decode+0x20>
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbx),%rdi
mov    %r15,%rsi
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    1177 <url_decode+0x67>
add    $0x3,%rbx
add    $0x1,%r12
jmp    1130 <url_decode+0x20>
test   %r12d,%r12d
je     117d <url_decode+0x6d>
movb   $0x0,(%r14,%r12,1)
jmp    117d <url_decode+0x6d>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%2hhx\00"
define i32 @url_decode(i8* %0) {
br label %2
2: ; preds = %14, %1
%3 = phi i8* [ %0, %1 ], [ %16, %14 ]
%4 = phi i32 [ 0, %1 ], [ %17, %14 ]
%5 = phi i8* [ %0, %1 ], [ %15, %14 ]
%6 = load i8, i8* %5
switch i8 %6, label %12 [
i8 0, label %18
i8 37, label %7
]
7: ; preds = %2
%8 = getelementptr inbounds i8, i8* %5, i64 1
%9 = tail call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %3)
%10 = icmp eq i32 %9, 1
%11 = getelementptr inbounds i8, i8* %5, i64 3
br i1 %10, label %14, label %21
12: ; preds = %2
store i8 %6, i8* %3
%13 = getelementptr inbounds i8, i8* %5, i64 1
br label %14
14: ; preds = %7, %12
%15 = phi i8* [ %11, %7 ], [ %13, %12 ]
%16 = getelementptr inbounds i8, i8* %3, i64 1
%17 = add nuw nsw i32 %4, 1
br label %2
18: ; preds = %2
%19 = icmp eq i32 %4, 0
br i1 %19, label %21, label %20
20: ; preds = %18
store i8 0, i8* %3
br label %21
21: ; preds = %7, %20, %18
%22 = phi i32 [ %4, %20 ], [ %4, %18 ], [ -1, %7 ]
ret i32 %22
}