#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(unsigned long);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *mkstring(const char *input) {
  char *str;
  unsigned long len;
  if (NULL == input) {
    len = 4;
  } else {
    if (4 > (len = strlen(input) + 1))
      len = 4;
  }
  if (NULL == (str = malloc(len)))
    return (NULL);
  if (NULL == input)
    str[0] = 0;
  else
    strcpy(str, input);
  return (str);
}


x86 assembly (clang, -O0):
<mkstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1155 <mkstring+0x25>
movq   $0x4,-0x20(%rbp)
jmp    1183 <mkstring+0x53>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rcx
mov    %rcx,-0x20(%rbp)
mov    $0x4,%eax
cmp    %rcx,%rax
jbe    117e <mkstring+0x4e>
movq   $0x4,-0x20(%rbp)
jmp    1183 <mkstring+0x53>
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11ab <mkstring+0x7b>
movq   $0x0,-0x8(%rbp)
jmp    11d8 <mkstring+0xa8>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11c3 <mkstring+0x93>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11d0 <mkstring+0xa0>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkstring>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     116d <mkstring+0x3d>
mov    %rdi,%r14
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    $0x5,%rax
mov    $0x4,%edi
cmovae %rax,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1184 <mkstring+0x54>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    1186 <mkstring+0x56>
mov    $0x4,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1184 <mkstring+0x54>
mov    %rax,%rbx
movb   $0x0,(%rax)
jmp    1186 <mkstring+0x56>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @mkstring(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* null, %6
br i1 %7, label %8, label %9
8: ; preds = %1
store i64 4, i64* %5
br label %17
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
store i64 %13, i64* %5
%14 = icmp ugt i64 4, %13
br i1 %14, label %15, label %16
15: ; preds = %9
store i64 4, i64* %5
br label %16
16: ; preds = %15, %9
br label %17
17: ; preds = %16, %8
%18 = load i64, i64* %5
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %4
%20 = icmp eq i8* null, %19
br i1 %20, label %21, label %22
21: ; preds = %17
store i8* null, i8** %2
br label %34
22: ; preds = %17
%23 = load i8*, i8** %3
%24 = icmp eq i8* null, %23
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 0
store i8 0, i8* %27
br label %32
28: ; preds = %22
%29 = load i8*, i8** %4
%30 = load i8*, i8** %3
%31 = call i32 @strcpy(i8* %29, i8* %30)
br label %32
32: ; preds = %28, %25
%33 = load i8*, i8** %4
store i8* %33, i8** %2
br label %34
34: ; preds = %32, %21
%35 = load i8*, i8** %2
ret i8* %35
}

LLVM IR (-O2):
define i8* @mkstring(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call dereferenceable_or_null(4) i8* @malloc(i64 4)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %14
6: ; preds = %1
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = icmp ugt i64 %9, 4
%11 = select i1 %10, i64 %9, i64 4
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %15
14: ; preds = %3
store i8 0, i8* %4
br label %17
15: ; preds = %6
%16 = tail call i32 @strcpy(i8* %12, i8* %0)
br label %17
17: ; preds = %6, %14, %15, %3
%18 = phi i8* [ null, %3 ], [ %12, %15 ], [ %4, %14 ], [ null, %6 ]
ret i8* %18
}