#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *stderr;
char **sys_errlist;
int sys_nerr;
void perror(const char *s) {
  FILE *f = stderr;
  if (s && s[0])
    fprintf(f, "%s: ", s);
  if (errno >= sys_nerr)
    fprintf(f, "Unknown error: %d\n", errno);
  else
    fprintf(f, "%s\n", sys_errlist[errno]);
}


x86 assembly (clang, -O0):
<perror>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     115b <perror+0x4b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     115b <perror+0x4b>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xeac(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    0x2e7d(%rip),%rcx
cmp    (%rcx),%eax
jl     1193 <perror+0x83>
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
lea    0xe7e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    11bd <perror+0xad>
mov    -0x10(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e20(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perror>:
push   %rbx
mov    0x2eb8(%rip),%rax
mov    (%rax),%rbx
test   %rdi,%rdi
je     1139 <perror+0x29>
mov    %rdi,%rdx
cmpb   $0x0,(%rdi)
je     1139 <perror+0x29>
lea    0xed1(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2e88(%rip),%rax
movslq (%rax),%rdx
mov    0x2e9e(%rip),%rax
cmp    (%rax),%edx
jge    116e <perror+0x5e>
mov    0x2ea3(%rip),%rax
mov    (%rax),%rax
mov    (%rax,%rdx,8),%rdx
lea    0xeb5(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
pop    %rbx
jmp    1030 <fprintf@plt>
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
pop    %rbx
jmp    1030 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32* null
@.str = [5 x i8] c"%s: \00"
@errno = global i32 0
@sys_nerr = global i32 0
@.str.1 = [19 x i8] c"Unknown error: %d\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@sys_errlist = global i8** null
define void @perror(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = load i32*, i32** @stderr
store i32* %4, i32** %3
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %17
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = load i32*, i32** %3
%15 = load i8*, i8** %2
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %13, %7, %1
%18 = load i32, i32* @errno
%19 = load i32, i32* @sys_nerr
%20 = icmp sge i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32*, i32** %3
%23 = load i32, i32* @errno
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %33
25: ; preds = %17
%26 = load i32*, i32** %3
%27 = load i8**, i8*** @sys_errlist
%28 = load i32, i32* @errno
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %25, %21
ret void
}

LLVM IR (-O2):
@stderr = global i32* null
@.str = [5 x i8] c"%s: \00"
@errno = global i32 0
@sys_nerr = global i32 0
@.str.1 = [19 x i8] c"Unknown error: %d\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@sys_errlist = global i8** null
define void @perror(i8* %0) {
%2 = load i32*, i32** @stderr
%3 = icmp eq i8* %0, null
br i1 %3, label %9, label %4
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %4, %1
%10 = load i32, i32* @errno
%11 = load i32, i32* @sys_nerr
%12 = icmp slt i32 %10, %11
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %21
15: ; preds = %9
%16 = load i8**, i8*** @sys_errlist
%17 = sext i32 %10 to i64
%18 = getelementptr inbounds i8*, i8** %16, i64 %17
%19 = load i8*, i8** %18
%20 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %15, %13
ret void
}