#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 strlen(char *);
char *strndup(char *, int);
void strndup_2_Good() {
  char s1[] = "Hello Infer!";
  int len = strlen(s1);
  s1[5] = '\0';
  char *s2 = strndup(s1, len);
  for (int i = 0; i < strlen(s2); i++) {
    s2[i] = 'a';
  }
}


x86 assembly (clang, -O0):
<strndup_2_Good>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0xed1(%rip),%rax
mov    %rax,-0xd(%rbp)
mov    0xecf(%rip),%eax
mov    %eax,-0x5(%rbp)
mov    0xeca(%rip),%al
mov    %al,-0x1(%rbp)
lea    -0xd(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movb   $0x0,-0x8(%rbp)
lea    -0xd(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <strndup@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    11a2 <strndup_2_Good+0x82>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116c <strndup_2_Good+0x4c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndup_2_Good>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movabs $0x217265666e4920,%rax
mov    %rax,0xd(%rsp)
movabs $0x6e49206f6c6c6548,%rax
20 49 6e
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
movb   $0x0,0xd(%rsp)
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <strndup@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1198 <strndup_2_Good+0x78>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x61,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1180 <strndup_2_Good+0x60>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.strndup_2_Good.s1 = [13 x i8] c"Hello Infer!\00"
define void @strndup_2_Good() {
%1 = alloca [13 x i8]
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = bitcast [13 x i8]* %1 to i8*
%6 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %2
%8 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 5
store i8 0, i8* %8
%9 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i8* @strndup(i8* %9, i32 %10)
store i8* %11, i8** %3
store i32 0, i32* %4
br label %12
12: ; preds = %22, %0
%13 = load i32, i32* %4
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = icmp slt i32 %13, %15
br i1 %16, label %17, label %25
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 97, i8* %21
br label %22
22: ; preds = %17
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %12
25: ; preds = %12
ret void
}

LLVM IR (-O2):
@__const.strndup_2_Good.s1 = [13 x i8] c"Hello Infer!\00"
define void @strndup_2_Good() {
%1 = alloca [13 x i8]
%2 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%3 = call i32 @strlen(i8* %2)
%4 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 5
store i8 0, i8* %4
%5 = call i8* @strndup(i8* %2, i32 %3)
%6 = call i32 @strlen(i8* %5)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %9, label %8
8: ; preds = %9, %0
ret void
9: ; preds = %0, %9
%10 = phi i64 [ %12, %9 ], [ 0, %0 ]
%11 = getelementptr inbounds i8, i8* %5, i64 %10
store i8 97, i8* %11
%12 = add nuw nsw i64 %10, 1
%13 = call i32 @strlen(i8* %5)
%14 = sext i32 %13 to i64
%15 = icmp slt i64 %12, %14
br i1 %15, label %9, label %8
}