#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 */
scalar_t__ realloc_buf(unsigned char **, int *, int);
int char2buffer(unsigned char **buf, int *buf_len, int *pos, char data) {
  if (*buf_len < 1) {
    if (realloc_buf(buf, buf_len, 1) != 0) {
      return -1;
    }
  }
  (*buf)[*pos] = data;
  ++(*pos);
  --(*buf_len);
  return 0;
}


x86 assembly (clang, -O0):
<char2buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %al,-0x21(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1163 <char2buffer+0x53>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <realloc_buf@plt>
cmp    $0x0,%rax
je     115e <char2buffer+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <char2buffer+0x84>
jmp    1163 <char2buffer+0x53>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
movslq (%rcx),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<char2buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
cmpl   $0x0,(%rsi)
jg     1145 <char2buffer+0x35>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <realloc_buf@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1159 <char2buffer+0x49>
mov    0x0(%rbp),%rax
movslq (%r14),%rcx
mov    %r15b,(%rax,%rcx,1)
addl   $0x1,(%r14)
addl   $0xffffffff,(%rbx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @char2buffer(i8** %0, i32* %1, i32* %2, i8 signext %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8
store i8** %0, i8*** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i8 %3, i8* %9
%10 = load i32*, i32** %7
%11 = load i32, i32* %10
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %20
13: ; preds = %4
%14 = load i8**, i8*** %6
%15 = load i32*, i32** %7
%16 = call i64 @realloc_buf(i8** %14, i32* %15, i32 1)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %5
br label %34
19: ; preds = %13
br label %20
20: ; preds = %19, %4
%21 = load i8, i8* %9
%22 = load i8**, i8*** %6
%23 = load i8*, i8** %22
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %21, i8* %27
%28 = load i32*, i32** %8
%29 = load i32, i32* %28
%30 = add nsw i32 %29, 1
store i32 %30, i32* %28
%31 = load i32*, i32** %7
%32 = load i32, i32* %31
%33 = add nsw i32 %32, -1
store i32 %33, i32* %31
store i32 0, i32* %5
br label %34
34: ; preds = %20, %18
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
define i32 @char2buffer(i8** %0, i32* %1, i32* nocapture %2, i8 signext %3) {
%5 = load i32, i32* %1
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i64 @realloc_buf(i8** %0, i32* %1, i32 1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %7, %4
%11 = load i8*, i8** %0
%12 = load i32, i32* %2
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
store i8 %3, i8* %14
%15 = load i32, i32* %2
%16 = add nsw i32 %15, 1
store i32 %16, i32* %2
%17 = load i32, i32* %1
%18 = add nsw i32 %17, -1
store i32 %18, i32* %1
br label %19
19: ; preds = %7, %10
%20 = phi i32 [ 0, %10 ], [ -1, %7 ]
ret i32 %20
}