char *url_encode(char *str) {
  char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
  if (!buf)
    fatal_int("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/hlzz/dotfiles/refs/heads/master/dev/"
              "netdata/src/url.c",
              __FUNCTION__, 35, "Cannot allocate memory.");
  while (*pstr) {
    if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' ||
        *pstr == '~')
      *pbuf++ = *pstr;
    else if (*pstr == ' ')
      *pbuf++ = '+';
    else
      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
    pstr++;
  }
  *pbuf = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
imul   $0x3,%rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <url_encode+0x62>
lea    0xe68(%rip),%rdi
lea    0xef1(%rip),%rsi
mov    $0x23,%edx
lea    0xef0(%rip),%rcx
mov    $0x0,%al
call   1070 <fatal_int@plt>
jmp    11b7 <url_encode+0x67>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     12cc <url_encode+0x17c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isalnum@plt>
cmp    $0x0,%eax
jne    1219 <url_encode+0xc9>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     1219 <url_encode+0xc9>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
je     1219 <url_encode+0xc9>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1219 <url_encode+0xc9>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    1235 <url_encode+0xe5>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    12bb <url_encode+0x16b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    125c <url_encode+0x10c>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x2b,(%rax)
jmp    12b6 <url_encode+0x166>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x25,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
sar    $0x4,%edi
mov    $0x0,%al
call   1030 <to_hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
and    $0xf,%edi
mov    $0x0,%al
call   1030 <to_hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    12bb <url_encode+0x16b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11b7 <url_encode+0x67>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1040 <strlen@plt>
lea    (%rax,%rax,2),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1196 <url_encode+0x46>
lea    0xe84(%rip),%rdi
lea    0xf0d(%rip),%rsi
lea    0xf11(%rip),%rcx
mov    $0x23,%edx
xor    %eax,%eax
call   1070 <fatal_int@plt>
mov    (%r12),%bl
mov    %r14,%r15
test   %bl,%bl
je     1230 <url_encode+0xe0>
add    $0x1,%r12
mov    %r14,%r15
nopl   0x0(%rax)
movsbl %bl,%edi
call   1050 <isalnum@plt>
test   %eax,%eax
je     11e0 <url_encode+0x90>
mov    %bl,(%r15)
add    $0x1,%r15
movzbl (%r12),%ebx
add    $0x1,%r12
test   %bl,%bl
jne    11b0 <url_encode+0x60>
jmp    1230 <url_encode+0xe0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x5e,%bl
jg     11f7 <url_encode+0xa7>
lea    -0x2d(%rbx),%eax
cmp    $0x2,%al
jb     11bc <url_encode+0x6c>
cmp    $0x20,%bl
jne    1201 <url_encode+0xb1>
movb   $0x2b,(%r15)
jmp    11bf <url_encode+0x6f>
cmp    $0x7e,%bl
je     11bc <url_encode+0x6c>
cmp    $0x5f,%bl
je     11bc <url_encode+0x6c>
movb   $0x25,(%r15)
sar    $0x4,%bl
movsbl %bl,%edi
xor    %eax,%eax
call   1030 <to_hex@plt>
mov    %al,0x1(%r15)
movzbl -0x1(%r12),%edi
and    $0xf,%edi
xor    %eax,%eax
call   1030 <to_hex@plt>
mov    %al,0x2(%r15)
add    $0x3,%r15
jmp    11c3 <url_encode+0x73>
movb   $0x0,(%r15)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [144 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hlzz/dotfiles/refs/heads/master/dev/netdata/src/url.c\00"
@__FUNCTION__.url_encode = [11 x i8] c"url_encode\00"
@.str.1 = [24 x i8] c"Cannot allocate memory.\00"
define i8* @url_encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
store i8* %6, i8** %3
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
%9 = mul i64 %8, 3
%10 = add i64 %9, 1
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
store i8* %12, i8** %5
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = call i32 (i8*, i8*, i32, i8*, ...) bitcast (i32 (...)* @fatal_int to i32 (i8*, i8*, i32, i8*, ...)*)(i8* getelementptr inbounds ([144 x i8], [144 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__FUNCTION__.url_encode, i64 0, i64 0), i32 35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %1
br label %18
18: ; preds = %81, %17
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %84
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = call i32 @isalnum(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %48, label %28
28: ; preds = %22
%29 = load i8*, i8** %3
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 45
br i1 %32, label %48, label %33
33: ; preds = %28
%34 = load i8*, i8** %3
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 95
br i1 %37, label %48, label %38
38: ; preds = %33
%39 = load i8*, i8** %3
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 46
br i1 %42, label %48, label %43
43: ; preds = %38
%44 = load i8*, i8** %3
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 126
br i1 %47, label %48, label %53
48: ; preds = %43, %38, %33, %28, %22
%49 = load i8*, i8** %3
%50 = load i8, i8* %49
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %5
store i8 %50, i8* %51
br label %81
53: ; preds = %43
%54 = load i8*, i8** %3
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 32
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %5
store i8 43, i8* %59
br label %80
61: ; preds = %53
%62 = load i8*, i8** %5
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %5
store i8 37, i8* %62
%64 = load i8*, i8** %3
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = ashr i32 %66, 4
%68 = call i32 (i32, ...) bitcast (i32 (...)* @to_hex to i32 (i32, ...)*)(i32 %67)
%69 = trunc i32 %68 to i8
%70 = load i8*, i8** %5
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %5
store i8 %69, i8* %70
%72 = load i8*, i8** %3
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = and i32 %74, 15
%76 = call i32 (i32, ...) bitcast (i32 (...)* @to_hex to i32 (i32, ...)*)(i32 %75)
%77 = trunc i32 %76 to i8
%78 = load i8*, i8** %5
%79 = getelementptr inbounds i8, i8* %78, i32 1
store i8* %79, i8** %5
store i8 %77, i8* %78
br label %80
80: ; preds = %61, %58
br label %81
81: ; preds = %80, %48
%82 = load i8*, i8** %3
%83 = getelementptr inbounds i8, i8* %82, i32 1
store i8* %83, i8** %3
br label %18
84: ; preds = %18
%85 = load i8*, i8** %5
store i8 0, i8* %85
%86 = load i8*, i8** %4
ret i8* %86
}

LLVM IR (-O2):
@.str = [144 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hlzz/dotfiles/refs/heads/master/dev/netdata/src/url.c\00"
@__FUNCTION__.url_encode = [11 x i8] c"url_encode\00"
@.str.1 = [24 x i8] c"Cannot allocate memory.\00"
define i8* @url_encode(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = mul i64 %2, 3
%4 = add i64 %3, 1
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 (i8*, i8*, i32, i8*, ...) bitcast (i32 (...)* @fatal_int to i32 (i8*, i8*, i32, i8*, ...)*)(i8* getelementptr inbounds ([144 x i8], [144 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__FUNCTION__.url_encode, i64 0, i64 0), i32 35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
br i1 %11, label %43, label %12
12: ; preds = %9, %38
%13 = phi i8 [ %41, %38 ], [ %10, %9 ]
%14 = phi i8* [ %39, %38 ], [ %5, %9 ]
%15 = phi i8* [ %40, %38 ], [ %0, %9 ]
%16 = sext i8 %13 to i32
%17 = tail call i32 @isalnum(i32 %16)
%18 = freeze i32 %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %12
switch i8 %13, label %25 [
i8 126, label %21
i8 95, label %21
i8 46, label %21
i8 45, label %21
i8 32, label %23
]
21: ; preds = %20, %20, %20, %20, %12
%22 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %13, i8* %14
br label %38
23: ; preds = %20
%24 = getelementptr inbounds i8, i8* %14, i64 1
store i8 43, i8* %14
br label %38
25: ; preds = %20
%26 = getelementptr inbounds i8, i8* %14, i64 1
store i8 37, i8* %14
%27 = ashr i8 %13, 4
%28 = sext i8 %27 to i32
%29 = tail call i32 (i32, ...) bitcast (i32 (...)* @to_hex to i32 (i32, ...)*)(i32 %28)
%30 = trunc i32 %29 to i8
%31 = getelementptr inbounds i8, i8* %14, i64 2
store i8 %30, i8* %26
%32 = load i8, i8* %15
%33 = and i8 %32, 15
%34 = zext i8 %33 to i32
%35 = tail call i32 (i32, ...) bitcast (i32 (...)* @to_hex to i32 (i32, ...)*)(i32 %34)
%36 = trunc i32 %35 to i8
%37 = getelementptr inbounds i8, i8* %14, i64 3
store i8 %36, i8* %31
br label %38
38: ; preds = %23, %25, %21
%39 = phi i8* [ %22, %21 ], [ %24, %23 ], [ %37, %25 ]
%40 = getelementptr inbounds i8, i8* %15, i64 1
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 0
br i1 %42, label %43, label %12
43: ; preds = %38, %9
%44 = phi i8* [ %5, %9 ], [ %39, %38 ]
store i8 0, i8* %44
ret i8* %5
}