#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 *, size_t);
int /*<<< orphan*/ rtrim(char *);
scalar_t__ xmalloc(size_t);
char *xstrndupt(const char *str, size_t sz) {
  size_t newlen = 0;
  char *cin = (char *)str;
  char *newstr;
  while ((newlen < sz) && (*cin != '\0')) {
    newlen++;
    cin++;
  }
  newstr = (char *)xmalloc(newlen + 1);
  memcpy(newstr, str, newlen);
  newstr[newlen] = 0;
  rtrim(newstr);
  return newstr;
}


x86 assembly (clang, -O0):
<xstrndupt>:
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    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x29(%rbp)
jae    1173 <xstrndupt+0x43>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1183 <xstrndupt+0x53>
jmp    11a0 <xstrndupt+0x70>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1150 <xstrndupt+0x20>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <rtrim@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrndupt>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
test   %rsi,%rsi
je     1153 <xstrndupt+0x23>
nop
cmpb   $0x0,(%r14,%rbx,1)
je     1153 <xstrndupt+0x23>
add    $0x1,%rbx
cmp    %rbx,%rsi
jne    1140 <xstrndupt+0x10>
mov    %rsi,%rbx
lea    0x1(%rbx),%rdi
call   1030 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rdi
call   1040 <rtrim@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @xstrndupt(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %13, label %4
4: ; preds = %2, %9
%5 = phi i64 [ %10, %9 ], [ 0, %2 ]
%6 = phi i8* [ %11, %9 ], [ %0, %2 ]
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = add nuw i64 %5, 1
%11 = getelementptr inbounds i8, i8* %6, i64 1
%12 = icmp eq i64 %10, %1
br i1 %12, label %13, label %4
13: ; preds = %4, %9, %2
%14 = phi i64 [ 0, %2 ], [ %1, %9 ], [ %5, %4 ]
%15 = add i64 %14, 1
%16 = tail call i64 @xmalloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @memcpy(i8* %17, i8* %0, i64 %14)
%19 = getelementptr inbounds i8, i8* %17, i64 %14
store i8 0, i8* %19
%20 = tail call i32 @rtrim(i8* %17)
ret i8* %17
}