#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*/ strncpy(char *, char const *, int);
int strnlen(char const *, int);
char *yaffs_clone_str(const char *str) {
  char *new_str = (0);
  int len;
  if (!str)
    str = "";
  len = strnlen(str, 159);
  new_str = malloc((len + 1) * sizeof(char));
  if (new_str) {
    strncpy(new_str, str, len);
    new_str[len] = 0;
  }
  return new_str;
}


x86 assembly (clang, -O0):
<yaffs_clone_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    115a <yaffs_clone_str+0x2a>
lea    0xeaa(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x9f,%esi
call   1040 <strnlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a9 <yaffs_clone_str+0x79>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<yaffs_clone_str>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
lea    0xec1(%rip),%r15
cmovne %rdi,%r15
mov    %r15,%rdi
mov    $0x9f,%esi
call   1040 <strnlen@plt>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1179 <yaffs_clone_str+0x49>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @yaffs_clone_str(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %3
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
br label %8
8: ; preds = %7, %1
%9 = load i8*, i8** %2
%10 = call i32 @strnlen(i8* %9, i32 159)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 1
%15 = trunc i64 %14 to i32
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %28
19: ; preds = %8
%20 = load i8*, i8** %3
%21 = load i8*, i8** %2
%22 = load i32, i32* %4
%23 = call i32 @strncpy(i8* %20, i8* %21, i32 %22)
%24 = load i8*, i8** %3
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %19, %8
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @yaffs_clone_str(i8* %0) {
%2 = icmp eq i8* %0, null
%3 = select i1 %2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%4 = tail call i32 @strnlen(i8* %3, i32 159)
%5 = add nsw i32 %4, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = tail call i32 @strncpy(i8* %6, i8* %3, i32 %4)
%10 = sext i32 %4 to i64
%11 = getelementptr inbounds i8, i8* %6, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %8, %1
ret i8* %6
}