#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__ guac_kubernetes_is_url_safe(char);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int guac_kubernetes_escape_url_component(char *output, int length,
                                         const char *str) {
  char *current = output;
  while (*str != '\0') {
    char c = *str;
    if (guac_kubernetes_is_url_safe(c)) {
      if (length < 1)
        return 1;
      *(current++) = c;
      length--;
    } else {
      if (length < 4)
        return 1;
      snprintf(current, 4, "%%%02X", (int)c);
      current += 3;
      length -= 3;
    }
    str++;
  }
  if (length < 1)
    return 1;
  *current = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<guac_kubernetes_escape_url_component>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f4 <guac_kubernetes_escape_url_component+0xd4>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%edi
call   1030 <guac_kubernetes_is_url_safe@plt>
cmp    $0x0,%rax
je     119f <guac_kubernetes_escape_url_component+0x7f>
cmpl   $0x1,-0x14(%rbp)
jge    117d <guac_kubernetes_escape_url_component+0x5d>
movl   $0x1,-0x4(%rbp)
jmp    1218 <guac_kubernetes_escape_url_component+0xf8>
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11e3 <guac_kubernetes_escape_url_component+0xc3>
cmpl   $0x4,-0x14(%rbp)
jge    11b5 <guac_kubernetes_escape_url_component+0x95>
movl   $0x1,-0x4(%rbp)
jmp    1218 <guac_kubernetes_escape_url_component+0xf8>
mov    -0x28(%rbp),%rdi
movsbl -0x29(%rbp),%ecx
mov    $0x4,%esi
lea    0xe37(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    113b <guac_kubernetes_escape_url_component+0x1b>
cmpl   $0x1,-0x14(%rbp)
jge    120a <guac_kubernetes_escape_url_component+0xea>
movl   $0x1,-0x4(%rbp)
jmp    1218 <guac_kubernetes_escape_url_component+0xf8>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guac_kubernetes_escape_url_component>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r13d
mov    %rdi,%r14
mov    (%rdx),%bl
test   %bl,%bl
je     11a5 <guac_kubernetes_escape_url_component+0x85>
mov    %rdx,%rbp
add    $0x1,%rbp
lea    0xebb(%rip),%r15
jmp    1170 <guac_kubernetes_escape_url_component+0x50>
nopw   0x0(%rax,%rax,1)
test   %r13d,%r13d
jle    11b2 <guac_kubernetes_escape_url_component+0x92>
mov    %bl,(%r14)
add    $0x1,%r14
mov    $0xffffffff,%eax
add    %eax,%r13d
movzbl 0x0(%rbp),%ebx
add    $0x1,%rbp
test   %bl,%bl
je     11a5 <guac_kubernetes_escape_url_component+0x85>
movsbl %bl,%r12d
mov    %r12d,%edi
call   1030 <guac_kubernetes_is_url_safe@plt>
test   %rax,%rax
jne    1150 <guac_kubernetes_escape_url_component+0x30>
cmp    $0x4,%r13d
jl     11b2 <guac_kubernetes_escape_url_component+0x92>
mov    %r14,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
mov    %r12d,%ecx
call   1040 <snprintf@plt>
add    $0x3,%r14
mov    $0xfffffffd,%eax
jmp    1161 <guac_kubernetes_escape_url_component+0x41>
test   %r13d,%r13d
jle    11b2 <guac_kubernetes_escape_url_component+0x92>
movb   $0x0,(%r14)
xor    %eax,%eax
jmp    11b7 <guac_kubernetes_escape_url_component+0x97>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"%%%02X\00"
define i32 @guac_kubernetes_escape_url_component(i8* %0, i32 %1, i8* nocapture %2) {
%4 = load i8, i8* %2
%5 = icmp eq i8 %4, 0
br i1 %5, label %30, label %6
6: ; preds = %3, %23
%7 = phi i8 [ %28, %23 ], [ %4, %3 ]
%8 = phi i8* [ %25, %23 ], [ %0, %3 ]
%9 = phi i8* [ %27, %23 ], [ %2, %3 ]
%10 = phi i32 [ %26, %23 ], [ %1, %3 ]
%11 = sext i8 %7 to i32
%12 = tail call i64 @guac_kubernetes_is_url_safe(i8 signext %7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %6
%15 = icmp slt i32 %10, 1
br i1 %15, label %35, label %16
16: ; preds = %14
%17 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %7, i8* %8
br label %23
18: ; preds = %6
%19 = icmp slt i32 %10, 4
br i1 %19, label %35, label %20
20: ; preds = %18
%21 = tail call i32 @snprintf(i8* %8, i32 4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %11)
%22 = getelementptr inbounds i8, i8* %8, i64 3
br label %23
23: ; preds = %16, %20
%24 = phi i32 [ -1, %16 ], [ -3, %20 ]
%25 = phi i8* [ %17, %16 ], [ %22, %20 ]
%26 = add nsw i32 %10, %24
%27 = getelementptr inbounds i8, i8* %9, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %6
30: ; preds = %23, %3
%31 = phi i32 [ %1, %3 ], [ %26, %23 ]
%32 = phi i8* [ %0, %3 ], [ %25, %23 ]
%33 = icmp slt i32 %31, 1
br i1 %33, label %35, label %34
34: ; preds = %30
store i8 0, i8* %32
br label %35
35: ; preds = %18, %14, %30, %34
%36 = phi i32 [ 0, %34 ], [ 1, %30 ], [ 1, %14 ], [ 1, %18 ]
ret i32 %36
}