#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(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strdup(char const *);
scalar_t__ strlen(char const *);
char *string_strndup(const char *string, int length) {
  char *result;
  if ((int)strlen(string) < length)
    return strdup(string);
  result = malloc(length + 1);
  if (!result)
    return NULL;
  memcpy(result, string, length);
  result[length] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<string_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    -0x14(%rbp),%eax
jge    1173 <string_strndup+0x33>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11be <string_strndup+0x7e>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119a <string_strndup+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    11be <string_strndup+0x7e>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%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):
<string_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    %ebx,%eax
jge    1160 <string_strndup+0x20>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strdup@plt>
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1187 <string_strndup+0x47>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
jmp    118a <string_strndup+0x4a>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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