#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__ __talloc(void const *, size_t);
int /*<<< orphan*/ _talloc_set_name_const(char *, char *);
int /*<<< orphan*/ lock();
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ unlock();
char *talloc_strndup(const void *t, const char *p, size_t n) {
  size_t len;
  char *ret;
  for (len = 0; len < n && p[len]; len++)
    ;
  lock();
  ret = (char *)__talloc(t, len + 1);
  unlock();
  if (!ret) {
    return NULL;
  }
  memcpy(ret, p, len);
  ret[len] = 0;
  _talloc_set_name_const(ret, ret);
  return ret;
}


x86 assembly (clang, -O0):
<talloc_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x20(%rbp),%rcx
mov    %al,-0x31(%rbp)
jae    1194 <talloc_strndup+0x44>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11a4 <talloc_strndup+0x54>
jmp    11ba <talloc_strndup+0x6a>
jmp    11a9 <talloc_strndup+0x59>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116c <talloc_strndup+0x1c>
mov    $0x0,%al
call   1040 <lock@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1050 <__talloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <unlock@plt>
cmpq   $0x0,-0x30(%rbp)
jne    11f5 <talloc_strndup+0xa5>
movq   $0x0,-0x8(%rbp)
jmp    1227 <talloc_strndup+0xd7>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <_talloc_set_name_const@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<talloc_strndup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
test   %rdx,%rdx
je     1183 <talloc_strndup+0x33>
cs nopw 0x0(%rax,%rax,1)
nop
cmpb   $0x0,(%r14,%rbx,1)
je     1183 <talloc_strndup+0x33>
add    $0x1,%rbx
cmp    %rbx,%rdx
jne    1170 <talloc_strndup+0x20>
mov    %rdx,%rbx
xor    %r12d,%r12d
xor    %eax,%eax
call   1040 <lock@plt>
lea    0x1(%rbx),%rsi
mov    %r15,%rdi
call   1050 <__talloc@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <unlock@plt>
test   %r15,%r15
je     11c9 <talloc_strndup+0x79>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rdi
mov    %r15,%rsi
call   1070 <_talloc_set_name_const@plt>
mov    %r15,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @talloc_strndup(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %24, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = load i8*, i8** %6
%16 = load i64, i64* %8
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br label %21
21: ; preds = %14, %10
%22 = phi i1 [ false, %10 ], [ %20, %14 ]
br i1 %22, label %23, label %27
23: ; preds = %21
br label %24
24: ; preds = %23
%25 = load i64, i64* %8
%26 = add i64 %25, 1
store i64 %26, i64* %8
br label %10
27: ; preds = %21
%28 = call i32 (...) @lock()
%29 = load i8*, i8** %5
%30 = load i64, i64* %8
%31 = add i64 %30, 1
%32 = call i64 @__talloc(i8* %29, i64 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %9
%34 = call i32 (...) @unlock()
%35 = load i8*, i8** %9
%36 = icmp ne i8* %35, null
br i1 %36, label %38, label %37
37: ; preds = %27
store i8* null, i8** %4
br label %50
38: ; preds = %27
%39 = load i8*, i8** %9
%40 = load i8*, i8** %6
%41 = load i64, i64* %8
%42 = call i32 @memcpy(i8* %39, i8* %40, i64 %41)
%43 = load i8*, i8** %9
%44 = load i64, i64* %8
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %9
%47 = load i8*, i8** %9
%48 = call i32 @_talloc_set_name_const(i8* %46, i8* %47)
%49 = load i8*, i8** %9
store i8* %49, i8** %4
br label %50
50: ; preds = %38, %37
%51 = load i8*, i8** %4
ret i8* %51
}

LLVM IR (-O2):
define i8* @talloc_strndup(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %13, label %5
5: ; preds = %3, %10
%6 = phi i64 [ %11, %10 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = add nuw i64 %6, 1
%12 = icmp eq i64 %11, %2
br i1 %12, label %13, label %5
13: ; preds = %5, %10, %3
%14 = phi i64 [ 0, %3 ], [ %2, %10 ], [ %6, %5 ]
%15 = tail call i32 (...) @lock()
%16 = add i64 %14, 1
%17 = tail call i64 @__talloc(i8* %0, i64 %16)
%18 = inttoptr i64 %17 to i8*
%19 = tail call i32 (...) @unlock()
%20 = icmp eq i64 %17, 0
br i1 %20, label %25, label %21
21: ; preds = %13
%22 = tail call i32 @memcpy(i8* %18, i8* %1, i64 %14)
%23 = getelementptr inbounds i8, i8* %18, i64 %14
store i8 0, i8* %23
%24 = tail call i32 @_talloc_set_name_const(i8* %18, i8* %18)
br label %25
25: ; preds = %13, %21
%26 = phi i8* [ %18, %21 ], [ null, %13 ]
ret i8* %26
}