#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(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *xstrndup(const char *s, size_t n) {
  size_t l = 0;
  char *p;
  while (s[l] != '\0' && l < n) {
    l++;
  }
  p = malloc(l + 1);
  memcpy(p, s, l);
  p[l] = '\0';
  return p;
}


x86 assembly (clang, -O0):
<xstrndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     1160 <xstrndup+0x40>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1170 <xstrndup+0x50>
jmp    1181 <xstrndup+0x61>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1138 <xstrndup+0x18>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrndup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
lea    0x1(%rax),%rbx
cmpb   $0x0,(%r14,%rax,1)
je     1143 <xstrndup+0x23>
cmp    %rsi,%rax
mov    %rbx,%rax
jb     1130 <xstrndup+0x10>
mov    %rbx,%rdi
call   1040 <malloc@plt>
mov    %rax,%r15
lea    -0x1(%rbx),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <memcpy@plt>
movb   $0x0,-0x1(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @xstrndup(i8* %0, i64 %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i64 [ 0, %2 ], [ %10, %3 ]
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = icmp ne i8 %6, 0
%8 = icmp ult i64 %4, %1
%9 = select i1 %7, i1 %8, i1 false
%10 = add i64 %4, 1
br i1 %9, label %3, label %11
11: ; preds = %3
%12 = tail call i8* @malloc(i64 %10)
%13 = tail call i32 @memcpy(i8* %12, i8* %0, i64 %4)
%14 = getelementptr inbounds i8, i8* %12, i64 %4
store i8 0, i8* %14
ret i8* %12
}