#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 */
scalar_t__ errno;
int /*<<< orphan*/ message(char *, char *, ...);
char *strerror(scalar_t__);
void my_perror(char *msg) {
  if (errno == 0) {
    message("error: %s.\n", msg);
  } else {
    char *errstr = strerror(errno);
    message("error: %s: [%d] %s\n", msg, errno, errstr);
  }
}


x86 assembly (clang, -O0):
<my_perror>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
cmpq   $0x0,(%rax)
jne    1154 <my_perror+0x34>
mov    -0x8(%rbp),%rsi
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1030 <message@plt>
jmp    1187 <my_perror+0x67>
mov    0x2e85(%rip),%rax
mov    (%rax),%rdi
call   1040 <strerror@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2e6e(%rip),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <message@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_perror>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eb2(%rip),%r14
mov    (%r14),%rdi
test   %rdi,%rdi
je     1159 <my_perror+0x39>
call   1040 <strerror@plt>
mov    (%r14),%rdx
lea    0xec7(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <message@plt>
lea    0xea0(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <message@plt>

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

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