#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 */
char *format_single_arg(char const *, char const *);
int /*<<< orphan*/ free(char *);
scalar_t__ is_null_or_empty(char const *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ portable_snprintf(char *, size_t, char const *, char const *,
                                     char *, char const *);
size_t strlen(char const *);
char *format_error_message(const char *fn, const char *act, const char *msg) {
  static const char *format = "Error %s%s: %s";
  static const char *fn_format = " (%s)";
  size_t n;
  char *p, *s;
  n = strlen(format) + strlen(act) + strlen(msg);
  if (is_null_or_empty(fn))
    s = NULL;
  else {
    s = format_single_arg(fn_format, fn);
    n += strlen(s);
  }
  p = (char *)malloc(n);
  if (s) {
    portable_snprintf(p, n, format, act, s, msg);
    free(s);
  } else
    portable_snprintf(p, n, format, act, "", msg);
  return p;
}


x86 assembly (clang, -O0):
<format_error_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2ed5(%rip),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <is_null_or_empty@plt>
cmp    $0x0,%rax
je     11d2 <format_error_message+0x72>
movq   $0x0,-0x30(%rbp)
jmp    11f7 <format_error_message+0x97>
mov    0x2e7f(%rip),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <format_single_arg@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     123d <format_error_message+0xdd>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e32(%rip),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    -0x18(%rbp),%r9
call   1060 <portable_snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1260 <format_error_message+0x100>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e04(%rip),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r9
lea    0xdb9(%rip),%r8
call   1060 <portable_snprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_error_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %rax,%rbp
add    $0xe,%rbp
mov    %rbx,%rdi
call   1080 <is_null_or_empty@plt>
test   %rax,%rax
je     11c7 <format_error_message+0x67>
mov    %rbp,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
lea    0xe53(%rip),%rdx
lea    0xe60(%rip),%r8
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r15,%rcx
mov    %r14,%r9
call   1060 <portable_snprintf@plt>
jmp    1217 <format_error_message+0xb7>
lea    0xe41(%rip),%rdi
mov    %rbx,%rsi
call   1050 <format_single_arg@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1040 <strlen@plt>
add    %rax,%rbp
mov    %rbp,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %r13,%r13
je     11a6 <format_error_message+0x46>
lea    0xe05(%rip),%rdx
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r15,%rcx
mov    %r13,%r8
mov    %r14,%r9
call   1060 <portable_snprintf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@format_error_message.format = internal global i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i32 0, i32 0)
@.str = [15 x i8] c"Error %s%s: %s\00"
@format_error_message.fn_format = internal global i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0)
@.str.1 = [6 x i8] c" (%s)\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @format_error_message(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** @format_error_message.format
%11 = call i64 @strlen(i8* %10)
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
%14 = add i64 %11, %13
%15 = load i8*, i8** %6
%16 = call i64 @strlen(i8* %15)
%17 = add i64 %14, %16
store i64 %17, i64* %7
%18 = load i8*, i8** %4
%19 = call i64 @is_null_or_empty(i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i8* null, i8** %9
br label %30
22: ; preds = %3
%23 = load i8*, i8** @format_error_message.fn_format
%24 = load i8*, i8** %4
%25 = call i8* @format_single_arg(i8* %23, i8* %24)
store i8* %25, i8** %9
%26 = load i8*, i8** %9
%27 = call i64 @strlen(i8* %26)
%28 = load i64, i64* %7
%29 = add i64 %28, %27
store i64 %29, i64* %7
br label %30
30: ; preds = %22, %21
%31 = load i64, i64* %7
%32 = call i64 @malloc(i64 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %8
%34 = load i8*, i8** %9
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %46
36: ; preds = %30
%37 = load i8*, i8** %8
%38 = load i64, i64* %7
%39 = load i8*, i8** @format_error_message.format
%40 = load i8*, i8** %5
%41 = load i8*, i8** %9
%42 = load i8*, i8** %6
%43 = call i32 @portable_snprintf(i8* %37, i64 %38, i8* %39, i8* %40, i8* %41, i8* %42)
%44 = load i8*, i8** %9
%45 = call i32 @free(i8* %44)
br label %53
46: ; preds = %30
%47 = load i8*, i8** %8
%48 = load i64, i64* %7
%49 = load i8*, i8** @format_error_message.format
%50 = load i8*, i8** %5
%51 = load i8*, i8** %6
%52 = call i32 @portable_snprintf(i8* %47, i64 %48, i8* %49, i8* %50, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %46, %36
%54 = load i8*, i8** %8
ret i8* %54
}

LLVM IR (-O2):
@.str = [15 x i8] c"Error %s%s: %s\00"
@.str.1 = [6 x i8] c" (%s)\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @format_error_message(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %4, 14
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = add i64 %5, %6
%8 = tail call i64 @is_null_or_empty(i8* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = tail call i64 @malloc(i64 %7)
%12 = inttoptr i64 %11 to i8*
br label %23
13: ; preds = %3
%14 = tail call i8* @format_single_arg(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%15 = tail call i64 @strlen(i8* dereferenceable(1) %14)
%16 = add i64 %15, %7
%17 = tail call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i8* %14, null
br i1 %19, label %23, label %20
20: ; preds = %13
%21 = tail call i32 @portable_snprintf(i8* %18, i64 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %14, i8* %2)
%22 = tail call i32 @free(i8* %14)
br label %27
23: ; preds = %10, %13
%24 = phi i8* [ %12, %10 ], [ %18, %13 ]
%25 = phi i64 [ %7, %10 ], [ %16, %13 ]
%26 = tail call i32 @portable_snprintf(i8* %24, i64 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %27
27: ; preds = %23, %20
%28 = phi i8* [ %24, %23 ], [ %18, %20 ]
ret i8* %28
}