#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 */
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *xmalloc(int);
char *xstrdup(const char *str) {
  int len = 0;
  char *buf = NULL;
  if (str == NULL)
    return (NULL);
  len = strlen(str) + 1;
  buf = xmalloc(len);
  if (buf == NULL)
    return (NULL);
  (void)strncpy(buf, str, len);
  return (buf);
}


x86 assembly (clang, -O0):
<xstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <xstrdup+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11ae <xstrdup+0x7e>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1196 <xstrdup+0x66>
movq   $0x0,-0x8(%rbp)
jmp    11ae <xstrdup+0x7e>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrdup>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1164 <xstrdup+0x34>
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1050 <xmalloc@plt>
test   %rax,%rax
je     1164 <xstrdup+0x34>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
jmp    1166 <xstrdup+0x36>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @xstrdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %10, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i8* @xmalloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @strncpy(i8* %6, i8* %0, i32 %5)
br label %10
10: ; preds = %3, %1, %8
%11 = phi i8* [ %6, %8 ], [ null, %1 ], [ null, %3 ]
ret i8* %11
}