#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*/ fmtstr(char *, size_t *, size_t, char *, int, int, int);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
scalar_t__ strchr(char *, unsigned char);
void fmturl(char *buffer, size_t *currlen, size_t maxlen, char *value,
            int flags, int min, int max) {
  int len = 0;
  unsigned char *current, *dest;
  unsigned char *new_string;
  unsigned int digit1, digit2;
  char *digits = "0123456789abcdef";
  char *safe = "abcdefghijklmnopqrstuvwxyz"
               "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
               "0123456789$-_.!*(),/@\\";
  current = (unsigned char *)value;
  while (*current) {
    if (strchr(safe, *current)) {
      len++;
    } else {
      len += 3;
    }
    current++;
  }
  new_string = (unsigned char *)malloc(len + 1);
  if (!new_string) {
    fmtstr(buffer, currlen, maxlen, value, flags, min, max);
    return;
  }
  current = (unsigned char *)value;
  dest = new_string;
  while (*current) {
    if (strchr(safe, *current)) {
      *dest++ = *current++;
    } else if (*current == ' ') {
      *dest++ = '+';
      *current++;
    } else {
      *dest++ = '%';
      digit1 = (*current) / 16;
      digit2 = (*current) % 16;
      *dest++ = digits[digit1];
      *dest++ = digits[digit2];
      current++;
    }
  }
  *dest = '\0';
  fmtstr(buffer, currlen, maxlen, (char *)new_string, flags, min, max);
  free(new_string);
  return;
}


