#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);
char const *strerror(int);
char const *graphdb_strerror(int err, char *buf, size_t bufsize) {
  if (err == 0)
    return "no error";
  if (err > 0)
    return strerror(err);
  if (buf == NULL)
    return "unexpected error";
  snprintf(buf, bufsize, "unexpected error %d", err);
  return buf;
}


x86 assembly (clang, -O0):
<graphdb_strerror>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    114d <graphdb_strerror+0x2d>
lea    0xebc(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a2 <graphdb_strerror+0x82>
cmpl   $0x0,-0xc(%rbp)
jle    1168 <graphdb_strerror+0x48>
mov    -0xc(%rbp),%edi
call   1040 <strerror@plt>
mov    %rax,-0x8(%rbp)
jmp    11a2 <graphdb_strerror+0x82>
cmpq   $0x0,-0x18(%rbp)
jne    1183 <graphdb_strerror+0x63>
lea    0xe8f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a2 <graphdb_strerror+0x82>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%ecx
lea    0xe85(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<graphdb_strerror>:
push   %rbx
test   %edi,%edi
je     1131 <graphdb_strerror+0x11>
mov    %edi,%ecx
jle    113a <graphdb_strerror+0x1a>
mov    %ecx,%edi
pop    %rbx
jmp    1040 <strerror@plt>
lea    0xec8(%rip),%rbx
jmp    1160 <graphdb_strerror+0x40>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1159 <graphdb_strerror+0x39>
lea    0xed1(%rip),%rax
mov    %rbx,%rdi
mov    %rdx,%rsi
mov    %rax,%rdx
call   1030 <snprintf@plt>
jmp    1160 <graphdb_strerror+0x40>
lea    0xea9(%rip),%rbx
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"no error\00"
@.str.1 = [17 x i8] c"unexpected error\00"
@.str.2 = [20 x i8] c"unexpected error %d\00"
define i8* @graphdb_strerror(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i32 %0, 0
br i1 %4, label %13, label %5
5: ; preds = %3
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call i8* @strerror(i32 %0)
br label %13
9: ; preds = %5
%10 = icmp eq i8* %1, null
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %13
13: ; preds = %9, %3, %11, %7
%14 = phi i8* [ %8, %7 ], [ %1, %11 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %9 ]
ret i8* %14
}