#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*/ bzero(char *, int);
char *malloc(int);
char *strncpy(char *, char *, int);
char *my_strndup(char *s, int n) {
  char *res;
  if (s) {
    if (!(res = malloc(sizeof(char) * (n + 1))))
      return (NULL);
    bzero(res, n + 1);
    return (strncpy(res, s, n));
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<my_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a0 <my_strndup+0x70>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1178 <my_strndup+0x48>
movq   $0x0,-0x8(%rbp)
jmp    11a8 <my_strndup+0x78>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1050 <bzero@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    %rax,-0x8(%rbp)
jmp    11a8 <my_strndup+0x78>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strndup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1179 <my_strndup+0x49>
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x1(%r14),%r15d
mov    %r15d,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1179 <my_strndup+0x49>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15d,%esi
call   1050 <bzero@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strncpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
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 i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %27
9: ; preds = %2
%10 = load i32, i32* %5
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = mul i64 1, %12
%14 = trunc i64 %13 to i32
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %9
store i8* null, i8** %3
br label %28
18: ; preds = %9
%19 = load i8*, i8** %6
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = call i32 @bzero(i8* %19, i32 %21)
%23 = load i8*, i8** %6
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = call i8* @strncpy(i8* %23, i8* %24, i32 %25)
store i8* %26, i8** %3
br label %28
27: ; preds = %2
store i8* null, i8** %3
br label %28
28: ; preds = %27, %18, %17
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
define i8* @my_strndup(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = add nsw i32 %1, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i32 @bzero(i8* %6, i32 %5)
%10 = tail call i8* @strncpy(i8* %6, i8* %0, i32 %1)
br label %11
11: ; preds = %2, %4, %8
%12 = phi i8* [ %10, %8 ], [ null, %4 ], [ null, %2 ]
ret i8* %12
}