x86 assembly (clang, -O0):
<fmturl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe8f(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xe95(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     11d7 <fmturl+0x97>
mov    -0x60(%rbp),%rdi
mov    -0x38(%rbp),%rax
movzbl (%rax),%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     11bd <fmturl+0x7d>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11c6 <fmturl+0x86>
mov    -0x2c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1188 <fmturl+0x48>
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1219 <fmturl+0xd9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    -0x28(%rbp),%r9d
mov    0x10(%rbp),%eax
mov    %eax,(%rsp)
call   1060 <fmtstr@plt>
jmp    135f <fmturl+0x21f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     132c <fmturl+0x1ec>
mov    -0x60(%rbp),%rdi
mov    -0x38(%rbp),%rax
movzbl (%rax),%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     1277 <fmturl+0x137>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
mov    (%rax),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
jmp    1327 <fmturl+0x1e7>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x20,%eax
jne    12aa <fmturl+0x16a>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
movb   $0x2b,(%rax)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1322 <fmturl+0x1e2>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
movb   $0x25,(%rax)
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %edx,-0x50(%rbp)
mov    -0x58(%rbp),%rax
mov    -0x4c(%rbp),%ecx
mov    (%rax,%rcx,1),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%ecx
mov    (%rax,%rcx,1),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1327 <fmturl+0x1e7>
jmp    1229 <fmturl+0xe9>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    -0x28(%rbp),%r9d
mov    0x10(%rbp),%eax
mov    %eax,(%rsp)
call   1060 <fmtstr@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmturl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%ebp
mov    %r8d,%r12d
mov    %rcx,%r13
mov    0x60(%rsp),%ecx
mov    0x0(%r13),%al
test   %al,%al
mov    %rdx,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %rdi,0x10(%rsp)
mov    %ecx,0x8(%rsp)
je     11bd <fmturl+0x7d>
mov    %ebp,%r15d
lea    0x1(%r13),%rbp
xor    %ebx,%ebx
lea    0xe8b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
movzbl %al,%esi
mov    %r14,%rdi
call   1040 <strchr@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rbx,%rcx,2),%ebx
add    $0x1,%ebx
movzbl 0x0(%rbp),%eax
add    $0x1,%rbp
test   %al,%al
jne    1190 <fmturl+0x50>
add    $0x1,%ebx
mov    %r15d,%ebp
jmp    11c2 <fmturl+0x82>
mov    $0x1,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     125a <fmturl+0x11a>
mov    %rax,%r14
mov    %r12d,0xc(%rsp)
mov    0x0(%r13),%al
test   %al,%al
je     128d <fmturl+0x14d>
lea    0xe24(%rip),%r15
lea    0xe0c(%rip),%r12
mov    %r14,%rbx
jmp    1213 <fmturl+0xd3>
nopl   0x0(%rax)
mov    %cl,(%rbx)
add    $0x1,%rbx
movzbl 0x1(%r13),%eax
add    $0x1,%r13
test   %al,%al
je     1290 <fmturl+0x150>
movzbl %al,%esi
mov    %r15,%rdi
call   1040 <strchr@plt>
movzbl 0x0(%r13),%ecx
test   %rax,%rax
jne    1200 <fmturl+0xc0>
cmp    $0x20,%cl
jne    1232 <fmturl+0xf2>
movb   $0x2b,(%rbx)
jmp    1202 <fmturl+0xc2>
movb   $0x25,(%rbx)
movzbl 0x0(%r13),%eax
mov    %rax,%rcx
shr    $0x4,%rcx
and    $0xf,%eax
movzbl (%rcx,%r12,1),%ecx
mov    %cl,0x1(%rbx)
movzbl (%rax,%r12,1),%eax
mov    %al,0x2(%rbx)
add    $0x3,%rbx
jmp    1206 <fmturl+0xc6>
mov    0x8(%rsp),%eax
mov    %eax,(%rsp)
mov    0x10(%rsp),%rdi
mov    0x18(%rsp),%rsi
mov    0x20(%rsp),%rdx
mov    %r13,%rcx
mov    %r12d,%r8d
mov    %ebp,%r9d
call   1060 <fmtstr@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rbx
movb   $0x0,(%rbx)
mov    0x8(%rsp),%eax
mov    %eax,(%rsp)
mov    0x10(%rsp),%rdi
mov    0x18(%rsp),%rsi
mov    0x20(%rsp),%rdx
mov    %r14,%rcx
mov    0xc(%rsp),%r8d
mov    %ebp,%r9d
call   1060 <fmtstr@plt>
mov    %r14,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789abcdef\00"
@.str.1 = [75 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_.!*(),/@\\\00"
define void @fmturl(i8* %0, i64* %1, i64 %2, i8* %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca i8*
%9 = alloca i64*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i8*
%19 = alloca i32
%20 = alloca i32
%21 = alloca i8*
%22 = alloca i8*
store i8* %0, i8** %8
store i64* %1, i64** %9
store i64 %2, i64* %10
store i8* %3, i8** %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 %6, i32* %14
store i32 0, i32* %15
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %21
store i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.1, i64 0, i64 0), i8** %22
%23 = load i8*, i8** %11
store i8* %23, i8** %16
br label %24
24: ; preds = %40, %7
%25 = load i8*, i8** %16
%26 = load i8, i8* %25
%27 = icmp ne i8 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %24
%29 = load i8*, i8** %22
%30 = load i8*, i8** %16
%31 = load i8, i8* %30
%32 = call i64 @strchr(i8* %29, i8 zeroext %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i32, i32* %15
%36 = add nsw i32 %35, 1
store i32 %36, i32* %15
br label %40
37: ; preds = %28
%38 = load i32, i32* %15
%39 = add nsw i32 %38, 3
store i32 %39, i32* %15
br label %40
40: ; preds = %37, %34
%41 = load i8*, i8** %16
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %16
br label %24
43: ; preds = %24
%44 = load i32, i32* %15
%45 = add nsw i32 %44, 1
%46 = call i64 @malloc(i32 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %18
%48 = load i8*, i8** %18
%49 = icmp ne i8* %48, null
br i1 %49, label %59, label %50
50: ; preds = %43
%51 = load i8*, i8** %8
%52 = load i64*, i64** %9
%53 = load i64, i64* %10
%54 = load i8*, i8** %11
%55 = load i32, i32* %12
%56 = load i32, i32* %13
%57 = load i32, i32* %14
%58 = call i32 @fmtstr(i8* %51, i64* %52, i64 %53, i8* %54, i32 %55, i32 %56, i32 %57)
br label %130
59: ; preds = %43
%60 = load i8*, i8** %11
store i8* %60, i8** %16
%61 = load i8*, i8** %18
store i8* %61, i8** %17
br label %62
62: ; preds = %117, %59
%63 = load i8*, i8** %16
%64 = load i8, i8* %63
%65 = icmp ne i8 %64, 0
br i1 %65, label %66, label %118
66: ; preds = %62
%67 = load i8*, i8** %22
%68 = load i8*, i8** %16
%69 = load i8, i8* %68
%70 = call i64 @strchr(i8* %67, i8 zeroext %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %78
72: ; preds = %66
%73 = load i8*, i8** %16
%74 = getelementptr inbounds i8, i8* %73, i32 1
store i8* %74, i8** %16
%75 = load i8, i8* %73
%76 = load i8*, i8** %17
%77 = getelementptr inbounds i8, i8* %76, i32 1
store i8* %77, i8** %17
store i8 %75, i8* %76
br label %117
78: ; preds = %66
%79 = load i8*, i8** %16
%80 = load i8, i8* %79
%81 = zext i8 %80 to i32
%82 = icmp eq i32 %81, 32
br i1 %82, label %83, label %89
83: ; preds = %78
%84 = load i8*, i8** %17
%85 = getelementptr inbounds i8, i8* %84, i32 1
store i8* %85, i8** %17
store i8 43, i8* %84
%86 = load i8*, i8** %16
%87 = getelementptr inbounds i8, i8* %86, i32 1
store i8* %87, i8** %16
%88 = load i8, i8* %86
br label %116
89: ; preds = %78
%90 = load i8*, i8** %17
%91 = getelementptr inbounds i8, i8* %90, i32 1
store i8* %91, i8** %17
store i8 37, i8* %90
%92 = load i8*, i8** %16
%93 = load i8, i8* %92
%94 = zext i8 %93 to i32
%95 = sdiv i32 %94, 16
store i32 %95, i32* %19
%96 = load i8*, i8** %16
%97 = load i8, i8* %96
%98 = zext i8 %97 to i32
%99 = srem i32 %98, 16
store i32 %99, i32* %20
%100 = load i8*, i8** %21
%101 = load i32, i32* %19
%102 = zext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %100, i64 %102
%104 = load i8, i8* %103
%105 = load i8*, i8** %17
%106 = getelementptr inbounds i8, i8* %105, i32 1
store i8* %106, i8** %17
store i8 %104, i8* %105
%107 = load i8*, i8** %21
%108 = load i32, i32* %20
%109 = zext i32 %108 to i64
%110 = getelementptr inbounds i8, i8* %107, i64 %109
%111 = load i8, i8* %110
%112 = load i8*, i8** %17
%113 = getelementptr inbounds i8, i8* %112, i32 1
store i8* %113, i8** %17
store i8 %111, i8* %112
%114 = load i8*, i8** %16
%115 = getelementptr inbounds i8, i8* %114, i32 1
store i8* %115, i8** %16
br label %116
116: ; preds = %89, %83
br label %117
117: ; preds = %116, %72
br label %62
118: ; preds = %62
%119 = load i8*, i8** %17
store i8 0, i8* %119
%120 = load i8*, i8** %8
%121 = load i64*, i64** %9
%122 = load i64, i64* %10
%123 = load i8*, i8** %18
%124 = load i32, i32* %12
%125 = load i32, i32* %13
%126 = load i32, i32* %14
%127 = call i32 @fmtstr(i8* %120, i64* %121, i64 %122, i8* %123, i32 %124, i32 %125, i32 %126)
%128 = load i8*, i8** %18
%129 = call i32 @free(i8* %128)
br label %130
130: ; preds = %118, %50
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
@.str.1 = [75 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_.!*(),/@\\\00"
define void @fmturl(i8* %0, i64* %1, i64 %2, i8* %3, i32 %4, i32 %5, i32 %6) {
%8 = load i8, i8* %3
%9 = icmp eq i8 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %7, %10
%11 = phi i8 [ %19, %10 ], [ %8, %7 ]
%12 = phi i32 [ %17, %10 ], [ 0, %7 ]
%13 = phi i8* [ %18, %10 ], [ %3, %7 ]
%14 = tail call i64 @strchr(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %11)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i32 3, i32 1
%17 = add nuw nsw i32 %16, %12
%18 = getelementptr inbounds i8, i8* %13, i64 1
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %10
21: ; preds = %10
%22 = add nuw i32 %17, 1
br label %23
23: ; preds = %21, %7
%24 = phi i32 [ 1, %7 ], [ %22, %21 ]
%25 = tail call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
%27 = icmp eq i64 %25, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i8, i8* %3
%30 = icmp eq i8 %29, 0
br i1 %30, label %64, label %33
31: ; preds = %23
%32 = tail call i32 @fmtstr(i8* %0, i64* %1, i64 %2, i8* %3, i32 %4, i32 %5, i32 %6)
br label %68
33: ; preds = %28, %59
%34 = phi i8 [ %62, %59 ], [ %29, %28 ]
%35 = phi i8* [ %61, %59 ], [ %3, %28 ]
%36 = phi i8* [ %60, %59 ], [ %26, %28 ]
%37 = tail call i64 @strchr(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %34)
%38 = icmp eq i64 %37, 0
%39 = load i8, i8* %35
br i1 %38, label %42, label %40
40: ; preds = %33
%41 = getelementptr inbounds i8, i8* %36, i64 1
store i8 %39, i8* %36
br label %59
42: ; preds = %33
%43 = icmp eq i8 %39, 32
%44 = getelementptr inbounds i8, i8* %36, i64 1
br i1 %43, label %45, label %46
45: ; preds = %42
store i8 43, i8* %36
br label %59
46: ; preds = %42
store i8 37, i8* %36
%47 = load i8, i8* %35
%48 = zext i8 %47 to i32
%49 = lshr i32 %48, 4
%50 = and i32 %48, 15
%51 = zext i32 %49 to i64
%52 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = getelementptr inbounds i8, i8* %36, i64 2
store i8 %53, i8* %44
%55 = zext i32 %50 to i64
%56 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = getelementptr inbounds i8, i8* %36, i64 3
store i8 %57, i8* %54
br label %59
59: ; preds = %45, %46, %40
%60 = phi i8* [ %41, %40 ], [ %44, %45 ], [ %58, %46 ]
%61 = getelementptr inbounds i8, i8* %35, i64 1
%62 = load i8, i8* %61
%63 = icmp eq i8 %62, 0
br i1 %63, label %64, label %33
64: ; preds = %59, %28
%65 = phi i8* [ %26, %28 ], [ %60, %59 ]
store i8 0, i8* %65
%66 = tail call i32 @fmtstr(i8* %0, i64* %1, i64 %2, i8* %26, i32 %4, i32 %5, i32 %6)
%67 = tail call i32 @free(i8* %26)
br label %68
68: ; preds = %64, %31
ret void
}