#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*/ convert_to_lowercase_n(unsigned char *, int);
int /*<<< orphan*/ debug(char *, char *);
int /*<<< orphan*/ find_domain(char *, char *, int);
int /*<<< orphan*/ strcasesandr(char *, int, char *, char *);
char *strchr(char *, char);
int strchrcount(char *, char);
int /*<<< orphan*/ strlcat(char *, char *, int);
char *strstr(char *, char *);
int url_normalization(char url[], int urlsize) {
  char domain[urlsize];
  char *cpnt;
  int subdomains;
  if ((cpnt = strchr((url + 7), '#')) != NULL) {
    cpnt[0] = '\0';
  }
  if ((cpnt = strstr((url + 7), ":80/")) != NULL) {
    strcasesandr(url, urlsize, ":80/", "/");
  }
  if (((cpnt = strchr((url + 7), '/')) == NULL) &&
      ((cpnt = strchr((url + 7), '?')) == NULL)) {
    strlcat(url, "/", urlsize);
  }
  if (((cpnt = strchr((url + 7), '/')) == NULL) &&
      ((cpnt = strchr((url + 7), '?')) != NULL)) {
    strcasesandr(url, urlsize, "?", "/?");
  }
  if (!find_domain(url, domain, urlsize)) {
    debug("gyldig_url: can't find domain. Url \"%s\"\n", url);
    return 0;
  }
  subdomains = strchrcount(domain, '.');
  if ((cpnt = strchr((url + 7), '/')) != NULL) {
    convert_to_lowercase_n((unsigned char *)url, (cpnt - url));
  }
  return 1;
}


