#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 (!string)
    return NULL;
  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)
cmpq   $0x0,-0x10(%rbp)
jne    1167 <string_strndup+0x27>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <string_strndup+0x96>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    -0x14(%rbp),%eax
jge    118b <string_strndup+0x4b>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11d6 <string_strndup+0x96>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b2 <string_strndup+0x72>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <string_strndup+0x96>
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
test   %rdi,%rdi
je     1190 <string_strndup+0x50>
mov    %esi,%r14d
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    %r14d,%eax
jge    1167 <string_strndup+0x27>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strdup@plt>
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1190 <string_strndup+0x50>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
jmp    1193 <string_strndup+0x53>
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 = 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 i8*, i8** %4
%12 = call i64 @strlen(i8* %11)
%13 = trunc i64 %12 to i32
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i8*, i8** %4
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %3
br label %36
19: ; preds = %10
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %19
store i8* null, i8** %3
br label %36
26: ; preds = %19
%27 = load i8*, i8** %6
%28 = load i8*, i8** %4
%29 = load i32, i32* %5
%30 = call i32 @memcpy(i8* %27, i8* %28, i32 %29)
%31 = load i8*, i8** %6
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %6
store i8* %35, i8** %3
br label %36
36: ; preds = %26, %25, %16, %9
%37 = load i8*, i8** %3
ret i8* %37
}

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