#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__ calloc(int, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *_s2_f2cstr(char *text, int textlen) {
  int l = textlen + 1;
  char *retval = (char *)calloc(l, sizeof(char));
  if (!retval) {
    return NULL;
  }
  strncpy(retval, text, l - 1);
  retval[l - 1] = '\0';
  return retval;
}


x86 assembly (clang, -O0):
<_s2_f2cstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1161 <_s2_f2cstr+0x41>
movq   $0x0,-0x8(%rbp)
jmp    118d <_s2_f2cstr+0x6d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_s2_f2cstr>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x1(%r14),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     1158 <_s2_f2cstr+0x38>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    115a <_s2_f2cstr+0x3a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @_s2_f2cstr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = add nsw i32 %8, 1
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = call i64 @calloc(i32 %10, i32 1)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* null, i8** %3
br label %28
16: ; preds = %2
%17 = load i8*, i8** %7
%18 = load i8*, i8** %4
%19 = load i32, i32* %6
%20 = sub nsw i32 %19, 1
%21 = call i32 @strncpy(i8* %17, i8* %18, i32 %20)
%22 = load i8*, i8** %7
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %7
store i8* %27, i8** %3
br label %28
28: ; preds = %16, %15
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
define i8* @_s2_f2cstr(i8* %0, i32 %1) {
%3 = add nsw i32 %1, 1
%4 = tail call i64 @calloc(i32 %3, i32 1)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = tail call i32 @strncpy(i8* %5, i8* %0, i32 %1)
%9 = sext i32 %1 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 %9
store i8 0, i8* %10
br label %11
11: ; preds = %2, %7
%12 = phi i8* [ %5, %7 ], [ null, %2 ]
ret i8* %12
}