#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ isxdigit(unsigned char);
unsigned char *malloc(scalar_t__);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
scalar_t__ strtol(char const *, int /*<<< orphan*/ *, int);
unsigned char *clean_uncgi(unsigned char *s, void *opts) {
  unsigned char *output, *input_walk, *output_walk;
  unsigned char conv[3];
  if (s == NULL) {
    return NULL;
  }
  output = malloc(strlen((const char *)s) + 1);
  if (output == NULL) {
    fprintf(stderr, "out of memory: %s\n", strerror(errno));
    return NULL;
  }
  input_walk = s;
  output_walk = output;
  while (*input_walk != '\0') {
    if (input_walk[0] == '%' && isxdigit(input_walk[1]) &&
        isxdigit(input_walk[2])) {
      conv[0] = input_walk[1];
      conv[1] = input_walk[2];
      conv[2] = 0;
      *output_walk++ = (unsigned char)strtol((const char *)conv, NULL, 16);
      input_walk += 3;
    } else {
      *output_walk++ = *input_walk++;
    }
  }
  *output_walk = '\0';
  return output;
}


x86 assembly (clang, -O0):
<clean_uncgi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1188 <clean_uncgi+0x28>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <clean_uncgi+0x167>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e5 <clean_uncgi+0x85>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xe2d(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <clean_uncgi+0x167>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     12b8 <clean_uncgi+0x158>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x25,%eax
jne    1291 <clean_uncgi+0x131>
mov    -0x28(%rbp),%rax
movzbl 0x1(%rax),%edi
call   1070 <isxdigit@plt>
cmp    $0x0,%rax
je     1291 <clean_uncgi+0x131>
mov    -0x28(%rbp),%rax
movzbl 0x2(%rax),%edi
call   1070 <isxdigit@plt>
cmp    $0x0,%rax
je     1291 <clean_uncgi+0x131>
mov    -0x28(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x33(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2(%rax),%al
mov    %al,-0x32(%rbp)
movb   $0x0,-0x31(%rbp)
lea    -0x33(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x28(%rbp)
jmp    12b3 <clean_uncgi+0x153>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    11f5 <clean_uncgi+0x95>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clean_uncgi>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1237 <clean_uncgi+0xd7>
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     120f <clean_uncgi+0xaf>
mov    %rax,%r14
lea    0x5(%rsp),%r15
mov    %rax,%r12
jmp    11b3 <clean_uncgi+0x53>
nopl   0x0(%rax,%rax,1)
test   %al,%al
je     1208 <clean_uncgi+0xa8>
movzbl (%rbx),%eax
add    $0x1,%rbx
mov    %al,(%r12)
add    $0x1,%r12
movzbl (%rbx),%eax
cmp    $0x25,%al
jne    11a0 <clean_uncgi+0x40>
movzbl 0x1(%rbx),%edi
call   1070 <isxdigit@plt>
test   %rax,%rax
je     11a4 <clean_uncgi+0x44>
movzbl 0x2(%rbx),%edi
call   1070 <isxdigit@plt>
test   %rax,%rax
je     11a4 <clean_uncgi+0x44>
movzbl 0x1(%rbx),%eax
mov    %al,0x5(%rsp)
movzbl 0x2(%rbx),%eax
mov    %al,0x6(%rsp)
movb   $0x0,0x7(%rsp)
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %al,(%r12)
add    $0x3,%rbx
add    $0x1,%r12
jmp    11b3 <clean_uncgi+0x53>
movb   $0x0,(%r12)
jmp    123a <clean_uncgi+0xda>
mov    0x2dca(%rip),%rax
mov    (%rax),%ebx
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdd3(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"out of memory: %s\0A\00"
@errno = global i32 0
define i8* @clean_uncgi(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [3 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i8* null, i8** %3
br label %78
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %15, 1
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %23)
store i8* null, i8** %3
br label %78
25: ; preds = %13
%26 = load i8*, i8** %4
store i8* %26, i8** %7
%27 = load i8*, i8** %6
store i8* %27, i8** %8
br label %28
28: ; preds = %74, %25
%29 = load i8*, i8** %7
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %75
33: ; preds = %28
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = icmp eq i32 %37, 37
br i1 %38, label %39, label %68
39: ; preds = %33
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = load i8, i8* %41
%43 = call i64 @isxdigit(i8 zeroext %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %68
45: ; preds = %39
%46 = load i8*, i8** %7
%47 = getelementptr inbounds i8, i8* %46, i64 2
%48 = load i8, i8* %47
%49 = call i64 @isxdigit(i8 zeroext %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %68
51: ; preds = %45
%52 = load i8*, i8** %7
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = load i8, i8* %53
%55 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
store i8 %54, i8* %55
%56 = load i8*, i8** %7
%57 = getelementptr inbounds i8, i8* %56, i64 2
%58 = load i8, i8* %57
%59 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
store i8 %58, i8* %59
%60 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 2
store i8 0, i8* %60
%61 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%62 = call i64 @strtol(i8* %61, i32* null, i32 16)
%63 = trunc i64 %62 to i8
%64 = load i8*, i8** %8
%65 = getelementptr inbounds i8, i8* %64, i32 1
store i8* %65, i8** %8
store i8 %63, i8* %64
%66 = load i8*, i8** %7
%67 = getelementptr inbounds i8, i8* %66, i64 3
store i8* %67, i8** %7
br label %74
68: ; preds = %45, %39, %33
%69 = load i8*, i8** %7
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %7
%71 = load i8, i8* %69
%72 = load i8*, i8** %8
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %8
store i8 %71, i8* %72
br label %74
74: ; preds = %68, %51
br label %28
75: ; preds = %28
%76 = load i8*, i8** %8
store i8 0, i8* %76
%77 = load i8*, i8** %6
store i8* %77, i8** %3
br label %78
78: ; preds = %75, %20, %12
%79 = load i8*, i8** %3
ret i8* %79
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"out of memory: %s\0A\00"
@errno = global i32 0
define noalias i8* @clean_uncgi(i8* %0, i8* nocapture readnone %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %0, null
br i1 %5, label %46, label %6
6: ; preds = %2
%7 = tail call i64 @strlen(i8* %0)
%8 = add nsw i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
br label %19
14: ; preds = %6
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %46
19: ; preds = %11, %42
%20 = phi i8* [ %43, %42 ], [ %0, %11 ]
%21 = phi i8* [ %44, %42 ], [ %9, %11 ]
%22 = load i8, i8* %20
switch i8 %22, label %39 [
i8 0, label %45
i8 37, label %23
]
23: ; preds = %19
%24 = getelementptr inbounds i8, i8* %20, i64 1
%25 = load i8, i8* %24
%26 = tail call i64 @isxdigit(i8 zeroext %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %39, label %28
28: ; preds = %23
%29 = getelementptr inbounds i8, i8* %20, i64 2
%30 = load i8, i8* %29
%31 = tail call i64 @isxdigit(i8 zeroext %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %28
%34 = load i8, i8* %24
store i8 %34, i8* %4
%35 = load i8, i8* %29
store i8 %35, i8* %12
store i8 0, i8* %13
%36 = call i64 @strtol(i8* nocapture %4, i32* null, i32 16)
%37 = trunc i64 %36 to i8
store i8 %37, i8* %21
%38 = getelementptr inbounds i8, i8* %20, i64 3
br label %42
39: ; preds = %19, %28, %23
%40 = getelementptr inbounds i8, i8* %20, i64 1
%41 = load i8, i8* %20
store i8 %41, i8* %21
br label %42
42: ; preds = %39, %33
%43 = phi i8* [ %38, %33 ], [ %40, %39 ]
%44 = getelementptr inbounds i8, i8* %21, i64 1
br label %19
45: ; preds = %19
store i8 0, i8* %21
br label %46
46: ; preds = %2, %45, %14
%47 = phi i8* [ null, %14 ], [ %9, %45 ], [ null, %2 ]
ret i8* %47
}