#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 *CRYPTO_malloc(size_t, char const *, int);
size_t OPENSSL_strnlen(char const *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *CRYPTO_strndup(const char *str, size_t s, const char *file, int line) {
  size_t maxlen;
  char *ret;
  if (str == NULL)
    return NULL;
  maxlen = OPENSSL_strnlen(str, s);
  ret = CRYPTO_malloc(maxlen + 1, file, line);
  if (ret) {
    memcpy(ret, str, maxlen);
    ret[maxlen] = '\0';
  }
  return ret;
}


x86 assembly (clang, -O0):
<CRYPTO_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115f <CRYPTO_strndup+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <CRYPTO_strndup+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <OPENSSL_strnlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <CRYPTO_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11b0 <CRYPTO_strndup+0x80>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CRYPTO_strndup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1177 <CRYPTO_strndup+0x47>
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rdi,%r14
call   1030 <OPENSSL_strnlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <CRYPTO_malloc@plt>
test   %rax,%rax
je     1177 <CRYPTO_strndup+0x47>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    1179 <CRYPTO_strndup+0x49>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @CRYPTO_strndup(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %4
store i8* null, i8** %5
br label %36
15: ; preds = %4
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i64 @OPENSSL_strnlen(i8* %16, i64 %17)
store i64 %18, i64* %10
%19 = load i64, i64* %10
%20 = add i64 %19, 1
%21 = load i8*, i8** %8
%22 = load i32, i32* %9
%23 = call i8* @CRYPTO_malloc(i64 %20, i8* %21, i32 %22)
store i8* %23, i8** %11
%24 = load i8*, i8** %11
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %34
26: ; preds = %15
%27 = load i8*, i8** %11
%28 = load i8*, i8** %6
%29 = load i64, i64* %10
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %11
%32 = load i64, i64* %10
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %26, %15
%35 = load i8*, i8** %11
store i8* %35, i8** %5
br label %36
36: ; preds = %34, %14
%37 = load i8*, i8** %5
ret i8* %37
}

LLVM IR (-O2):
define i8* @CRYPTO_strndup(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %14, label %6
6: ; preds = %4
%7 = tail call i64 @OPENSSL_strnlen(i8* %0, i64 %1)
%8 = add i64 %7, 1
%9 = tail call i8* @CRYPTO_malloc(i64 %8, i8* %2, i32 %3)
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = tail call i32 @memcpy(i8* %9, i8* %0, i64 %7)
%13 = getelementptr inbounds i8, i8* %9, i64 %7
store i8 0, i8* %13
br label %14
14: ; preds = %6, %11, %4
%15 = phi i8* [ null, %4 ], [ %9, %11 ], [ null, %6 ]
ret i8* %15
}