#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ isupper(unsigned char);
unsigned char *malloc(scalar_t__);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ tolower(int /*<<< orphan*/);
unsigned char *clean_lower(unsigned char *s, void *opts) {
  unsigned char *output, *input_walk, *output_walk;
  if (s == NULL) {
    return NULL;
  }
  output = malloc(strlen((const char *)s) + 1);
  if (output == NULL) {
    fprintf(stderr, "out of memory: %s\n", strerror(errno));
    return NULL;
  }
  input_walk = s;
  output_walk = output;
  while (*input_walk != '\0') {
    if (isupper(*input_walk)) {
      *output_walk++ = tolower(*input_walk++);
    } else {
      *output_walk++ = *input_walk++;
    }
  }
  *output_walk = '\0';
  return output;
}


x86 assembly (clang, -O0):
<clean_lower>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1188 <clean_lower+0x28>
movq   $0x0,-0x8(%rbp)
jmp    1280 <clean_lower+0x120>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e5 <clean_lower+0x85>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe2d(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1280 <clean_lower+0x120>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     1271 <clean_lower+0x111>
mov    -0x28(%rbp),%rax
movzbl (%rax),%edi
call   1070 <isupper@plt>
cmp    $0x0,%rax
je     124a <clean_lower+0xea>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movzbl (%rax),%edi
call   1050 <tolower@plt>
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    126c <clean_lower+0x10c>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    11f5 <clean_lower+0x95>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clean_lower>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11fe <clean_lower+0x9e>
mov    %rdi,%r15
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d6 <clean_lower+0x76>
mov    %rax,%r14
mov    (%r15),%al
mov    %r14,%rbx
test   %al,%al
je     11d1 <clean_lower+0x71>
add    $0x1,%r15
mov    %r14,%rbx
jmp    11b2 <clean_lower+0x52>
nopw   0x0(%rax,%rax,1)
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl (%r15),%eax
add    $0x1,%r15
test   %al,%al
je     11d1 <clean_lower+0x71>
movzbl %al,%edi
call   1070 <isupper@plt>
mov    %rax,%rcx
movzbl -0x1(%r15),%eax
test   %rcx,%rcx
je     11a0 <clean_lower+0x40>
movzbl %al,%edi
call   1050 <tolower@plt>
jmp    11a0 <clean_lower+0x40>
movb   $0x0,(%rbx)
jmp    1201 <clean_lower+0xa1>
mov    0x2e03(%rip),%rax
mov    (%rax),%ebx
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe0c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"out of memory: %s\0A\00"
@errno = global i32 0
define i8* @clean_lower(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %3
br label %56
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
%15 = add nsw i64 %14, 1
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %22)
store i8* null, i8** %3
br label %56
24: ; preds = %12
%25 = load i8*, i8** %4
store i8* %25, i8** %7
%26 = load i8*, i8** %6
store i8* %26, i8** %8
br label %27
27: ; preds = %52, %24
%28 = load i8*, i8** %7
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %53
32: ; preds = %27
%33 = load i8*, i8** %7
%34 = load i8, i8* %33
%35 = call i64 @isupper(i8 zeroext %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %32
%38 = load i8*, i8** %7
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %7
%40 = load i8, i8* %38
%41 = zext i8 %40 to i32
%42 = call i32 @tolower(i32 %41)
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %8
store i8 %43, i8* %44
br label %52
46: ; preds = %32
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %7
%49 = load i8, i8* %47
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %8
store i8 %49, i8* %50
br label %52
52: ; preds = %46, %37
br label %27
53: ; preds = %27
%54 = load i8*, i8** %8
store i8 0, i8* %54
%55 = load i8*, i8** %6
store i8* %55, i8** %3
br label %56
56: ; preds = %53, %19, %11
%57 = load i8*, i8** %3
ret i8* %57
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"out of memory: %s\0A\00"
@errno = global i32 0
define noalias i8* @clean_lower(i8* %0, i8* nocapture readnone %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %36, label %4
4: ; preds = %2
%5 = tail call i64 @strlen(i8* %0)
%6 = add nsw i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %4
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
br i1 %11, label %34, label %17
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %36
17: ; preds = %9, %28
%18 = phi i8 [ %32, %28 ], [ %10, %9 ]
%19 = phi i8* [ %30, %28 ], [ %7, %9 ]
%20 = phi i8* [ %31, %28 ], [ %0, %9 ]
%21 = tail call i64 @isupper(i8 zeroext %18)
%22 = icmp eq i64 %21, 0
%23 = load i8, i8* %20
br i1 %22, label %28, label %24
24: ; preds = %17
%25 = zext i8 %23 to i32
%26 = tail call i32 @tolower(i32 %25)
%27 = trunc i32 %26 to i8
br label %28
28: ; preds = %17, %24
%29 = phi i8 [ %27, %24 ], [ %23, %17 ]
%30 = getelementptr inbounds i8, i8* %19, i64 1
%31 = getelementptr inbounds i8, i8* %20, i64 1
store i8 %29, i8* %19
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
br i1 %33, label %34, label %17
34: ; preds = %28, %9
%35 = phi i8* [ %7, %9 ], [ %30, %28 ]
store i8 0, i8* %35
br label %36
36: ; preds = %2, %34, %12
%37 = phi i8* [ null, %12 ], [ %7, %34 ], [ null, %2 ]
ret i8* %37
}