#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*/ ERANGE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memmove(char *, char *, size_t);
char *strerror_r(int /*<<< orphan*/, char *, size_t);
int strlen(char *);
int NaClGetLastErrorString(char *buffer, size_t length) {
  char *message;
  if (buffer == NULL || length == 0) {
    errno = ERANGE;
    return -1;
  }
  message = strerror_r(errno, buffer, length);
  if (message != buffer) {
    size_t message_bytes = strlen(message) + 1;
    if (message_bytes < length) {
      length = message_bytes;
    }
    memmove(buffer, message, length);
    buffer[length - 1] = '\0';
  }
  return 0;
}


x86 assembly (clang, -O0):
<NaClGetLastErrorString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <NaClGetLastErrorString+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1174 <NaClGetLastErrorString+0x44>
mov    0x2e8b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e72(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <NaClGetLastErrorString+0xbc>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strerror_r@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11e5 <NaClGetLastErrorString+0xb5>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c4 <NaClGetLastErrorString+0x94>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NaClGetLastErrorString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1190 <NaClGetLastErrorString+0x60>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1190 <NaClGetLastErrorString+0x60>
mov    %rdi,%r14
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <strerror_r@plt>
xor    %r15d,%r15d
cmp    %r14,%rax
je     11a8 <NaClGetLastErrorString+0x78>
mov    %rax,%rbp
mov    %rax,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %rbx,%rax
cmovb  %rax,%rbx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1050 <memmove@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
jmp    11a8 <NaClGetLastErrorString+0x78>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    0x2e38(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ERANGE = global i32 0
@errno = global i32 0
define i32 @NaClGetLastErrorString(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10, %2
%14 = load i32, i32* @ERANGE
store i32 %14, i32* @errno
store i32 -1, i32* %3
br label %43
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = load i8*, i8** %4
%18 = load i64, i64* %5
%19 = call i8* @strerror_r(i32 %16, i8* %17, i64 %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = icmp ne i8* %20, %21
br i1 %22, label %23, label %42
23: ; preds = %15
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
store i64 %27, i64* %7
%28 = load i64, i64* %7
%29 = load i64, i64* %5
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i64, i64* %7
store i64 %32, i64* %5
br label %33
33: ; preds = %31, %23
%34 = load i8*, i8** %4
%35 = load i8*, i8** %6
%36 = load i64, i64* %5
%37 = call i32 @memmove(i8* %34, i8* %35, i64 %36)
%38 = load i8*, i8** %4
%39 = load i64, i64* %5
%40 = sub i64 %39, 1
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %33, %15
store i32 0, i32* %3
br label %43
43: ; preds = %42, %13
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@ERANGE = global i32 0
@errno = global i32 0
define i32 @NaClGetLastErrorString(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i64 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @ERANGE
store i32 %7, i32* @errno
br label %21
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror_r(i32 %9, i8* %0, i64 %1)
%11 = icmp eq i8* %10, %0
br i1 %11, label %21, label %12
12: ; preds = %8
%13 = tail call i32 @strlen(i8* %10)
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = icmp ult i64 %15, %1
%17 = select i1 %16, i64 %15, i64 %1
%18 = tail call i32 @memmove(i8* %0, i8* %10, i64 %17)
%19 = add i64 %17, -1
%20 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %8, %12, %6
%22 = phi i32 [ -1, %6 ], [ 0, %12 ], [ 0, %8 ]
ret i32 %22
}