#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 EINVAL;
int ERANGE;
int /*<<< orphan*/ __errstr(int, char *, char *, size_t);
size_t strlcpy(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ *sys_errlist;
int sys_nerr;
int strerror_r(int errnum, char *strerrbuf, size_t buflen) {
  int retval = 0;
  if (errnum < 0 || errnum >= sys_nerr) {
    __errstr(errnum, "Unknown error", strerrbuf, buflen);
    retval = EINVAL;
  } else {
    if (strlcpy(strerrbuf, sys_errlist[errnum], buflen) >= buflen)
      retval = ERANGE;
  }
  return (retval);
}


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)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1156 <strerror_r+0x36>
mov    -0x4(%rbp),%eax
mov    0x2e9a(%rip),%rcx
cmp    (%rcx),%eax
jl     117e <strerror_r+0x5e>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe98(%rip),%rsi
call   1040 <__errstr@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11b7 <strerror_r+0x97>
mov    -0x10(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x18(%rbp),%rdx
call   1030 <strlcpy@plt>
cmp    -0x18(%rbp),%rax
jb     11b2 <strerror_r+0x92>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11b7 <strerror_r+0x97>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strerror_r>:
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%rdx
test   %edi,%edi
js     1163 <strerror_r+0x43>
mov    0x2eb6(%rip),%rax
cmp    %edi,(%rax)
jle    1163 <strerror_r+0x43>
mov    0x2ebb(%rip),%rax
mov    (%rax),%rax
mov    %edi,%ecx
mov    (%rax,%rcx,4),%esi
mov    %rdx,%rdi
mov    %rbx,%rdx
call   1030 <strlcpy@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rbx,%rcx
jb     117b <strerror_r+0x5b>
mov    0x2e77(%rip),%rax
jmp    1179 <strerror_r+0x59>
lea    0xe96(%rip),%rsi
mov    %rbx,%rcx
call   1040 <__errstr@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@sys_nerr = global i32 0
@.str = [14 x i8] c"Unknown error\00"
@EINVAL = global i32 0
@sys_errlist = global i32* null
@ERANGE = global i32 0
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 %6, i1 false
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = tail call i32 @__errstr(i32 %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %1, i64 %2)
%10 = load i32, i32* @EINVAL
br label %20
11: ; preds = %3
%12 = load i32*, i32** @sys_errlist
%13 = zext i32 %0 to i64
%14 = getelementptr inbounds i32, i32* %12, i64 %13
%15 = load i32, i32* %14
%16 = tail call i64 @strlcpy(i8* %1, i32 %15, i64 %2)
%17 = icmp ult i64 %16, %2
%18 = load i32, i32* @ERANGE
%19 = select i1 %17, i32 0, i32 %18
br label %20
20: ; preds = %11, %8
%21 = phi i32 [ %10, %8 ], [ %19, %11 ]
ret i32 %21
}