#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 */
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *buf_tidy(const char *str) {
  int i;
  int n = strlen(str);
  char *ret = malloc(n + 1);
  strcpy(ret, str);
  for (i = 0; i < n; ++i) {
    if (ret[i] == '\t')
      ret[i] = ' ';
    else if (ret[i] < ' ') {
      ret[i] = '\0';
      break;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<buf_tidy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11db <buf_tidy+0xab>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x9,%eax
jne    119d <buf_tidy+0x6d>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
jmp    11c8 <buf_tidy+0x98>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jge    11c3 <buf_tidy+0x93>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11db <buf_tidy+0xab>
jmp    11c8 <buf_tidy+0x98>
jmp    11cd <buf_tidy+0x9d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116b <buf_tidy+0x3b>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_tidy>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
test   %r14d,%r14d
jle    118f <buf_tidy+0x5f>
mov    %r14d,%eax
xor    %ecx,%ecx
jmp    117d <buf_tidy+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movb   $0x20,(%rbx,%rcx,1)
add    $0x1,%rcx
cmp    %rcx,%rax
je     118f <buf_tidy+0x5f>
movzbl (%rbx,%rcx,1),%edx
cmp    $0x9,%dl
je     1170 <buf_tidy+0x40>
cmp    $0x1f,%dl
jg     1174 <buf_tidy+0x44>
movb   $0x0,(%rbx,%rcx,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @buf_tidy(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = add nsw i32 %8, 1
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = load i8*, i8** %2
%13 = call i32 @strcpy(i8* %11, i8* %12)
store i32 0, i32* %3
br label %14
14: ; preds = %46, %1
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %49
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 9
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = load i8*, i8** %5
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 32, i8* %30
br label %45
31: ; preds = %18
%32 = load i8*, i8** %5
%33 = load i32, i32* %3
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp slt i32 %37, 32
br i1 %38, label %39, label %44
39: ; preds = %31
%40 = load i8*, i8** %5
%41 = load i32, i32* %3
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
br label %49
44: ; preds = %31
br label %45
45: ; preds = %44, %26
br label %46
46: ; preds = %45
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %14
49: ; preds = %39, %14
%50 = load i8*, i8** %5
ret i8* %50
}

LLVM IR (-O2):
define i8* @buf_tidy(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = tail call i32 @strcpy(i8* %4, i8* %0)
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %21
7: ; preds = %1
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %18
%10 = phi i64 [ 0, %7 ], [ %19, %18 ]
%11 = getelementptr inbounds i8, i8* %4, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 9
br i1 %13, label %14, label %15
14: ; preds = %9
store i8 32, i8* %11
br label %18
15: ; preds = %9
%16 = icmp slt i8 %12, 32
br i1 %16, label %17, label %18
17: ; preds = %15
store i8 0, i8* %11
br label %21
18: ; preds = %14, %15
%19 = add nuw nsw i64 %10, 1
%20 = icmp eq i64 %19, %8
br i1 %20, label %21, label %9
21: ; preds = %18, %1, %17
ret i8* %4
}