#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 */
int /*<<< orphan*/ Panic(char *);
int /*<<< orphan*/ free(char *);
int strlen(char *);
char *strndup(char *, int);
void Check_strndup(void) {
  char *ptr;
  ptr = strndup("1234567890", 5);
  if (!ptr)
    Panic("strndup");
  if (ptr[5] != '\0')
    Panic("strndup NULL byte");
  if (strlen(ptr) != 5)
    Panic("strndup string length");
  free(ptr);
}


x86 assembly (clang, -O0):
<Check_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x5,%esi
call   1050 <strndup@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1174 <Check_strndup+0x34>
lea    0xe9c(%rip),%rdi
call   1060 <Panic@plt>
mov    -0x8(%rbp),%rax
movsbl 0x5(%rax),%eax
cmp    $0x0,%eax
je     1191 <Check_strndup+0x51>
lea    0xe87(%rip),%rdi
call   1060 <Panic@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x5,%eax
je     11af <Check_strndup+0x6f>
lea    0xe7b(%rip),%rdi
call   1060 <Panic@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Check_strndup>:
push   %rbx
lea    0xeb8(%rip),%rdi
mov    $0x5,%esi
call   1050 <strndup@plt>
mov    %rax,%rbx
test   %rax,%rax
je     118e <Check_strndup+0x4e>
cmpb   $0x0,0x5(%rbx)
je     116c <Check_strndup+0x2c>
lea    0xeac(%rip),%rdi
call   1060 <Panic@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x5,%eax
je     1185 <Check_strndup+0x45>
lea    0xea5(%rip),%rdi
call   1060 <Panic@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <free@plt>
lea    0xe76(%rip),%rdi
call   1060 <Panic@plt>
cmpb   $0x0,0x5(%rbx)
jne    1160 <Check_strndup+0x20>
jmp    116c <Check_strndup+0x2c>

LLVM IR (-O0):
@.str = [11 x i8] c"1234567890\00"
@.str.1 = [8 x i8] c"strndup\00"
@.str.2 = [18 x i8] c"strndup NULL byte\00"
@.str.3 = [22 x i8] c"strndup string length\00"
define void @Check_strndup() {
%1 = alloca i8*
%2 = call i8* @strndup(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 5)
store i8* %2, i8** %1
%3 = load i8*, i8** %1
%4 = icmp ne i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i32 @Panic(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = load i8*, i8** %1
%9 = getelementptr inbounds i8, i8* %8, i64 5
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 @Panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %7
%16 = load i8*, i8** %1
%17 = call i32 @strlen(i8* %16)
%18 = icmp ne i32 %17, 5
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @Panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = load i8*, i8** %1
%23 = call i32 @free(i8* %22)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"1234567890\00"
@.str.1 = [8 x i8] c"strndup\00"
@.str.2 = [18 x i8] c"strndup NULL byte\00"
@.str.3 = [22 x i8] c"strndup string length\00"
define void @Check_strndup() {
%1 = tail call dereferenceable_or_null(6) i8* @strndup(i8* dereferenceable(11) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 5)
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @Panic(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = getelementptr inbounds i8, i8* %1, i64 5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @Panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = tail call i32 @strlen(i8* %1)
%13 = icmp eq i32 %12, 5
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @Panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = tail call i32 @free(i8* %1)
ret void
}