#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*/ errno;
int /*<<< orphan*/ printk(char *, char const *, ...);
scalar_t__ strerror(int /*<<< orphan*/);
void perror(const char *error_msg) {
  const char *error = (const char *)strerror(errno);
  if (error_msg && *error_msg != '\0')
    printk("%s: %s\n", error_msg, error);
  else
    printk("%s\n", error);
}


x86 assembly (clang, -O0):
<perror>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1174 <perror+0x54>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1174 <perror+0x54>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
jmp    1186 <perror+0x66>
mov    -0x10(%rbp),%rsi
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perror>:
push   %rbx
mov    %rdi,%rbx
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
test   %rbx,%rbx
je     1151 <perror+0x31>
cmpb   $0x0,(%rbx)
je     1151 <perror+0x31>
lea    0xebd(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
pop    %rbx
jmp    1030 <printk@plt>
lea    0xeac(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
pop    %rbx
jmp    1030 <printk@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @perror(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i32, i32* @errno
%5 = call i64 @strerror(i32 %4)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %3
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %18
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i8*, i8** %2
%16 = load i8*, i8** %3
%17 = call i32 (i8*, i8*, ...) @printk(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
br label %21
18: ; preds = %9, %1
%19 = load i8*, i8** %3
%20 = call i32 (i8*, i8*, ...) @printk(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %18, %14
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @perror(i8* %0) {
%2 = load i32, i32* @errno
%3 = tail call i64 @strerror(i32 %2)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 (i8*, i8*, ...) @printk(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %4)
br label %13
11: ; preds = %6, %1
%12 = tail call i32 (i8*, i8*, ...) @printk(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %13
13: ; preds = %11, %9
ret void
}