#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 *calloc(size_t, int);
int /*<<< orphan*/ puts(char *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *ic_strdup(char *ch) {
  char *buffer = 0;
  size_t len = 0;
  if (!ch) {
    puts("ic_strdup: ch was null");
    return 0;
  }
  len = strlen(ch);
  buffer = calloc(len + 1, sizeof(char));
  if (!buffer) {
    puts("ic_strdup: call to calloc failed");
    return 0;
  }
  strncpy(buffer, ch, len);
  buffer[len] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<ic_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1180 <ic_strdup+0x40>
lea    0xe92(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ec <ic_strdup+0xac>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c7 <ic_strdup+0x87>
lea    0xe62(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ec <ic_strdup+0xac>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ic_strdup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1180 <ic_strdup+0x40>
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     1189 <ic_strdup+0x49>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    1197 <ic_strdup+0x57>
lea    0xe79(%rip),%rdi
jmp    1190 <ic_strdup+0x50>
lea    0xe87(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [23 x i8] c"ic_strdup: ch was null\00"
@.str.1 = [33 x i8] c"ic_strdup: call to calloc failed\00"
define i8* @ic_strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
store i64 0, i64* %5
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %29
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = add i64 %13, 1
%15 = call i8* @calloc(i64 %14, i32 1)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %10
%19 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %29
20: ; preds = %10
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = load i64, i64* %5
%24 = call i32 @strncpy(i8* %21, i8* %22, i64 %23)
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %4
store i8* %28, i8** %2
br label %29
29: ; preds = %20, %18, %8
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
@.str = [23 x i8] c"ic_strdup: ch was null\00"
@.str.1 = [33 x i8] c"ic_strdup: call to calloc failed\00"
define i8* @ic_strdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %15
5: ; preds = %1
%6 = tail call i64 @strlen(i8* %0)
%7 = add i64 %6, 1
%8 = tail call i8* @calloc(i64 %7, i32 1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %15
12: ; preds = %5
%13 = tail call i32 @strncpy(i8* %8, i8* %0, i64 %6)
%14 = getelementptr inbounds i8, i8* %8, i64 %6
store i8 0, i8* %14
br label %15
15: ; preds = %12, %10, %3
%16 = phi i8* [ %8, %12 ], [ null, %10 ], [ null, %3 ]
ret i8* %16
}