#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char const *);
void mk_errmsg(const char *buffer, char **error_msg) {
  if (error_msg) {
    if ((*error_msg = malloc(strlen(buffer) + 1)) == NULL) {
      fprintf(stderr, "Couldn't allocate memory for error message.\n");
      exit(EXIT_FAILURE);
    }
    sprintf(*error_msg, "%s", buffer);
  }
}


x86 assembly (clang, -O0):
<mk_errmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11cb <mk_errmsg+0x7b>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11b4 <mk_errmsg+0x64>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe67(%rip),%rsi
call   1060 <sprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mk_errmsg>:
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1187 <mk_errmsg+0x37>
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     118f <mk_errmsg+0x3f>
mov    %rax,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <strcpy@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"Couldn't allocate memory for error message.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define void @mk_errmsg(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
store i8* %0, i8** %3
store i8** %1, i8*** %4
%5 = load i8**, i8*** %4
%6 = icmp ne i8** %5, null
br i1 %6, label %7, label %24
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
%10 = add nsw i64 %9, 1
%11 = call i8* @malloc(i64 %10)
%12 = load i8**, i8*** %4
store i8* %11, i8** %12
%13 = icmp eq i8* %11, null
br i1 %13, label %14, label %19
14: ; preds = %7
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %7
%20 = load i8**, i8*** %4
%21 = load i8*, i8** %20
%22 = load i8*, i8** %3
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %19, %2
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"Couldn't allocate memory for error message.\0A\00"
@EXIT_FAILURE = global i32 0
define void @mk_errmsg(i8* nocapture %0, i8** writeonly %1) {
%3 = icmp eq i8** %1, null
br i1 %3, label %16, label %4
4: ; preds = %2
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add nsw i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
store i8* %7, i8** %1
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4
%15 = tail call i8* @strcpy(i8* %7, i8* dereferenceable(1) %0)
br label %16
16: ; preds = %14, %2
ret void
}