#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 *_pico_alloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *_pico_clone_alloc(char *str, int size) {
  char *cloned;
  size_t cloneSize;
  if (str == NULL) {
    return NULL;
  }
  cloneSize = (size < 0) ? strlen(str) : size;
  cloned = _pico_alloc(cloneSize + 1);
  if (cloned == NULL) {
    return NULL;
  }
  memset(cloned, 0, cloneSize);
  if (cloneSize < strlen(str)) {
    memcpy(cloned, str, cloneSize);
    cloned[cloneSize] = '\0';
  } else {
    strcpy(cloned, str);
  }
  return cloned;
}


x86 assembly (clang, -O0):
<_pico_clone_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1177 <_pico_clone_alloc+0x27>
movq   $0x0,-0x8(%rbp)
jmp    1233 <_pico_clone_alloc+0xe3>
cmpl   $0x0,-0x14(%rbp)
jge    1193 <_pico_clone_alloc+0x43>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
jmp    119b <_pico_clone_alloc+0x4b>
movslq -0x14(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <_pico_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cc <_pico_clone_alloc+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    1233 <_pico_clone_alloc+0xe3>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jae    121e <_pico_clone_alloc+0xce>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    122b <_pico_clone_alloc+0xdb>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_pico_clone_alloc>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11b1 <_pico_clone_alloc+0x61>
mov    %rdi,%r14
test   %esi,%esi
js     1166 <_pico_clone_alloc+0x16>
mov    %esi,%r15d
jmp    1171 <_pico_clone_alloc+0x21>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x1(%r15),%rdi
call   1070 <_pico_alloc@plt>
test   %rax,%rax
je     11b1 <_pico_clone_alloc+0x61>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1050 <memset@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
cmp    %rax,%r15
jae    11b5 <_pico_clone_alloc+0x65>
mov    %r15,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    11ba <_pico_clone_alloc+0x6a>
xor    %ebx,%ebx
jmp    11ba <_pico_clone_alloc+0x6a>
call   1030 <strcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @_pico_clone_alloc(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %24, label %4
4: ; preds = %2
%5 = icmp slt i32 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i64 @strlen(i8* %0)
br label %10
8: ; preds = %4
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %6
%11 = phi i64 [ %7, %6 ], [ %9, %8 ]
%12 = add i64 %11, 1
%13 = tail call i8* @_pico_alloc(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %24, label %15
15: ; preds = %10
%16 = tail call i32 @memset(i8* %13, i32 0, i64 %11)
%17 = tail call i64 @strlen(i8* %0)
%18 = icmp ult i64 %11, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i32 @memcpy(i8* %13, i8* %0, i64 %11)
%21 = getelementptr inbounds i8, i8* %13, i64 %11
store i8 0, i8* %21
br label %24
22: ; preds = %15
%23 = tail call i32 @strcpy(i8* %13, i8* %0)
br label %24
24: ; preds = %19, %22, %10, %2
%25 = phi i8* [ null, %2 ], [ null, %10 ], [ %13, %22 ], [ %13, %19 ]
ret i8* %25
}