#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*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ h_errno;
char *hstrerror(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
char *strerror(int /*<<< orphan*/);
int strlen(char const *);
char *create_buffer(int err_type, const char *msg) {
  const char *ptr;
  char *buffer;
  int buflen = strlen(msg) + 1;
  switch (err_type) {
  case 1:
    ptr = strerror(errno);
    break;
  case 2:
    ptr = hstrerror(h_errno);
    break;
  default:
    exit(EXIT_FAILURE);
  }
  buflen += strlen(ptr) + 2;
  if ((buffer = (char *)malloc(buflen)) == NULL) {
    perror("malloc() failed");
    exit(EXIT_FAILURE);
  }
  (void)sprintf(buffer, "%s: %s", msg, ptr);
  *(buffer + buflen - 1) = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<create_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x1,%eax
je     11b3 <create_buffer+0x43>
jmp    11a2 <create_buffer+0x32>
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
je     11ca <create_buffer+0x5a>
jmp    11e1 <create_buffer+0x71>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x18(%rbp)
jmp    11ef <create_buffer+0x7f>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1060 <hstrerror@plt>
mov    %rax,-0x18(%rbp)
jmp    11ef <create_buffer+0x7f>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x2,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1231 <create_buffer+0xc1>
lea    0xde2(%rip),%rdi
call   1050 <perror@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdcc(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%ebx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
cmp    $0x2,%ebx
je     11a2 <create_buffer+0x32>
cmp    $0x1,%ebx
jne    1206 <create_buffer+0x96>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
jmp    11b0 <create_buffer+0x40>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1060 <hstrerror@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1030 <strlen@plt>
lea    (%r14,%rax,1),%ebp
add    $0x3,%ebp
mov    %ebp,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11fa <create_buffer+0x8a>
mov    %rax,%rbx
lea    0xe38(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r12,%rcx
call   1070 <sprintf@plt>
movslq %ebp,%rax
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdff(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
@h_errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [16 x i8] c"malloc() failed\00"
@.str.1 = [7 x i8] c"%s: %s\00"
define i8* @create_buffer(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
store i32 %10, i32* %7
%11 = load i32, i32* %3
switch i32 %11, label %18 [
i32 1, label %12
i32 2, label %15
]
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
store i8* %14, i8** %5
br label %21
15: ; preds = %2
%16 = load i32, i32* @h_errno
%17 = call i8* @hstrerror(i32 %16)
store i8* %17, i8** %5
br label %21
18: ; preds = %2
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15, %12
%22 = load i8*, i8** %5
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %23, 2
%25 = load i32, i32* %7
%26 = add nsw i32 %25, %24
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %21
%36 = load i8*, i8** %6
%37 = load i8*, i8** %4
%38 = load i8*, i8** %5
%39 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %37, i8* %38)
%40 = load i8*, i8** %6
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = getelementptr inbounds i8, i8* %43, i64 -1
store i8 0, i8* %44
%45 = load i8*, i8** %6
ret i8* %45
}

LLVM IR (-O2):
@errno = global i32 0
@h_errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [16 x i8] c"malloc() failed\00"
@.str.1 = [7 x i8] c"%s: %s\00"
define i8* @create_buffer(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
switch i32 %0, label %10 [
i32 1, label %4
i32 2, label %7
]
4: ; preds = %2
%5 = load i32, i32* @errno
%6 = tail call i8* @strerror(i32 %5)
br label %13
7: ; preds = %2
%8 = load i32, i32* @h_errno
%9 = tail call i8* @hstrerror(i32 %8)
br label %13
10: ; preds = %2
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %7, %4
%14 = phi i8* [ %9, %7 ], [ %6, %4 ]
%15 = tail call i32 @strlen(i8* %14)
%16 = add i32 %3, 3
%17 = add i32 %16, %15
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %13
%26 = tail call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %14)
%27 = sext i32 %17 to i64
%28 = getelementptr inbounds i8, i8* %19, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -1
store i8 0, i8* %29
ret i8* %19
}