#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);
size_t strlen(char const *);
extern char *strndup(const char *s, size_t n) {
  char *r;
  size_t len;
  if ((len = strlen(s)) <= n) {
    if (!(r = malloc(len + 1)))
      return NULL;
    memcpy(r, s, len);
    r[len] = '\0';
    return r;
  } else if ((r = malloc(n + 1)) != NULL) {
    memcpy(r, s, n);
    r[n] = '\0';
  }
  return r;
}


x86 assembly (clang, -O0):
<strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmp    -0x18(%rbp),%rax
ja     11a9 <strndup+0x79>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    117f <strndup+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    11ee <strndup+0xbe>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ee <strndup+0xbe>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11e1 <strndup+0xb1>
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)
jmp    11e6 <strndup+0xb6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    %r15,%rax
jbe    116f <strndup+0x3f>
lea    0x1(%r15),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     119c <strndup+0x6c>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %r12,%rax
add    %r15,%rax
jmp    1197 <strndup+0x67>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     119c <strndup+0x6c>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r12,%rax
add    %rbx,%rax
movb   $0x0,(%rax)
jmp    119f <strndup+0x6f>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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