#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 */
scalar_t__ malloc(size_t);
char *memcpy(char *, char const *, size_t);
int strlen(char const *);
char *etui_strdup(const char *s) {
  char *res;
  size_t len;
  if (!s)
    return NULL;
  len = strlen(s) + 1;
  res = (char *)malloc(len * sizeof(char));
  return res ? memcpy(res, s, len) : NULL;
}


x86 assembly (clang, -O0):
<etui_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <etui_strdup+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11af <etui_strdup+0x7f>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119c <etui_strdup+0x6c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    %rax,-0x28(%rbp)
jmp    11a7 <etui_strdup+0x77>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    11a7 <etui_strdup+0x77>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<etui_strdup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1168 <etui_strdup+0x38>
mov    %rdi,%r14
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1168 <etui_strdup+0x38>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
jmp    116a <etui_strdup+0x3a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @etui_strdup(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 ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i8* null, i8** %2
br label %28
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 = load i64, i64* %5
%15 = mul i64 %14, 1
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %9
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = load i64, i64* %5
%24 = call i8* @memcpy(i8* %21, i8* %22, i64 %23)
br label %26
25: ; preds = %9
br label %26
26: ; preds = %25, %20
%27 = phi i8* [ %24, %20 ], [ null, %25 ]
store i8* %27, i8** %2
br label %28
28: ; preds = %26, %8
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
define i8* @etui_strdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %11, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i64 @malloc(i64 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = inttoptr i64 %7 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %10, i8* align 1 %0, i64 %6, i1 false)
br label %11
11: ; preds = %9, %3, %1
%12 = phi i8* [ null, %1 ], [ %10, %9 ], [ null, %3 ]
ret i8* %12
}