#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);
scalar_t__ strerror_r(int, char *, size_t);
char *sdb_strerror(int errnum, char *strerrbuf, size_t buflen) {
  *strerrbuf = '\0';
  if (strerror_r(errnum, strerrbuf, buflen))
    snprintf(strerrbuf, buflen,
             "unknown error #%i "
             "(strerror_r(3) failed)",
             errnum);
  strerrbuf[buflen - 1] = '\0';
  return strerrbuf;
}


x86 assembly (clang, -O0):
<sdb_strerror>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strerror_r@plt>
cmp    $0x0,%rax
je     116b <sdb_strerror+0x4b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%ecx
lea    0xe9a(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdb_strerror>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
movb   $0x0,(%rsi)
call   1040 <strerror_r@plt>
test   %rax,%rax
je     114d <sdb_strerror+0x2d>
lea    0xec0(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"unknown error #%i (strerror_r(3) failed)\00"
define i8* @sdb_strerror(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 i8*, i8** %5
store i8 0, i8* %7
%8 = load i32, i32* %4
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = call i64 @strerror_r(i32 %8, i8* %9, i64 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = load i32, i32* %4
%17 = call i32 @snprintf(i8* %14, i64 %15, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %13, %3
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = sub i64 %20, 1
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 0, i8* %22
%23 = load i8*, i8** %5
ret i8* %23
}

LLVM IR (-O2):
@.str = [41 x i8] c"unknown error #%i (strerror_r(3) failed)\00"
define i8* @sdb_strerror(i32 %0, i8* returned %1, i64 %2) {
store i8 0, i8* %1
%4 = tail call i64 @strerror_r(i32 %0, i8* %1, i64 %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %8
8: ; preds = %6, %3
%9 = add i64 %2, -1
%10 = getelementptr inbounds i8, i8* %1, i64 %9
store i8 0, i8* %10
ret i8* %1
}