#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);
int strlen(char const *);
char *zexy_strndup(const char *s, size_t n) {
  char *result = 0;
  size_t len = strlen(s) + 1;
  if (len > n) {
    len = n + 1;
  }
  result = malloc(len);
  if (!result) {
    return result;
  }
  memcpy(result, s, len);
  result[len - 1] = 0;
  return result;
}


x86 assembly (clang, -O0):
<zexy_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1174 <zexy_strndup+0x44>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1199 <zexy_strndup+0x69>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c2 <zexy_strndup+0x92>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zexy_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
lea    0x1(%r15),%rbx
cmp    %r15,%rax
cmovbe %rax,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1174 <zexy_strndup+0x44>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,-0x1(%rbx,%r15,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @zexy_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
store i8* null, i8** %6
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = load i64, i64* %5
%14 = icmp ugt i64 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i64, i64* %5
%17 = add i64 %16, 1
store i64 %17, i64* %7
br label %18
18: ; preds = %15, %2
%19 = load i64, i64* %7
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = load i8*, i8** %6
store i8* %24, i8** %3
br label %35
25: ; preds = %18
%26 = load i8*, i8** %6
%27 = load i8*, i8** %4
%28 = load i64, i64* %7
%29 = call i32 @memcpy(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %6
%31 = load i64, i64* %7
%32 = sub i64 %31, 1
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %6
store i8* %34, i8** %3
br label %35
35: ; preds = %25, %23
%36 = load i8*, i8** %3
ret i8* %36
}

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