#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__ ERANGE;
scalar_t__ errno;
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strerror_r(int, char *, int);
void STRERROR(int errnum, char *errorstr) {
  if (strerror_r(errnum, errorstr, 512) < 0) {
    if (errno == ERANGE)
      sprintf(errorstr, "Error string too short to hold errno: %d", errnum);
    else
      sprintf(errorstr, "Unkown error: %d", errnum);
  }
}


x86 assembly (clang, -O0):
<STRERROR>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strerror_r@plt>
cmp    $0x0,%rax
jge    1194 <STRERROR+0x74>
mov    0x2e87(%rip),%rax
mov    (%rax),%rax
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rax
jne    117c <STRERROR+0x5c>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe8e(%rip),%rsi
call   1040 <sprintf@plt>
jmp    118f <STRERROR+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe9f(%rip),%rsi
call   1040 <sprintf@plt>
jmp    1194 <STRERROR+0x74>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<STRERROR>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x200,%edx
call   1030 <strerror_r@plt>
test   %rax,%rax
js     113e <STRERROR+0x1e>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e93(%rip),%rax
mov    (%rax),%rax
mov    0x2e99(%rip),%rcx
cmp    (%rcx),%rax
lea    0xea7(%rip),%rax
lea    0xec9(%rip),%rsi
cmove  %rax,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <sprintf@plt>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [41 x i8] c"Error string too short to hold errno: %d\00"
@.str.1 = [17 x i8] c"Unkown error: %d\00"
define void @STRERROR(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = load i32, i32* %3
%6 = load i8*, i8** %4
%7 = call i64 @strerror_r(i32 %5, i8* %6, i32 512)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %2
%10 = load i64, i64* @errno
%11 = load i64, i64* @ERANGE
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i32, i32* %3
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %21
17: ; preds = %9
%18 = load i8*, i8** %4
%19 = load i32, i32* %3
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %13
br label %22
22: ; preds = %21, %2
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [41 x i8] c"Error string too short to hold errno: %d\00"
@.str.1 = [17 x i8] c"Unkown error: %d\00"
define void @STRERROR(i32 %0, i8* %1) {
%3 = tail call i64 @strerror_r(i32 %0, i8* %1, i32 512)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = load i64, i64* @ERANGE
%8 = icmp eq i64 %6, %7
%9 = select i1 %8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0)
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) %9, i32 %0)
br label %11
11: ; preds = %5, %2
ret void
}