#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 || (length < 0))
    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)
je     1164 <string_strndup+0x24>
cmpl   $0x0,-0x14(%rbp)
jge    1171 <string_strndup+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <string_strndup+0xa0>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    -0x14(%rbp),%eax
jge    1195 <string_strndup+0x55>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11e0 <string_strndup+0xa0>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bc <string_strndup+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <string_strndup+0xa0>
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
xor    %r15d,%r15d
test   %rdi,%rdi
je     119a <string_strndup+0x5a>
mov    %esi,%r14d
test   %esi,%esi
js     119a <string_strndup+0x5a>
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    %r14d,%eax
jge    116e <string_strndup+0x2e>
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     1197 <string_strndup+0x57>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
mov    %r14d,%eax
movb   $0x0,(%r15,%rax,1)
jmp    119a <string_strndup+0x5a>
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 %9, label %12
9: ; preds = %2
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i8* null, i8** %3
br label %39
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = trunc i64 %15 to i32
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %3
br label %39
22: ; preds = %13
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %22
store i8* null, i8** %3
br label %39
29: ; preds = %22
%30 = load i8*, i8** %6
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = call i32 @memcpy(i8* %30, i8* %31, i32 %32)
%34 = load i8*, i8** %6
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %6
store i8* %38, i8** %3
br label %39
39: ; preds = %29, %28, %19, %12
%40 = load i8*, i8** %3
ret i8* %40
}

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