#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__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *mystrndup(const char *str, size_t n) {
  char *dst;
  if (!str)
    return NULL;
  if (n >= 0xffffffff)
    n = strlen(str);
  if (n == 0)
    return NULL;
  if ((dst = (char *)malloc(n + 1)) == NULL)
    return NULL;
  memcpy(dst, str, n);
  dst[n] = 0;
  return dst;
}


x86 assembly (clang, -O0):
<mystrndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <mystrndup+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <mystrndup+0xa9>
mov    $0xffffffff,%eax
cmp    %rax,-0x18(%rbp)
jb     1174 <mystrndup+0x44>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118c <mystrndup+0x5c>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <mystrndup+0xa9>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b4 <mystrndup+0x84>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <mystrndup+0xa9>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystrndup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1180 <mystrndup+0x50>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0xffffffff,%eax
cmp    %rax,%rsi
jb     1155 <mystrndup+0x25>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     1180 <mystrndup+0x50>
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1180 <mystrndup+0x50>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    1183 <mystrndup+0x53>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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