#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*/ errno;
int /*<<< orphan*/ strerror_r(int /*<<< orphan*/, char *, int);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, int);
void set_error_string(char *error_string_buffer, int error_string_maxlen,
                      const char *error_string) {
  if (NULL != error_string_buffer && error_string_maxlen > 0) {
    strerror_r(errno, error_string_buffer, error_string_maxlen);
    strncat(error_string_buffer, ", ",
            error_string_maxlen - strlen(error_string_buffer) - 1);
    strncat(error_string_buffer, error_string,
            error_string_maxlen - strlen(error_string_buffer) - 1);
    error_string_buffer[error_string_maxlen - 1] = '\0';
  }
}


x86 assembly (clang, -O0):
<set_error_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     11e2 <set_error_string+0xb2>
cmpl   $0x0,-0xc(%rbp)
jle    11e2 <set_error_string+0xb2>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <strerror_r@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rdi
sub    %eax,%edx
sub    $0x1,%edx
lea    0xe68(%rip),%rsi
call   1040 <strncat@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    %eax,%edx
sub    $0x1,%edx
call   1040 <strncat@plt>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_error_string>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     119a <set_error_string+0x6a>
mov    %esi,%r14d
test   %esi,%esi
jle    119a <set_error_string+0x6a>
mov    %rdx,%r15
mov    %rdi,%rbx
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <strerror_r@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
not    %eax
lea    (%rax,%r14,1),%edx
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strncat@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
not    %eax
lea    (%rax,%r14,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strncat@plt>
add    $0xffffffff,%r14d
movb   $0x0,(%rbx,%r14,1)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [3 x i8] c", \00"
define void @set_error_string(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%7 = load i8*, i8** %4
%8 = icmp ne i8* null, %7
br i1 %8, label %9, label %37
9: ; preds = %3
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %37
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = call i32 @strerror_r(i32 %13, i8* %14, i32 %15)
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %18, %20
%22 = sub nsw i32 %21, 1
%23 = call i32 @strncat(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %4
%25 = load i8*, i8** %6
%26 = load i32, i32* %5
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %26, %28
%30 = sub nsw i32 %29, 1
%31 = call i32 @strncat(i8* %24, i8* %25, i32 %30)
%32 = load i8*, i8** %4
%33 = load i32, i32* %5
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %12, %9, %3
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [3 x i8] c", \00"
define void @set_error_string(i8* %0, i32 %1, i8* %2) {
%4 = icmp ne i8* %0, null
%5 = icmp sgt i32 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %21
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror_r(i32 %8, i8* %0, i32 %1)
%10 = tail call i32 @strlen(i8* %0)
%11 = xor i32 %10, -1
%12 = add i32 %11, %1
%13 = tail call i32 @strncat(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = tail call i32 @strlen(i8* %0)
%15 = xor i32 %14, -1
%16 = add i32 %15, %1
%17 = tail call i32 @strncat(i8* %0, i8* %2, i32 %16)
%18 = add nsw i32 %1, -1
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %7, %3
ret void
}