#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*/ dologout(int);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ perror_reply(int, char *);
int strlen(char const *);
char *sgetsave(const char *s) {
  char *string;
  size_t len;
  if (s == NULL)
    s = "";
  len = strlen(s) + 1;
  string = malloc(len);
  if (string == NULL) {
    perror_reply(421, "Local resource failure: malloc");
    dologout(1);
  }
  memcpy(string, s, len);
  return string;
}


x86 assembly (clang, -O0):
<sgetsave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1172 <sgetsave+0x22>
lea    0xeb0(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <sgetsave+0x67>
mov    $0x1a5,%edi
lea    0xe58(%rip),%rsi
call   1070 <perror_reply@plt>
mov    $0x1,%edi
call   1040 <dologout@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sgetsave>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
lea    0xebf(%rip),%rbx
cmovne %rdi,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r14
mov    %r14,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    119c <sgetsave+0x4c>
lea    0xe78(%rip),%rsi
mov    $0x1a5,%edi
call   1070 <perror_reply@plt>
mov    $0x1,%edi
call   1040 <dologout@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [31 x i8] c"Local resource failure: malloc\00"
define i8* @sgetsave(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
br label %8
8: ; preds = %7, %1
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = call i32 @perror_reply(i32 421, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @dologout(i32 1)
br label %20
20: ; preds = %17, %8
%21 = load i8*, i8** %3
%22 = load i8*, i8** %2
%23 = load i64, i64* %4
%24 = call i32 @memcpy(i8* %21, i8* %22, i64 %23)
%25 = load i8*, i8** %3
ret i8* %25
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [31 x i8] c"Local resource failure: malloc\00"
define i8* @sgetsave(i8* %0) {
%2 = icmp eq i8* %0, null
%3 = select i1 %2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%4 = tail call i32 @strlen(i8* %3)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror_reply(i32 421, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @dologout(i32 1)
br label %12
12: ; preds = %9, %1
%13 = tail call i32 @memcpy(i8* %7, i8* %3, i64 %6)
ret i8* %7
}