#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(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *xstrdup(const char *s, int len) {
  if (len >= 0) {
    char *res = malloc(len + 1);
    if (res != NULL) {
      if (len > 0)
        memcpy(res, s, len);
      res[len] = 0;
    }
    return res;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<xstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1186 <xstrdup+0x66>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1179 <xstrdup+0x59>
cmpl   $0x0,-0x14(%rbp)
jle    116d <xstrdup+0x4d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    118e <xstrdup+0x6e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrdup>:
push   %r15
push   %r14
push   %rbx
test   %esi,%esi
js     1158 <xstrdup+0x38>
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x1(%rbx),%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1158 <xstrdup+0x38>
mov    %rax,%r14
test   %ebx,%ebx
je     114f <xstrdup+0x2f>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
jmp    115b <xstrdup+0x3b>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @xstrdup(i8* %0, i32 %1) {
%3 = icmp sgt i32 %1, -1
br i1 %3, label %4, label %15
4: ; preds = %2
%5 = add nuw nsw i32 %1, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %15, label %8
8: ; preds = %4
%9 = icmp eq i32 %1, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %1)
br label %12
12: ; preds = %10, %8
%13 = zext i32 %1 to i64
%14 = getelementptr inbounds i8, i8* %6, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %2, %4, %12
%16 = phi i8* [ %6, %12 ], [ null, %4 ], [ null, %2 ]
ret i8* %16
}