#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 ERANGE;
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *strerror(int);
size_t strlen(char *);
int strerror_r(int error, char *buf, size_t size) {
  char *str = strerror(error);
  size_t len = strlen(str);
  if (size < len + 1) {
    return ERANGE;
  }
  memcpy(buf, str, len + 1);
  return 0;
}


x86 assembly (clang, -O0):
<strerror_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <strerror@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jae    1182 <strerror_r+0x52>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    119e <strerror_r+0x6e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strerror_r>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
call   1050 <strerror@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
cmp    %r15,%rax
jbe    115f <strerror_r+0x2f>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
jmp    116f <strerror_r+0x3f>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <memcpy@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ERANGE = global i32 0
define i32 @strerror_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i32, i32* %5
%11 = call i8* @strerror(i32 %10)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %9
%14 = load i64, i64* %7
%15 = load i64, i64* %9
%16 = add i64 %15, 1
%17 = icmp ult i64 %14, %16
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i32, i32* @ERANGE
store i32 %19, i32* %4
br label %26
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = load i8*, i8** %8
%23 = load i64, i64* %9
%24 = add i64 %23, 1
%25 = call i32 @memcpy(i8* %21, i8* %22, i64 %24)
store i32 0, i32* %4
br label %26
26: ; preds = %20, %18
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@ERANGE = global i32 0
define i32 @strerror_r(i32 %0, i8* %1, i64 %2) {
%4 = tail call i8* @strerror(i32 %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%6 = add i64 %5, 1
%7 = icmp ugt i64 %6, %2
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = load i32, i32* @ERANGE
br label %12
10: ; preds = %3
%11 = tail call i32 @memcpy(i8* %1, i8* %4, i64 %6)
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ %9, %8 ], [ 0, %10 ]
ret i32 %13
}