#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*/ abort();
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
char *strerror(int);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strerror_copy(const char *prefix, int errnum) {
  if (!prefix) {
    prefix = "";
  }
  size_t prefixLen = strlen(prefix);
  const char *errString = strerror(errnum);
  if (!errString) {
    errString = "(unknown)";
  }
  size_t errLen = strlen(errString);
  char *buffer = malloc(prefixLen + errLen + 1);
  if (!buffer) {
    perror("Could not allocate space for error string");
    abort();
  }
  strncpy(buffer, prefix, prefixLen);
  strncpy(buffer + prefixLen, errString, errLen);
  buffer[prefixLen + errLen] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<strerror_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1185 <strerror_copy+0x25>
lea    0xe88(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <strerror@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b4 <strerror_copy+0x54>
lea    0xe50(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f4 <strerror_copy+0x94>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x30(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strerror_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
test   %rdi,%rdi
lea    0xe92(%rip),%r14
cmovne %rdi,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1080 <strerror@plt>
test   %rax,%rax
lea    0xe69(%rip),%r13
cmovne %rax,%r13
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
lea    (%rax,%r15,1),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f6 <strerror_copy+0x96>
mov    %rax,%rbx
mov    %r12,%rbp
add    %r15,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
add    %r15,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1040 <strncpy@plt>
movb   $0x0,(%rbx,%rbp,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [10 x i8] c"(unknown)\00"
@.str.2 = [42 x i8] c"Could not allocate space for error string\00"
define i8* @strerror_copy(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %3
br label %12
12: ; preds = %11, %2
%13 = load i8*, i8** %3
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %5
%15 = load i32, i32* %4
%16 = call i8* @strerror(i32 %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %12
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %20
20: ; preds = %19, %12
%21 = load i8*, i8** %6
%22 = call i64 @strlen(i8* %21)
store i64 %22, i64* %7
%23 = load i64, i64* %5
%24 = load i64, i64* %7
%25 = add i64 %23, %24
%26 = add i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 (...) @abort()
unreachable
33: ; preds = %20
%34 = load i8*, i8** %8
%35 = load i8*, i8** %3
%36 = load i64, i64* %5
%37 = call i32 @strncpy(i8* %34, i8* %35, i64 %36)
%38 = load i8*, i8** %8
%39 = load i64, i64* %5
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = load i8*, i8** %6
%42 = load i64, i64* %7
%43 = call i32 @strncpy(i8* %40, i8* %41, i64 %42)
%44 = load i8*, i8** %8
%45 = load i64, i64* %5
%46 = load i64, i64* %7
%47 = add i64 %45, %46
%48 = getelementptr inbounds i8, i8* %44, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %8
ret i8* %49
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [10 x i8] c"(unknown)\00"
@.str.2 = [42 x i8] c"Could not allocate space for error string\00"
define i8* @strerror_copy(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = select i1 %3, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%5 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%6 = tail call i8* @strerror(i32 %1)
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %6
%9 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%10 = add i64 %9, %5
%11 = add i64 %10, 1
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 (...) @abort()
unreachable
17: ; preds = %2
%18 = tail call i32 @strncpy(i8* %12, i8* %4, i64 %5)
%19 = getelementptr inbounds i8, i8* %12, i64 %5
%20 = tail call i32 @strncpy(i8* %19, i8* %8, i64 %9)
%21 = getelementptr inbounds i8, i8* %12, i64 %10
store i8 0, i8* %21
ret i8* %12
}