#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 /*<<< orphan*/ memcpy(char *, char const *, int);
char *sqlite3MallocRaw(int, int);
char *sqlite3StrNDup(const char *z, int n) {
  char *zNew;
  if (z == 0)
    return 0;
  zNew = sqlite3MallocRaw(n + 1, 1);
  if (zNew) {
    memcpy(zNew, z, n);
    zNew[n] = 0;
  }
  return zNew;
}


x86 assembly (clang, -O0):
<sqlite3StrNDup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1147 <sqlite3StrNDup+0x27>
movq   $0x0,-0x8(%rbp)
jmp    118a <sqlite3StrNDup+0x6a>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1030 <sqlite3MallocRaw@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1182 <sqlite3StrNDup+0x62>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqlite3StrNDup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     115d <sqlite3StrNDup+0x3d>
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x1(%r14),%edi
mov    $0x1,%esi
call   1030 <sqlite3MallocRaw@plt>
test   %rax,%rax
je     115d <sqlite3StrNDup+0x3d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    115f <sqlite3StrNDup+0x3f>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @sqlite3StrNDup(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 i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
store i8* null, i8** %3
br label %27
10: ; preds = %2
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
%13 = call i8* @sqlite3MallocRaw(i32 %12, i32 1)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %25
16: ; preds = %10
%17 = load i8*, i8** %6
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = call i32 @memcpy(i8* %17, i8* %18, i32 %19)
%21 = load i8*, i8** %6
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %16, %10
%26 = load i8*, i8** %6
store i8* %26, i8** %3
br label %27
27: ; preds = %25, %9
%28 = load i8*, i8** %3
ret i8* %28
}

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