x86 assembly (clang, -O0):
<url_normalization>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
mov    $0x23,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11dc <url_normalization+0x5c>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
lea    0xe15(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1218 <url_normalization+0x98>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    0xdf4(%rip),%rdx
lea    0xdf0(%rip),%rcx
call   1030 <strcasesandr@plt>
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    126b <url_normalization+0xeb>
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
mov    $0x3f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    126b <url_normalization+0xeb>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd9d(%rip),%rsi
call   1040 <strlcat@plt>
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12c5 <url_normalization+0x145>
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
mov    $0x3f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12c5 <url_normalization+0x145>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    0xd4d(%rip),%rdx
lea    0xd45(%rip),%rcx
call   1030 <strcasesandr@plt>
mov    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
call   1080 <find_domain@plt>
cmp    $0x0,%eax
jne    1301 <url_normalization+0x181>
mov    -0x10(%rbp),%rsi
lea    0xd1f(%rip),%rdi
call   1050 <debug@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1356 <url_normalization+0x1d6>
mov    -0x40(%rbp),%rdi
mov    $0x2e,%esi
call   1090 <strchrcount@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1348 <url_normalization+0x1c8>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1070 <convert_to_lowercase_n@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_normalization>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%r14
mov    %esi,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0x7(%rdi),%rbx
mov    %rbx,%rdi
mov    $0x23,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11bd <url_normalization+0x3d>
movb   $0x0,(%rax)
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11ea <url_normalization+0x6a>
lea    0xe28(%rip),%rdx
lea    0xe24(%rip),%rcx
mov    %r14,%rdi
mov    %r12d,%esi
call   1030 <strcasesandr@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
jne    1220 <url_normalization+0xa0>
mov    %rbx,%rdi
mov    $0x3f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
jne    1220 <url_normalization+0xa0>
lea    0xdee(%rip),%rsi
mov    %r14,%rdi
mov    %r12d,%edx
call   1040 <strlcat@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
jne    125d <url_normalization+0xdd>
mov    %rbx,%rdi
mov    $0x3f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     125d <url_normalization+0xdd>
lea    0xdbb(%rip),%rdx
lea    0xdb3(%rip),%rcx
mov    %r14,%rdi
mov    %r12d,%esi
call   1030 <strcasesandr@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1080 <find_domain@plt>
test   %eax,%eax
je     12a2 <url_normalization+0x122>
mov    %r15,%rdi
mov    $0x2e,%esi
call   1090 <strchrcount@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     12b3 <url_normalization+0x133>
sub    %r14d,%eax
mov    %r14,%rdi
mov    %eax,%esi
call   1070 <convert_to_lowercase_n@plt>
jmp    12b3 <url_normalization+0x133>
lea    0xd5f(%rip),%rdi
mov    %r14,%rsi
call   1050 <debug@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c":80/\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c"?\00"
@.str.3 = [3 x i8] c"/?\00"
@.str.4 = [41 x i8] c"gyldig_url: can't find domain. Url \22%s\22\0A\00"
define i32 @url_normalization(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* %5
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i64 7
%17 = call i8* @strchr(i8* %16, i8 signext 35)
store i8* %17, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i8*, i8** %8
%21 = getelementptr inbounds i8, i8* %20, i64 0
store i8 0, i8* %21
br label %22
22: ; preds = %19, %2
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i64 7
%25 = call i8* @strstr(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = load i32, i32* %5
%30 = call i32 @strcasesandr(i8* %28, i32 %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %27, %22
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i64 7
%34 = call i8* @strchr(i8* %33, i8 signext 47)
store i8* %34, i8** %8
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %45
36: ; preds = %31
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 7
%39 = call i8* @strchr(i8* %38, i8 signext 63)
store i8* %39, i8** %8
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i8*, i8** %4
%43 = load i32, i32* %5
%44 = call i32 @strlcat(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %41, %36, %31
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i64 7
%48 = call i8* @strchr(i8* %47, i8 signext 47)
store i8* %48, i8** %8
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %59
50: ; preds = %45
%51 = load i8*, i8** %4
%52 = getelementptr inbounds i8, i8* %51, i64 7
%53 = call i8* @strchr(i8* %52, i8 signext 63)
store i8* %53, i8** %8
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = load i8*, i8** %4
%57 = load i32, i32* %5
%58 = call i32 @strcasesandr(i8* %56, i32 %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %55, %50, %45
%60 = load i8*, i8** %4
%61 = load i32, i32* %5
%62 = call i32 @find_domain(i8* %60, i8* %14, i32 %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %67, label %64
64: ; preds = %59
%65 = load i8*, i8** %4
%66 = call i32 @debug(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %65)
store i32 0, i32* %3
store i32 1, i32* %10
br label %83
67: ; preds = %59
%68 = call i32 @strchrcount(i8* %14, i8 signext 46)
store i32 %68, i32* %9
%69 = load i8*, i8** %4
%70 = getelementptr inbounds i8, i8* %69, i64 7
%71 = call i8* @strchr(i8* %70, i8 signext 47)
store i8* %71, i8** %8
%72 = icmp ne i8* %71, null
br i1 %72, label %73, label %82
73: ; preds = %67
%74 = load i8*, i8** %4
%75 = load i8*, i8** %8
%76 = load i8*, i8** %4
%77 = ptrtoint i8* %75 to i64
%78 = ptrtoint i8* %76 to i64
%79 = sub i64 %77, %78
%80 = trunc i64 %79 to i32
%81 = call i32 @convert_to_lowercase_n(i8* %74, i32 %80)
br label %82
82: ; preds = %73, %67
store i32 1, i32* %3
store i32 1, i32* %10
br label %83
83: ; preds = %82, %64
%84 = load i8*, i8** %6
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@.str = [5 x i8] c":80/\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c"?\00"
@.str.3 = [3 x i8] c"/?\00"
@.str.4 = [41 x i8] c"gyldig_url: can't find domain. Url \22%s\22\0A\00"
define i32 @url_normalization(i8* %0, i32 %1) {
%3 = zext i32 %1 to i64
%4 = alloca i8, i64 %3
%5 = getelementptr inbounds i8, i8* %0, i64 7
%6 = tail call i8* @strchr(i8* %5, i8 signext 35)
%7 = icmp eq i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %2
store i8 0, i8* %6
br label %9
9: ; preds = %8, %2
%10 = tail call i8* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @strcasesandr(i8* %0, i32 %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = tail call i8* @strchr(i8* %5, i8 signext 47)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = tail call i8* @strchr(i8* %5, i8 signext 63)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @strlcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %22
22: ; preds = %20, %17, %14
%23 = tail call i8* @strchr(i8* %5, i8 signext 47)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = tail call i8* @strchr(i8* %5, i8 signext 63)
%27 = icmp eq i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = tail call i32 @strcasesandr(i8* %0, i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %25, %22
%31 = call i32 @find_domain(i8* %0, i8* %4, i32 %1)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @debug(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %45
35: ; preds = %30
%36 = call i32 @strchrcount(i8* %4, i8 signext 46)
%37 = call i8* @strchr(i8* %5, i8 signext 47)
%38 = icmp eq i8* %37, null
br i1 %38, label %45, label %39
39: ; preds = %35
%40 = ptrtoint i8* %37 to i64
%41 = ptrtoint i8* %0 to i64
%42 = sub i64 %40, %41
%43 = trunc i64 %42 to i32
%44 = call i32 @convert_to_lowercase_n(i8* %0, i32 %43)
br label %45
45: ; preds = %35, %39, %33
%46 = phi i32 [ 0, %33 ], [ 1, %39 ], [ 1, %35 ]
ret i32 %46
}