#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 *cli_malloc(size_t const);
size_t strlen(char const *);
unsigned char toupper(unsigned char const);
unsigned char *normalize_encoding(const unsigned char *enc) {
  unsigned char *norm;
  size_t i;
  const size_t len = strlen((const char *)enc);
  norm = cli_malloc(len + 1);
  if (!norm)
    return NULL;
  if (enc == (const unsigned char *)"OTHER")
    enc = (const unsigned char *)"ISO-8859-1";
  for (i = 0; i < strlen((const char *)enc); i++)
    norm[i] = toupper(enc[i]);
  norm[len] = '\0';
  return norm;
}


x86 assembly (clang, -O0):
<normalize_encoding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <cli_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1172 <normalize_encoding+0x42>
movq   $0x0,-0x8(%rbp)
jmp    11fa <normalize_encoding+0xca>
lea    0xe87(%rip),%rax
cmp    %rax,-0x10(%rbp)
jne    118e <normalize_encoding+0x5e>
lea    0xe7c(%rip),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jae    11e6 <normalize_encoding+0xb6>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1196 <normalize_encoding+0x66>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalize_encoding>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %rax,%r14
lea    0x1(%rax),%rdi
call   1050 <cli_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11b5 <normalize_encoding+0x85>
lea    0xea4(%rip),%rax
cmp    %rax,%r12
lea    0xea0(%rip),%r13
cmovne %r12,%r13
mov    0x0(%r13),%al
test   %al,%al
je     11b0 <normalize_encoding+0x80>
movzbl %al,%edi
call   1030 <toupper@plt>
mov    %al,(%r15)
mov    %r13,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%rax
jb     11b0 <normalize_encoding+0x80>
mov    $0x1,%ebx
movzbl 0x0(%r13,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %r13,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbx
jb     1190 <normalize_encoding+0x60>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"OTHER\00"
@.str.1 = [11 x i8] c"ISO-8859-1\00"
define i8* @normalize_encoding(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = add i64 %9, 1
%11 = call i8* @cli_malloc(i64 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %1
store i8* null, i8** %2
br label %42
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = icmp eq i8* %16, getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0)
br i1 %17, label %18, label %19
18: ; preds = %15
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %19
19: ; preds = %18, %15
store i64 0, i64* %5
br label %20
20: ; preds = %34, %19
%21 = load i64, i64* %5
%22 = load i8*, i8** %3
%23 = call i64 @strlen(i8* %22)
%24 = icmp ult i64 %21, %23
br i1 %24, label %25, label %37
25: ; preds = %20
%26 = load i8*, i8** %3
%27 = load i64, i64* %5
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8, i8* %28
%30 = call zeroext i8 @toupper(i8 zeroext %29)
%31 = load i8*, i8** %4
%32 = load i64, i64* %5
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 %30, i8* %33
br label %34
34: ; preds = %25
%35 = load i64, i64* %5
%36 = add i64 %35, 1
store i64 %36, i64* %5
br label %20
37: ; preds = %20
%38 = load i8*, i8** %4
%39 = load i64, i64* %6
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %4
store i8* %41, i8** %2
br label %42
42: ; preds = %37, %14
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@.str = [6 x i8] c"OTHER\00"
@.str.1 = [11 x i8] c"ISO-8859-1\00"
define i8* @normalize_encoding(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, 1
%4 = tail call i8* @cli_malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %26, label %6
6: ; preds = %1
%7 = icmp eq i8* %0, getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0)
%8 = select i1 %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %0
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %24, label %11
11: ; preds = %6
%12 = tail call zeroext i8 @toupper(i8 zeroext %9)
store i8 %12, i8* %4
%13 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%14 = icmp ugt i64 %13, 1
br i1 %14, label %15, label %24
15: ; preds = %11, %15
%16 = phi i64 [ %21, %15 ], [ 1, %11 ]
%17 = getelementptr inbounds i8, i8* %8, i64 %16
%18 = load i8, i8* %17
%19 = tail call zeroext i8 @toupper(i8 zeroext %18)
%20 = getelementptr inbounds i8, i8* %4, i64 %16
store i8 %19, i8* %20
%21 = add nuw i64 %16, 1
%22 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%23 = icmp ult i64 %21, %22
br i1 %23, label %15, label %24
24: ; preds = %15, %11, %6
%25 = getelementptr inbounds i8, i8* %4, i64 %2
store i8 0, i8* %25
br label %26
26: ; preds = %1, %24
ret i8* %4
}