#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*/ snprintf(char *, size_t, char *, int);
int /*<<< orphan*/ strlcpy(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ *sys_errlist;
int sys_nerr;
int strerror_r(int num, char *buf, size_t len) {
  if (num >= 0 || num < sys_nerr)
    strlcpy(buf, sys_errlist[num], len);
  else
    snprintf(buf, len, "Unknown error %d", num);
  return 0;
}


x86 assembly (clang, -O0):
<strerror_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    114f <strerror_r+0x2f>
mov    -0x4(%rbp),%eax
mov    0x2ea1(%rip),%rcx
cmp    (%rcx),%eax
jge    1172 <strerror_r+0x52>
mov    -0x10(%rbp),%rdi
mov    0x2e9e(%rip),%rax
mov    (%rax),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x18(%rbp),%rdx
call   1040 <strlcpy@plt>
jmp    1189 <strerror_r+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%ecx
lea    0xe7c(%rip),%rdx
call   1030 <snprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strerror_r>:
push   %rax
mov    %rsi,%rax
mov    %edi,%ecx
test   %edi,%edi
jns    114c <strerror_r+0x2c>
mov    0x2eb7(%rip),%rsi
cmp    %ecx,(%rsi)
jg     114c <strerror_r+0x2c>
lea    0xec4(%rip),%r8
mov    %rax,%rdi
mov    %rdx,%rsi
mov    %r8,%rdx
call   1030 <snprintf@plt>
jmp    1164 <strerror_r+0x44>
mov    0x2ea5(%rip),%rsi
mov    (%rsi),%rsi
movslq %ecx,%rcx
mov    (%rsi,%rcx,4),%esi
mov    %rax,%rdi
call   1040 <strlcpy@plt>
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
@sys_nerr = global i32 0
@sys_errlist = global i32* null
@.str = [17 x i8] c"Unknown error %d\00"
define i32 @strerror_r(i32 %0, i8* %1, i64 %2) {
%4 = icmp sgt i32 %0, -1
%5 = load i32, i32* @sys_nerr
%6 = icmp sgt i32 %5, %0
%7 = select i1 %4, i1 true, i1 %6
br i1 %7, label %8, label %14
8: ; preds = %3
%9 = load i32*, i32** @sys_errlist
%10 = sext i32 %0 to i64
%11 = getelementptr inbounds i32, i32* %9, i64 %10
%12 = load i32, i32* %11
%13 = tail call i32 @strlcpy(i8* %1, i32 %12, i64 %2)
br label %16
14: ; preds = %3
%15 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %16
16: ; preds = %14, %8
ret i32 0
}