#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*/ isalnum(unsigned char);
int /*<<< orphan*/ libcgi_error(int, char *, char *, int);
scalar_t__ malloc(int);
unsigned char *realloc(unsigned char *, int);
int /*<<< orphan*/ *strchr(char *, unsigned char);
int strlen(char *);
char *cgi_escape_special_chars(char *str) {
  unsigned char hex[] = "0123456789ABCDEF";
  register int i, j, len, tmp_len;
  unsigned char *tmp;
  len = strlen(str);
  tmp_len = len;
  tmp = (unsigned char *)malloc(len + 1);
  if (tmp == NULL)
    libcgi_error(4, "%s, line %s",
                 "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                 "outputs/2022-01-22/02-19-57/repos/ingran/balzac/refs/heads/"
                 "master/package/libs/libcgi-1.0/src/cgi.c",
                 442);
  for (i = 0, j = 0; i < len; i++, j++) {
    tmp[j] = (unsigned char)str[i];
    if (tmp[j] == ' ')
      tmp[j] = '+';
    else if (!isalnum(tmp[j]) && strchr("_-.", tmp[j]) == NULL) {
      tmp_len += 3;
      tmp = realloc(tmp, tmp_len);
      if (!tmp)
        libcgi_error(4, "%s, line %s",
                     "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                     "outputs/2022-01-22/02-19-57/repos/ingran/balzac/refs/"
                     "heads/master/package/libs/libcgi-1.0/src/cgi.c",
                     452);
      tmp[j++] = '%';
      tmp[j++] = hex[(unsigned char)str[i] >> 4];
      tmp[j] = hex[(unsigned char)str[i] & 0x0F];
    }
  }
  tmp[j] = '\0';
  return tmp;
}


