#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 */
char *malloc(int);
char *realloc(char *, short);
scalar_t__ strlen(char *);
char *rewrite_url(char *url) {
  char *newURL = malloc(50);
  short i = 0;
  while (i < (signed)strlen(url) && url[i] != '?')
    newURL[i] = url[i];
  newURL = realloc(newURL, i);
  return newURL;
}


x86 assembly (clang, -O0):
<rewrite_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x32,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movw   $0x0,-0x12(%rbp)
movswl -0x12(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x18(%rbp),%ecx
mov    %eax,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x13(%rbp)
jge    1188 <rewrite_url+0x58>
mov    -0x8(%rbp),%rax
movswq -0x12(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3f,%eax
setne  %al
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
test   $0x1,%al
jne    1198 <rewrite_url+0x68>
jmp    11b5 <rewrite_url+0x85>
mov    -0x8(%rbp),%rax
movswq -0x12(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movswq -0x12(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1150 <rewrite_url+0x20>
mov    -0x10(%rbp),%rdi
movswl -0x12(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewrite_url>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x32,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1156 <rewrite_url+0x26>
mov    (%rbx),%al
cmp    $0x3f,%al
jne    1167 <rewrite_url+0x37>
mov    %r14,%rdi
xor    %esi,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <realloc@plt>
mov    %al,(%r14)
nopw   0x0(%rax,%rax,1)
jmp    1170 <rewrite_url+0x40>

LLVM IR (-O0):
define i8* @rewrite_url(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i16
store i8* %0, i8** %2
%5 = call i8* @malloc(i32 50)
store i8* %5, i8** %3
store i16 0, i16* %4
br label %6
6: ; preds = %23, %1
%7 = load i16, i16* %4
%8 = sext i16 %7 to i32
%9 = load i8*, i8** %2
%10 = call i64 @strlen(i8* %9)
%11 = trunc i64 %10 to i32
%12 = icmp slt i32 %8, %11
br i1 %12, label %13, label %21
13: ; preds = %6
%14 = load i8*, i8** %2
%15 = load i16, i16* %4
%16 = sext i16 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 63
br label %21
21: ; preds = %13, %6
%22 = phi i1 [ false, %6 ], [ %20, %13 ]
br i1 %22, label %23, label %33
23: ; preds = %21
%24 = load i8*, i8** %2
%25 = load i16, i16* %4
%26 = sext i16 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %3
%30 = load i16, i16* %4
%31 = sext i16 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
br label %6
33: ; preds = %21
%34 = load i8*, i8** %3
%35 = load i16, i16* %4
%36 = call i8* @realloc(i8* %34, i16 signext %35)
store i8* %36, i8** %3
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
define i8* @rewrite_url(i8* nocapture %0) {
%2 = tail call dereferenceable_or_null(50) i8* @malloc(i32 50)
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = trunc i64 %3 to i32
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 63
br i1 %8, label %11, label %9
9: ; preds = %6
store i8 %7, i8* %2
br label %10
10: ; preds = %9, %10
br label %10
11: ; preds = %6, %1
%12 = tail call i8* @realloc(i8* %2, i16 signext 0)
ret i8* %12
}