#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 errno;
char *malloc(size_t);
size_t snprintf(int /*<<< orphan*/ *, int /*<<< orphan*/, char *, char const *,
                char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *, int);
char *strerror(int);
char *get_error_message(char const *msg) {
  size_t needed = snprintf(NULL, 0, "%s: %s (%d)", msg, strerror(errno), errno);
  char *buffer = malloc(needed + 1);
  sprintf(buffer, "%s: %s (%d)", msg, strerror(errno), errno);
  return buffer;
}


x86 assembly (clang, -O0):
<get_error_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x30(%rbp),%rcx
mov    %rax,%r8
mov    0x2e70(%rip),%rax
mov    (%rax),%r9d
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xe80(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
mov    0x2e16(%rip),%rax
mov    (%rax),%r8d
lea    0xe2c(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_error_message>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e8e(%rip),%r12
mov    (%r12),%edi
call   1060 <strerror@plt>
mov    (%r12),%r9d
lea    0xe9a(%rip),%r14
xor    %edi,%edi
xor    %esi,%esi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rax,%r8
call   1030 <snprintf@plt>
lea    0x1(%rax),%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    (%r12),%edi
call   1060 <strerror@plt>
mov    (%r12),%r8d
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %rax,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s: %s (%d)\00"
@errno = global i32 0
define i8* @get_error_message(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @errno
%7 = call i8* @strerror(i32 %6)
%8 = load i32, i32* @errno
%9 = call i64 @snprintf(i32* null, i32 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %7, i32 %8)
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = add i64 %10, 1
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = load i8*, i8** %2
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = load i32, i32* @errno
%18 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %16, i32 %17)
%19 = load i8*, i8** %4
ret i8* %19
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s: %s (%d)\00"
@errno = global i32 0
define noalias i8* @get_error_message(i8* %0) {
%2 = load i32, i32* @errno
%3 = tail call i8* @strerror(i32 %2)
%4 = load i32, i32* @errno
%5 = tail call i64 @snprintf(i32* null, i32 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %3, i32 %4)
%6 = add i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = load i32, i32* @errno
%11 = tail call i32 @sprintf(i8* dereferenceable(1) %7, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9, i32 %10)
ret i8* %7
}