#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 */
char *get_error();
int /*<<< orphan*/ set_error(int /*<<< orphan*/ *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
void crypt_get_error(char *buf, size_t size) {
  const char *error = get_error();
  if (!buf || size < 1)
    set_error(NULL);
  else if (error) {
    strncpy(buf, error, size - 1);
    buf[size - 1] = '\0';
    set_error(NULL);
  } else
    buf[0] = '\0';
}


x86 assembly (clang, -O0):
<crypt_get_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <get_error@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1161 <crypt_get_error+0x31>
cmpq   $0x1,-0x10(%rbp)
jae    116f <crypt_get_error+0x3f>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <set_error@plt>
jmp    11b9 <crypt_get_error+0x89>
cmpq   $0x0,-0x18(%rbp)
je     11ad <crypt_get_error+0x7d>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <set_error@plt>
jmp    11b4 <crypt_get_error+0x84>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b9 <crypt_get_error+0x89>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_get_error>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <get_error@plt>
test   %rbx,%rbx
je     1165 <crypt_get_error+0x35>
test   %r14,%r14
je     1165 <crypt_get_error+0x35>
test   %rax,%rax
je     1173 <crypt_get_error+0x43>
lea    -0x1(%r14),%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <set_error@plt>
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @crypt_get_error(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = call i8* (...) @get_error()
store i8* %6, i8** %5
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %4
%11 = icmp ult i64 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %9, %2
%13 = call i32 @set_error(i32* null)
br label %32
14: ; preds = %9
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %28
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = load i8*, i8** %5
%20 = load i64, i64* %4
%21 = sub i64 %20, 1
%22 = call i32 @strncpy(i8* %18, i8* %19, i64 %21)
%23 = load i8*, i8** %3
%24 = load i64, i64* %4
%25 = sub i64 %24, 1
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = call i32 @set_error(i32* null)
br label %31
28: ; preds = %14
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 0, i8* %30
br label %31
31: ; preds = %28, %17
br label %32
32: ; preds = %31, %12
ret void
}

LLVM IR (-O2):
define void @crypt_get_error(i8* %0, i64 %1) {
%3 = tail call i8* (...) @get_error()
%4 = icmp eq i8* %0, null
%5 = icmp eq i64 %1, 0
%6 = or i1 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @set_error(i32* null)
br label %17
9: ; preds = %2
%10 = icmp eq i8* %3, null
br i1 %10, label %16, label %11
11: ; preds = %9
%12 = add i64 %1, -1
%13 = tail call i32 @strncpy(i8* %0, i8* %3, i64 %12)
%14 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %14
%15 = tail call i32 @set_error(i32* null)
br label %17
16: ; preds = %9
store i8 0, i8* %0
br label %17
17: ; preds = %11, %16, %7
ret void
}