x86 assembly (clang, -O0):
<cgi_escape_special_chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0xe8d(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe88(%rip),%al
mov    %al,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d4 <cgi_escape_special_chars+0x74>
mov    $0x4,%edi
lea    0xe4e(%rip),%rsi
lea    0xe53(%rip),%rdx
mov    $0x1ba,%ecx
call   1030 <libcgi_error@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1328 <cgi_escape_special_chars+0x1c8>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    122a <cgi_escape_special_chars+0xca>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x2b,(%rax,%rcx,1)
jmp    130c <cgi_escape_special_chars+0x1ac>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1060 <isalnum@plt>
cmp    $0x0,%eax
jne    1307 <cgi_escape_special_chars+0x1a7>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
lea    0xe66(%rip),%rdi
movzbl (%rax,%rcx,1),%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
jne    1307 <cgi_escape_special_chars+0x1a7>
mov    -0x30(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12a7 <cgi_escape_special_chars+0x147>
mov    $0x4,%edi
lea    0xd7b(%rip),%rsi
lea    0xd80(%rip),%rdx
mov    $0x1c4,%ecx
call   1030 <libcgi_error@plt>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x28(%rbp)
movslq %ecx,%rcx
movb   $0x25,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
sar    $0x4,%eax
cltq
mov    -0x20(%rbp,%rax,1),%dl
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    $0xf,%eax
cltq
mov    -0x20(%rbp,%rax,1),%dl
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    130c <cgi_escape_special_chars+0x1ac>
jmp    1311 <cgi_escape_special_chars+0x1b1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11e2 <cgi_escape_special_chars+0x82>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgi_escape_special_chars>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a4 <cgi_escape_special_chars+0x44>
lea    0xe83(%rip),%rsi
lea    0xe88(%rip),%rdx
mov    $0x4,%edi
mov    $0x1ba,%ecx
call   1030 <libcgi_error@plt>
xor    %ebp,%ebp
test   %r15d,%r15d
jle    127a <cgi_escape_special_chars+0x11a>
mov    %r15,(%rsp)
mov    %r15d,%r13d
xor    %r12d,%r12d
jmp    11d5 <cgi_escape_special_chars+0x75>
nopl   0x0(%rax,%rax,1)
movb   $0x2b,(%rbx,%r15,1)
add    $0x1,%r12
add    $0x1,%ebp
cmp    %r12,%r13
je     1277 <cgi_escape_special_chars+0x117>
movzbl (%r14,%r12,1),%eax
movslq %ebp,%r15
mov    %al,(%rbx,%r15,1)
cmp    $0x20,%al
je     11c0 <cgi_escape_special_chars+0x60>
movzbl %al,%edi
call   1060 <isalnum@plt>
test   %eax,%eax
jne    11c5 <cgi_escape_special_chars+0x65>
movzbl (%rbx,%r15,1),%esi
lea    0xebc(%rip),%rdi
call   1050 <strchr@plt>
test   %rax,%rax
jne    11c5 <cgi_escape_special_chars+0x65>
mov    (%rsp),%rsi
add    $0x3,%esi
mov    %rbx,%rdi
mov    %rsi,(%rsp)
call   1080 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    123f <cgi_escape_special_chars+0xdf>
mov    $0x4,%edi
lea    0xde3(%rip),%rsi
lea    0xde8(%rip),%rdx
mov    $0x1c4,%ecx
call   1030 <libcgi_error@plt>
movb   $0x25,(%rbx,%r15,1)
movzbl (%r14,%r12,1),%eax
shr    $0x4,%rax
lea    0xdac(%rip),%rcx
movzbl (%rax,%rcx,1),%eax
lea    0x2(%r15),%rbp
mov    %al,0x1(%r15,%rbx,1)
movzbl (%r14,%r12,1),%eax
and    $0xf,%eax
movzbl (%rax,%rcx,1),%eax
mov    %al,0x2(%rbx,%r15,1)
jmp    11c5 <cgi_escape_special_chars+0x65>
movslq %ebp,%rbp
movb   $0x0,(%rbx,%rbp,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.cgi_escape_special_chars.hex = [17 x i8] c"0123456789ABCDEF\00"
@.str = [12 x i8] c"%s, line %s\00"
@.str.1 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/ingran/balzac/refs/heads/master/package/libs/libcgi-1.0/src/cgi.c\00"
@.str.2 = [4 x i8] c"_-.\00"
define i8* @cgi_escape_special_chars(i8* %0) {
%2 = alloca i8*
%3 = alloca [17 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %2
%9 = bitcast [17 x i8]* %3 to i8*
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
store i32 %12, i32* %7
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 1
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @libcgi_error(i32 4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.1, i64 0, i64 0), i32 442)
br label %21
21: ; preds = %19, %1
store i32 0, i32* %4
store i32 0, i32* %5
br label %22
22: ; preds = %111, %21
%23 = load i32, i32* %4
%24 = load i32, i32* %6
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %116
26: ; preds = %22
%27 = load i8*, i8** %2
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = load i8*, i8** %8
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
%36 = load i8*, i8** %8
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = icmp eq i32 %41, 32
br i1 %42, label %43, label %48
43: ; preds = %26
%44 = load i8*, i8** %8
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 43, i8* %47
br label %110
48: ; preds = %26
%49 = load i8*, i8** %8
%50 = load i32, i32* %5
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = call i32 @isalnum(i8 zeroext %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %109, label %56
56: ; preds = %48
%57 = load i8*, i8** %8
%58 = load i32, i32* %5
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = call i32* @strchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %61)
%63 = icmp eq i32* %62, null
br i1 %63, label %64, label %109
64: ; preds = %56
%65 = load i32, i32* %7
%66 = add nsw i32 %65, 3
store i32 %66, i32* %7
%67 = load i8*, i8** %8
%68 = load i32, i32* %7
%69 = call i8* @realloc(i8* %67, i32 %68)
store i8* %69, i8** %8
%70 = load i8*, i8** %8
%71 = icmp ne i8* %70, null
br i1 %71, label %74, label %72
72: ; preds = %64
%73 = call i32 @libcgi_error(i32 4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.1, i64 0, i64 0), i32 452)
br label %74
74: ; preds = %72, %64
%75 = load i8*, i8** %8
%76 = load i32, i32* %5
%77 = add nsw i32 %76, 1
store i32 %77, i32* %5
%78 = sext i32 %76 to i64
%79 = getelementptr inbounds i8, i8* %75, i64 %78
store i8 37, i8* %79
%80 = load i8*, i8** %2
%81 = load i32, i32* %4
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = load i8, i8* %83
%85 = zext i8 %84 to i32
%86 = ashr i32 %85, 4
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 %87
%89 = load i8, i8* %88
%90 = load i8*, i8** %8
%91 = load i32, i32* %5
%92 = add nsw i32 %91, 1
store i32 %92, i32* %5
%93 = sext i32 %91 to i64
%94 = getelementptr inbounds i8, i8* %90, i64 %93
store i8 %89, i8* %94
%95 = load i8*, i8** %2
%96 = load i32, i32* %4
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i8, i8* %95, i64 %97
%99 = load i8, i8* %98
%100 = zext i8 %99 to i32
%101 = and i32 %100, 15
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 %102
%104 = load i8, i8* %103
%105 = load i8*, i8** %8
%106 = load i32, i32* %5
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %105, i64 %107
store i8 %104, i8* %108
br label %109
109: ; preds = %74, %56, %48
br label %110
110: ; preds = %109, %43
br label %111
111: ; preds = %110
%112 = load i32, i32* %4
%113 = add nsw i32 %112, 1
store i32 %113, i32* %4
%114 = load i32, i32* %5
%115 = add nsw i32 %114, 1
store i32 %115, i32* %5
br label %22
116: ; preds = %22
%117 = load i8*, i8** %8
%118 = load i32, i32* %5
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds i8, i8* %117, i64 %119
store i8 0, i8* %120
%121 = load i8*, i8** %8
ret i8* %121
}

LLVM IR (-O2):
@__const.cgi_escape_special_chars.hex = [17 x i8] c"0123456789ABCDEF\00"
@.str = [12 x i8] c"%s, line %s\00"
@.str.1 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/ingran/balzac/refs/heads/master/package/libs/libcgi-1.0/src/cgi.c\00"
@.str.2 = [4 x i8] c"_-.\00"
define i8* @cgi_escape_special_chars(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @libcgi_error(i32 4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.1, i64 0, i64 0), i32 442)
br label %9
9: ; preds = %7, %1
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %64
11: ; preds = %9
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %11, %55
%14 = phi i64 [ 0, %11 ], [ %59, %55 ]
%15 = phi i8* [ %5, %11 ], [ %58, %55 ]
%16 = phi i32 [ %2, %11 ], [ %57, %55 ]
%17 = phi i32 [ 0, %11 ], [ %60, %55 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %14
%19 = load i8, i8* %18
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds i8, i8* %15, i64 %20
store i8 %19, i8* %21
%22 = icmp eq i8 %19, 32
br i1 %22, label %23, label %24
23: ; preds = %13
store i8 43, i8* %21
br label %55
24: ; preds = %13
%25 = tail call i32 @isalnum(i8 zeroext %19)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %55
27: ; preds = %24
%28 = load i8, i8* %21
%29 = tail call i32* @strchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %28)
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %55
31: ; preds = %27
%32 = add nsw i32 %16, 3
%33 = tail call i8* @realloc(i8* %15, i32 %32)
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = tail call i32 @libcgi_error(i32 4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.1, i64 0, i64 0), i32 452)
br label %37
37: ; preds = %35, %31
%38 = add nsw i32 %17, 1
%39 = getelementptr inbounds i8, i8* %33, i64 %20
store i8 37, i8* %39
%40 = load i8, i8* %18
%41 = lshr i8 %40, 4
%42 = zext i8 %41 to i64
%43 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.cgi_escape_special_chars.hex, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = add nsw i32 %17, 2
%46 = sext i32 %38 to i64
%47 = getelementptr inbounds i8, i8* %33, i64 %46
store i8 %44, i8* %47
%48 = load i8, i8* %18
%49 = and i8 %48, 15
%50 = zext i8 %49 to i64
%51 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.cgi_escape_special_chars.hex, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = sext i32 %45 to i64
%54 = getelementptr inbounds i8, i8* %33, i64 %53
store i8 %52, i8* %54
br label %55
55: ; preds = %23, %37, %27, %24
%56 = phi i32 [ %17, %23 ], [ %17, %24 ], [ %45, %37 ], [ %17, %27 ]
%57 = phi i32 [ %16, %23 ], [ %16, %24 ], [ %32, %37 ], [ %16, %27 ]
%58 = phi i8* [ %15, %23 ], [ %15, %24 ], [ %33, %37 ], [ %15, %27 ]
%59 = add nuw nsw i64 %14, 1
%60 = add nsw i32 %56, 1
%61 = icmp eq i64 %59, %12
br i1 %61, label %62, label %13
62: ; preds = %55
%63 = sext i32 %60 to i64
br label %64
64: ; preds = %62, %9
%65 = phi i64 [ 0, %9 ], [ %63, %62 ]
%66 = phi i8* [ %5, %9 ], [ %58, %62 ]
%67 = getelementptr inbounds i8, i8* %66, i64 %65
store i8 0, i8* %67
ret i8* %66
}