#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 *base_calloc(int, int);
char *base_malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *base_strdup(const char *s) {
  char *t;
  if (s == NULL || *s == '\0') {
    t = base_calloc(1, 1);
    *t = '\0';
  } else {
    size_t len = strlen(s);
    t = base_malloc(len + 1);
    memcpy(t, s, len + 1);
  }
  return t;
}


x86 assembly (clang, -O0):
<base_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1167 <base_strdup+0x27>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1183 <base_strdup+0x43>
mov    $0x1,%esi
mov    %esi,%edi
call   1030 <base_calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b6 <base_strdup+0x76>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1060 <base_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base_strdup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1179 <base_strdup+0x39>
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
je     1179 <base_strdup+0x39>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%r15
mov    %r15,%rdi
call   1060 <base_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
jmp    118e <base_strdup+0x4e>
mov    $0x1,%edi
mov    $0x1,%esi
call   1030 <base_calloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @base_strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7, %1
%13 = call i8* @base_calloc(i32 1, i32 1)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
store i8 0, i8* %14
br label %26
15: ; preds = %7
%16 = load i8*, i8** %2
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = add i64 %18, 1
%20 = call i8* @base_malloc(i64 %19)
store i8* %20, i8** %3
%21 = load i8*, i8** %3
%22 = load i8*, i8** %2
%23 = load i64, i64* %4
%24 = add i64 %23, 1
%25 = call i32 @memcpy(i8* %21, i8* %22, i64 %24)
br label %26
26: ; preds = %15, %12
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
define i8* @base_strdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3, %1
%7 = tail call i8* @base_calloc(i32 1, i32 1)
store i8 0, i8* %7
br label %13
8: ; preds = %3
%9 = tail call i64 @strlen(i8* %0)
%10 = add i64 %9, 1
%11 = tail call i8* @base_malloc(i64 %10)
%12 = tail call i32 @memcpy(i8* %11, i8* %0, i64 %10)
br label %13
13: ; preds = %8, %6
%14 = phi i8* [ %7, %6 ], [ %11, %8 ]
ret i8* %14
}