#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__ mmalloc(int);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *mstrndup(const char *s, size_t size) {
  const size_t len = strlen(s);
  int newlen = ((len) < (size) ? (len) : (size));
  char *new_s = (char *)mmalloc(newlen + 1);
  if (new_s) {
    strncpy(new_s, s, newlen);
    new_s[newlen] = 0;
    return new_s;
  } else
    return NULL;
}


x86 assembly (clang, -O0):
<mstrndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1168 <mstrndup+0x38>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1170 <mstrndup+0x40>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1050 <mmalloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11ba <mstrndup+0x8a>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c2 <mstrndup+0x92>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mstrndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
cmp    %rbx,%rax
cmovb  %rax,%rbx
lea    0x1(%rbx),%edi
call   1050 <mmalloc@plt>
test   %rax,%rax
je     116e <mstrndup+0x3e>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
jmp    1171 <mstrndup+0x41>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @mstrndup(i8* %0, i64 %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp ult i64 %3, %1
%5 = select i1 %4, i64 %3, i64 %1
%6 = trunc i64 %5 to i32
%7 = add nsw i32 %6, 1
%8 = tail call i64 @mmalloc(i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = tail call i32 @strncpy(i8* %9, i8* %0, i32 %6)
%13 = shl i64 %5, 32
%14 = ashr exact i64 %13, 32
%15 = getelementptr inbounds i8, i8* %9, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %2, %11
%17 = phi i8* [ %9, %11 ], [ null, %2 ]
ret i8* %17
}