#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 */
typedef size_t uint;

/* Variables and functions */
char *malloc(int);
char *st_text_copy_allocate(char *source) {
  uint i;
  char *text;
  for (i = 0; source[i] != 0; i++)
    ;
  text = malloc((sizeof *text) * (i + 1));
  for (i = 0; source[i] != 0; i++)
    text[i] = source[i];
  text[i] = 0;
  return text;
}


x86 assembly (clang, -O0):
<st_text_copy_allocate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     114f <st_text_copy_allocate+0x3f>
jmp    113e <st_text_copy_allocate+0x2e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1124 <st_text_copy_allocate+0x14>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11aa <st_text_copy_allocate+0x9a>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    116e <st_text_copy_allocate+0x5e>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<st_text_copy_allocate>:
push   %rbx
mov    %rdi,%rbx
xor    %edi,%edi
cs nopw 0x0(%rax,%rax,1)
cmpb   $0x0,(%rbx,%rdi,1)
lea    0x1(%rdi),%rdi
jne    1120 <st_text_copy_allocate+0x10>
call   1030 <malloc@plt>
mov    (%rbx),%dl
xor    %ecx,%ecx
test   %dl,%dl
je     1150 <st_text_copy_allocate+0x40>
nopw   0x0(%rax,%rax,1)
mov    %dl,(%rax,%rcx,1)
movzbl 0x1(%rbx,%rcx,1),%edx
add    $0x1,%rcx
test   %dl,%dl
jne    1140 <st_text_copy_allocate+0x30>
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
ret

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

LLVM IR (-O2):
define noalias i8* @st_text_copy_allocate(i8* nocapture %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ 0, %1 ], [ %7, %2 ]
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
%7 = add i64 %3, 1
br i1 %6, label %8, label %2
8: ; preds = %2
%9 = trunc i64 %3 to i32
%10 = add i32 %9, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %8, %14
%15 = phi i8 [ %20, %14 ], [ %12, %8 ]
%16 = phi i64 [ %18, %14 ], [ 0, %8 ]
%17 = getelementptr inbounds i8, i8* %11, i64 %16
store i8 %15, i8* %17
%18 = add i64 %16, 1
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %14
22: ; preds = %14, %8
%23 = phi i64 [ 0, %8 ], [ %18, %14 ]
%24 = getelementptr inbounds i8, i8* %11, i64 %23
store i8 0, i8* %24
ret i8* %11
}