#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *strdup(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *my_strndup(const char *s, int n) {
  if (n > 0) {
    char *rs = (char *)malloc(n + 4);
    memset(rs, 0, n + 4);
    strncpy(rs, s, n);
    rs[n] = 0;
    return rs;
  } else
    return strdup("");
}


x86 assembly (clang, -O0):
<my_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11a2 <my_strndup+0x62>
mov    -0x14(%rbp),%edi
add    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    $0x4,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b2 <my_strndup+0x72>
lea    0xe57(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strndup>:
test   %esi,%esi
jle    118e <my_strndup+0x4e>
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x4(%rbx),%r15d
mov    %r15d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %ebx,%eax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe6b(%rip),%rdi
jmp    1060 <strdup@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @my_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 i32, i32* %5
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %2
%10 = load i32, i32* %5
%11 = add nsw i32 %10, 4
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 4
%17 = call i32 @memset(i8* %14, i32 0, i32 %16)
%18 = load i8*, i8** %6
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = call i32 @strncpy(i8* %18, i8* %19, i32 %20)
%22 = load i8*, i8** %6
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
%26 = load i8*, i8** %6
store i8* %26, i8** %3
br label %29
27: ; preds = %2
%28 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %28, i8** %3
br label %29
29: ; preds = %27, %9
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @my_strndup(i8* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %12
4: ; preds = %2
%5 = add nuw nsw i32 %1, 4
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @memset(i8* %7, i32 0, i32 %5)
%9 = tail call i32 @strncpy(i8* %7, i8* %0, i32 %1)
%10 = zext i32 %1 to i64
%11 = getelementptr inbounds i8, i8* %7, i64 %10
store i8 0, i8* %11
br label %14
12: ; preds = %2
%13 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %4
%15 = phi i8* [ %7, %4 ], [ %13, %12 ]
ret i8* %15
}