#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__ strcmp(char const *, char *);
unsigned int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, unsigned int);
char *rewrite_url(char *url) {
  unsigned int idx;
  for (idx = 0; idx < strlen(url); idx++) {
    if (url[idx] == '?')
      break;
  }
  if (idx < strlen(url)) {
    strncpy(url, (const char *)url, idx);
    url[idx] = '\0';
  }
  if (strcmp((const char *)url, "/") == 0)
    return "/index.html";
  return url;
}


x86 assembly (clang, -O0):
<rewrite_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jae    118b <rewrite_url+0x5b>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3f,%eax
jne    1178 <rewrite_url+0x48>
jmp    118b <rewrite_url+0x5b>
jmp    117d <rewrite_url+0x4d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1143 <rewrite_url+0x13>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jae    11c2 <rewrite_url+0x92>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11ec <rewrite_url+0xbc>
lea    0xe1f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11f4 <rewrite_url+0xc4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewrite_url>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     116a <rewrite_url+0x3a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpb   $0x3f,(%r14,%rbx,1)
je     116a <rewrite_url+0x3a>
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jb     1150 <rewrite_url+0x20>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebx
jae    118a <rewrite_url+0x5a>
mov    %r14,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
lea    0xe6f(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
lea    0xe5f(%rip),%rax
cmovne %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [12 x i8] c"/index.html\00"
define i8* @rewrite_url(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
br label %5
5: ; preds = %20, %1
%6 = load i32, i32* %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = icmp ult i32 %6, %8
br i1 %9, label %10, label %23
10: ; preds = %5
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 63
br i1 %17, label %18, label %19
18: ; preds = %10
br label %23
19: ; preds = %10
br label %20
20: ; preds = %19
%21 = load i32, i32* %4
%22 = add i32 %21, 1
store i32 %22, i32* %4
br label %5
23: ; preds = %18, %5
%24 = load i32, i32* %4
%25 = load i8*, i8** %3
%26 = call i32 @strlen(i8* %25)
%27 = icmp ult i32 %24, %26
br i1 %27, label %28, label %37
28: ; preds = %23
%29 = load i8*, i8** %3
%30 = load i8*, i8** %3
%31 = load i32, i32* %4
%32 = call i32 @strncpy(i8* %29, i8* %30, i32 %31)
%33 = load i8*, i8** %3
%34 = load i32, i32* %4
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %28, %23
%38 = load i8*, i8** %3
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %44
42: ; preds = %37
%43 = load i8*, i8** %3
store i8* %43, i8** %2
br label %44
44: ; preds = %42, %41
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [12 x i8] c"/index.html\00"
define i8* @rewrite_url(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1, %9
%5 = phi i64 [ %10, %9 ], [ 0, %1 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 63
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = add nuw nsw i64 %5, 1
%11 = tail call i32 @strlen(i8* %0)
%12 = zext i32 %11 to i64
%13 = icmp ult i64 %10, %12
br i1 %13, label %4, label %14
14: ; preds = %4, %9
%15 = phi i64 [ %10, %9 ], [ %5, %4 ]
%16 = trunc i64 %15 to i32
br label %17
17: ; preds = %14, %1
%18 = phi i32 [ 0, %1 ], [ %16, %14 ]
%19 = tail call i32 @strlen(i8* %0)
%20 = icmp ult i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = tail call i32 @strncpy(i8* %0, i8* %0, i32 %18)
%23 = zext i32 %18 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %21, %17
%26 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
%28 = select i1 %27, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0
ret i8* %28
}