#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 */
unsigned char *grealloc(unsigned char *, int);
scalar_t__ isupper(unsigned char);
int strlen(char *);
unsigned char tolower(unsigned char);
char *canontoken(char *str) {
  static unsigned char *canon;
  static int allocated;
  unsigned char c, *p, *q;
  int len;
  p = (unsigned char *)str;
  len = strlen(str);
  if (len >= allocated) {
    allocated = len + 1 + 10;
    canon = grealloc(canon, allocated);
    if (!canon)
      return NULL;
  }
  q = (unsigned char *)canon;
  while ((c = *p++)) {
    if (isupper(c))
      c = tolower(c);
    *q++ = c;
  }
  *q = '\0';
  return (char *)canon;
}


x86 assembly (clang, -O0):
<canontoken>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    0x2ee7(%rip),%eax
jl     11b7 <canontoken+0x77>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
add    $0xa,%eax
mov    %eax,0x2ed2(%rip)
mov    0x2ec3(%rip),%rdi
mov    0x2ec5(%rip),%esi
call   1030 <grealloc@plt>
mov    %rax,0x2eb1(%rip)
cmpq   $0x0,0x2ea9(%rip)
jne    11b2 <canontoken+0x72>
movq   $0x0,-0x8(%rbp)
jmp    1228 <canontoken+0xe8>
jmp    11b7 <canontoken+0x77>
mov    0x2e8a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%al
mov    %al,-0x11(%rbp)
cmp    $0x0,%al
je     1216 <canontoken+0xd6>
movzbl -0x11(%rbp),%edi
call   1060 <isupper@plt>
cmp    $0x0,%rax
je     11fd <canontoken+0xbd>
movzbl -0x11(%rbp),%edi
call   1050 <tolower@plt>
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    11c2 <canontoken+0x82>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e24(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canontoken>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
cmp    0x2efb(%rip),%eax
jge    1160 <canontoken+0x20>
mov    0x2eea(%rip),%r15
jmp    1186 <canontoken+0x46>
add    $0xb,%eax
mov    %eax,0x2ee7(%rip)
mov    0x2ed8(%rip),%rdi
mov    %eax,%esi
call   1030 <grealloc@plt>
mov    %rax,%r15
mov    %rax,0x2ec7(%rip)
test   %rax,%rax
je     11df <canontoken+0x9f>
mov    (%r14),%bl
test   %bl,%bl
je     11d6 <canontoken+0x96>
add    $0x1,%r14
jmp    11b3 <canontoken+0x73>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %bl,(%r15)
add    $0x1,%r15
movzbl (%r14),%ebx
add    $0x1,%r14
test   %bl,%bl
je     11cd <canontoken+0x8d>
movzbl %bl,%ebp
mov    %ebp,%edi
call   1060 <isupper@plt>
test   %rax,%rax
je     11a0 <canontoken+0x60>
mov    %ebp,%edi
call   1050 <tolower@plt>
mov    %eax,%ebx
jmp    11a0 <canontoken+0x60>
mov    0x2e74(%rip),%rax
jmp    11d9 <canontoken+0x99>
mov    %r15,%rax
movb   $0x0,(%r15)
jmp    11e1 <canontoken+0xa1>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@canontoken.canon = internal global i8* null
@canontoken.allocated = internal global i32 0
define i8* @canontoken(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
store i8* %8, i8** %5
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = load i32, i32* @canontoken.allocated
%13 = icmp sge i32 %11, %12
br i1 %13, label %14, label %25
14: ; preds = %1
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
%17 = add nsw i32 %16, 10
store i32 %17, i32* @canontoken.allocated
%18 = load i8*, i8** @canontoken.canon
%19 = load i32, i32* @canontoken.allocated
%20 = call i8* @grealloc(i8* %18, i32 %19)
store i8* %20, i8** @canontoken.canon
%21 = load i8*, i8** @canontoken.canon
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %14
store i8* null, i8** %2
br label %46
24: ; preds = %14
br label %25
25: ; preds = %24, %1
%26 = load i8*, i8** @canontoken.canon
store i8* %26, i8** %6
br label %27
27: ; preds = %39, %25
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
%30 = load i8, i8* %28
store i8 %30, i8* %4
%31 = icmp ne i8 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %27
%33 = load i8, i8* %4
%34 = call i64 @isupper(i8 zeroext %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i8, i8* %4
%38 = call zeroext i8 @tolower(i8 zeroext %37)
store i8 %38, i8* %4
br label %39
39: ; preds = %36, %32
%40 = load i8, i8* %4
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %6
store i8 %40, i8* %41
br label %27
43: ; preds = %27
%44 = load i8*, i8** %6
store i8 0, i8* %44
%45 = load i8*, i8** @canontoken.canon
store i8* %45, i8** %2
br label %46
46: ; preds = %43, %23
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@canontoken.canon = internal unnamed_addr global i8* null
@canontoken.allocated = internal unnamed_addr global i32 0
define i8* @canontoken(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = load i32, i32* @canontoken.allocated
%4 = icmp slt i32 %2, %3
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = load i8*, i8** @canontoken.canon
br label %12
7: ; preds = %1
%8 = add nsw i32 %2, 11
store i32 %8, i32* @canontoken.allocated
%9 = load i8*, i8** @canontoken.canon
%10 = tail call i8* @grealloc(i8* %9, i32 %8)
store i8* %10, i8** @canontoken.canon
%11 = icmp eq i8* %10, null
br i1 %11, label %35, label %12
12: ; preds = %5, %7
%13 = phi i8* [ %6, %5 ], [ %10, %7 ]
%14 = load i8, i8* %0
%15 = icmp eq i8 %14, 0
br i1 %15, label %32, label %16
16: ; preds = %12, %25
%17 = phi i8 [ %28, %25 ], [ %14, %12 ]
%18 = phi i8* [ %20, %25 ], [ %0, %12 ]
%19 = phi i8* [ %27, %25 ], [ %13, %12 ]
%20 = getelementptr inbounds i8, i8* %18, i64 1
%21 = tail call i64 @isupper(i8 zeroext %17)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %16
%24 = tail call zeroext i8 @tolower(i8 zeroext %17)
br label %25
25: ; preds = %23, %16
%26 = phi i8 [ %24, %23 ], [ %17, %16 ]
%27 = getelementptr inbounds i8, i8* %19, i64 1
store i8 %26, i8* %19
%28 = load i8, i8* %20
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %16
30: ; preds = %25
%31 = load i8*, i8** @canontoken.canon
br label %32
32: ; preds = %30, %12
%33 = phi i8* [ %13, %12 ], [ %31, %30 ]
%34 = phi i8* [ %13, %12 ], [ %27, %30 ]
store i8 0, i8* %34
br label %35
35: ; preds = %7, %32
%36 = phi i8* [ %33, %32 ], [ null, %7 ]
ret i8* %36
}