#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 */
struct mpool {
  int dummy;
};

/* Variables and functions */
void *mpool_malloc(struct mpool *, size_t);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
void *mpool_strndup(struct mpool *pool, const char *str, size_t n) {
  void *ret;
  if (!str)
    return NULL;
  if ((ret = mpool_malloc(pool, n + 1))) {
    strncpy((char *)ret, str, n);
    ((char *)ret)[n] = '\0';
  }
  return ret;
}


x86 assembly (clang, -O0):
<mpool_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    114c <mpool_strndup+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    1190 <mpool_strndup+0x70>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1040 <mpool_malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1188 <mpool_strndup+0x68>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpool_strndup>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1156 <mpool_strndup+0x36>
mov    %rdx,%r14
mov    %rsi,%r15
lea    0x1(%rdx),%rsi
call   1040 <mpool_malloc@plt>
test   %rax,%rax
je     1156 <mpool_strndup+0x36>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r14,1)
jmp    1158 <mpool_strndup+0x38>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.mpool = type { i32 }
define i8* @mpool_strndup(%struct.mpool* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.mpool*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store %struct.mpool* %0, %struct.mpool** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %3
store i8* null, i8** %4
br label %28
12: ; preds = %3
%13 = load %struct.mpool*, %struct.mpool** %5
%14 = load i64, i64* %7
%15 = add i64 %14, 1
%16 = call i8* @mpool_malloc(%struct.mpool* %13, i64 %15)
store i8* %16, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = load i8*, i8** %8
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = call i32 @strncpy(i8* %19, i8* %20, i64 %21)
%23 = load i8*, i8** %8
%24 = load i64, i64* %7
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %18, %12
%27 = load i8*, i8** %8
store i8* %27, i8** %4
br label %28
28: ; preds = %26, %11
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
%struct.mpool = type { i32 }
define i8* @mpool_strndup(%struct.mpool* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = add i64 %2, 1
%7 = tail call i8* @mpool_malloc(%struct.mpool* %0, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = tail call i32 @strncpy(i8* %7, i8* %1, i64 %2)
%11 = getelementptr inbounds i8, i8* %7, i64 %2
store i8 0, i8* %11
br label %12
12: ; preds = %5, %9, %3
%13 = phi i8* [ null, %3 ], [ %7, %9 ], [ null, %5 ]
ret i8* %13
}