#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 */
typedef int /*<<< orphan*/ error_buffer;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strerror_r(int, char *, int);
scalar_t__ strlen(char *);
void mk_errno_errmsg(const char *buffer, char **error_msg) {
  char error_buffer[1024];
  char msg_buffer[1024];
  if (error_msg) {
    if (strerror_r(errno, error_buffer, sizeof(error_buffer)) != 0) {
      strcpy(error_buffer, "Undetermined error");
    }
    snprintf(msg_buffer, 1024, "%s [(%d) %s]", buffer, errno, error_buffer);
    if ((*error_msg = malloc(strlen(msg_buffer) + 1)) == NULL) {
      fprintf(stderr, "Couldn't allocate memory for error message.\n");
      exit(EXIT_FAILURE);
    }
    strcpy(*error_msg, msg_buffer);
  }
}


x86 assembly (clang, -O0):
<mk_errno_errmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1251 <mk_errno_errmsg+0xe1>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <strerror_r@plt>
cmp    $0x0,%rax
je     11c5 <mk_errno_errmsg+0x55>
lea    -0x410(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x810(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    0x2e01(%rip),%rax
mov    (%rax),%r8d
lea    -0x410(%rbp),%r9
mov    $0x400,%esi
lea    0xe26(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x810(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    123e <mk_errno_errmsg+0xce>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x810(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mk_errno_errmsg>:
push   %r15
push   %r14
push   %rbx
sub    $0x800,%rsp
test   %rsi,%rsi
je     1200 <mk_errno_errmsg+0x90>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e4a(%rip),%r15
mov    (%r15),%edi
mov    %rsp,%rsi
mov    $0x400,%edx
call   1070 <strerror_r@plt>
test   %rax,%rax
je     11b2 <mk_errno_errmsg+0x42>
lea    0xe56(%rip),%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
mov    (%r15),%r8d
lea    0xe57(%rip),%rdx
lea    0x400(%rsp),%r15
mov    %rsp,%r9
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     120d <mk_errno_errmsg+0x9d>
lea    0x400(%rsp),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [19 x i8] c"Undetermined error\00"
@.str.1 = [13 x i8] c"%s [(%d) %s]\00"
@stderr = global i32 0
@.str.2 = [45 x i8] c"Couldn't allocate memory for error message.\0A\00"
@EXIT_FAILURE = global i32 0
define void @mk_errno_errmsg(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
store i8* %0, i8** %3
store i8** %1, i8*** %4
%7 = load i8**, i8*** %4
%8 = icmp ne i8** %7, null
br i1 %8, label %9, label %39
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%12 = call i64 @strerror_r(i32 %10, i8* %11, i32 1024)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %14, %9
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%19 = load i8*, i8** %3
%20 = load i32, i32* @errno
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i32 @snprintf(i8* %18, i32 1024, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %20, i8* %21)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%24 = call i64 @strlen(i8* %23)
%25 = add nsw i64 %24, 1
%26 = call i8* @malloc(i64 %25)
%27 = load i8**, i8*** %4
store i8* %26, i8** %27
%28 = icmp eq i8* %26, null
br i1 %28, label %29, label %34
29: ; preds = %17
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %17
%35 = load i8**, i8*** %4
%36 = load i8*, i8** %35
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%38 = call i32 @strcpy(i8* %36, i8* %37)
br label %39
39: ; preds = %34, %2
ret void
}

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