#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*/ assert(int /*<<< orphan*/);
char *malloc(size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *tx_strndup(const char *s1, size_t n) {
  assert(s1 != NULL);
  size_t len = (((strlen(s1)) < (n)) ? (strlen(s1)) : (n));
  char *s2 = malloc(len + 1);
  if (s2 == NULL)
    return NULL;
  strncpy(s2, s1, len);
  s2[len] = '\0';
  return s2;
}


x86 assembly (clang, -O0):
<tx_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x18(%rbp),%rax
jae    1187 <tx_strndup+0x47>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
jmp    118f <tx_strndup+0x4f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c0 <tx_strndup+0x80>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <tx_strndup+0xa5>
mov    -0x28(%rbp),%rdi
mov    -0x10(%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):
<tx_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %r15,%rax
cmovb  %rax,%r15
lea    0x1(%r15),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     118c <tx_strndup+0x4c>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @tx_strndup(i8* %0, i64 %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = icmp ult i64 %6, %1
%8 = select i1 %7, i64 %6, i64 %1
%9 = add i64 %8, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = tail call i32 @strncpy(i8* %10, i8* %0, i64 %8)
%14 = getelementptr inbounds i8, i8* %10, i64 %8
store i8 0, i8* %14
br label %15
15: ; preds = %2, %12
ret i8* %10
}