#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int slibcpy(char **sout, char const *sin, int sin_len, int errval) {
  int len = sin_len;
  free(*sout);
  if (sin == 0) {
    *sout = 0;
  } else {
    if (len == 0)
      len = strlen(sin);
    *sout = (char *)malloc((len + 1) * sizeof(char));
    if (*sout == 0) {
      printf("\nMemory allocation error\n");
      return errval;
    }
    strncpy(*sout, sin, len);
    (*sout)[len] = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<slibcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1193 <slibcpy+0x43>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
jmp    120d <slibcpy+0xbd>
cmpl   $0x0,-0x24(%rbp)
jne    11a9 <slibcpy+0x59>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11eb <slibcpy+0x9b>
lea    0xe25(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <slibcpy+0xc4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slibcpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1030 <free@plt>
test   %r15,%r15
je     11a6 <slibcpy+0x56>
test   %ebp,%ebp
jne    117d <slibcpy+0x2d>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     11b2 <slibcpy+0x62>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
mov    (%rbx),%rax
movslq %ebp,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ad <slibcpy+0x5d>
movq   $0x0,(%rbx)
xor    %r14d,%r14d
jmp    11be <slibcpy+0x6e>
lea    0xe47(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"\0AMemory allocation error\0A\00"
define i32 @slibcpy(i8** %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %8
store i32 %11, i32* %10
%12 = load i8**, i8*** %6
%13 = load i8*, i8** %12
%14 = call i32 @free(i8* %13)
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = load i8**, i8*** %6
store i8* null, i8** %18
br label %51
19: ; preds = %4
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %7
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %10
br label %25
25: ; preds = %22, %19
%26 = load i32, i32* %10
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 1
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
%33 = load i8**, i8*** %6
store i8* %32, i8** %33
%34 = load i8**, i8*** %6
%35 = load i8*, i8** %34
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %25
%38 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* %9
store i32 %39, i32* %5
br label %52
40: ; preds = %25
%41 = load i8**, i8*** %6
%42 = load i8*, i8** %41
%43 = load i8*, i8** %7
%44 = load i32, i32* %10
%45 = call i32 @strncpy(i8* %42, i8* %43, i32 %44)
%46 = load i8**, i8*** %6
%47 = load i8*, i8** %46
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
br label %51
51: ; preds = %40, %17
store i32 0, i32* %5
br label %52
52: ; preds = %51, %37
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
@str = [25 x i8] c"\0AMemory allocation error\00"
define i32 @slibcpy(i8** nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = load i8*, i8** %0
%6 = tail call i32 @free(i8* %5)
%7 = icmp eq i8* %1, null
br i1 %7, label %8, label %9
8: ; preds = %4
store i8* null, i8** %0
br label %26
9: ; preds = %4
%10 = icmp eq i32 %2, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 @strlen(i8* %1)
br label %13
13: ; preds = %11, %9
%14 = phi i32 [ %12, %11 ], [ %2, %9 ]
%15 = add nsw i32 %14, 1
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %0
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %26
21: ; preds = %13
%22 = tail call i32 @strncpy(i8* %17, i8* %1, i32 %14)
%23 = load i8*, i8** %0
%24 = sext i32 %14 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %8, %21, %19
%27 = phi i32 [ %3, %19 ], [ 0, %21 ], [ 0, %8 ]
ret i32 %27
}