#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*/ fprintf(int /*<<< orphan*/, char *, char *);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int store_char(char c, char **buf, int *buf_len, int *used) {
  char *new_buf;
  if (*used == *buf_len) {
    *buf_len += 16;
    new_buf = realloc(*buf, *buf_len * sizeof *new_buf);
    if (!new_buf) {
      fprintf(stderr, "Memory allocation error (%s).\n", strerror(errno));
      return -1;
    }
    *buf = new_buf;
  }
  (*buf)[*used] = c;
  ++(*used);
  return 0;
}


x86 assembly (clang, -O0):
<store_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jne    11d2 <store_char+0xa2>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x10,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c7 <store_char+0x97>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe4a(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <store_char+0xc8>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x5(%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)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<store_char>:
push   %rbp
push   %r14
push   %rbx
mov    %rcx,%rbx
mov    %rsi,%rbp
mov    %edi,%r14d
mov    (%rcx),%esi
cmp    (%rdx),%esi
jne    115e <store_char+0x2e>
add    $0x10,%esi
mov    %esi,(%rdx)
mov    0x0(%rbp),%rdi
call   1040 <realloc@plt>
test   %rax,%rax
je     1170 <store_char+0x40>
mov    %rax,0x0(%rbp)
mov    (%rbx),%esi
jmp    1162 <store_char+0x32>
mov    0x0(%rbp),%rax
movslq %esi,%rcx
mov    %r14b,(%rax,%rcx,1)
addl   $0x1,(%rbx)
xor    %eax,%eax
jmp    119d <store_char+0x6d>
mov    0x2e69(%rip),%rax
mov    (%rax),%ebx
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe72(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Memory allocation error (%s).\0A\00"
@errno = global i32 0
define i32 @store_char(i8 signext %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
store i8 %0, i8* %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%11 = load i32*, i32** %9
%12 = load i32, i32* %11
%13 = load i32*, i32** %8
%14 = load i32, i32* %13
%15 = icmp eq i32 %12, %14
br i1 %15, label %16, label %38
16: ; preds = %4
%17 = load i32*, i32** %8
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 16
store i32 %19, i32* %17
%20 = load i8**, i8*** %7
%21 = load i8*, i8** %20
%22 = load i32*, i32** %8
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = mul i64 %24, 1
%26 = trunc i64 %25 to i32
%27 = call i8* @realloc(i8* %21, i32 %26)
store i8* %27, i8** %10
%28 = load i8*, i8** %10
%29 = icmp ne i8* %28, null
br i1 %29, label %35, label %30
30: ; preds = %16
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %33)
store i32 -1, i32* %5
br label %49
35: ; preds = %16
%36 = load i8*, i8** %10
%37 = load i8**, i8*** %7
store i8* %36, i8** %37
br label %38
38: ; preds = %35, %4
%39 = load i8, i8* %6
%40 = load i8**, i8*** %7
%41 = load i8*, i8** %40
%42 = load i32*, i32** %9
%43 = load i32, i32* %42
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
store i8 %39, i8* %45
%46 = load i32*, i32** %9
%47 = load i32, i32* %46
%48 = add nsw i32 %47, 1
store i32 %48, i32* %46
store i32 0, i32* %5
br label %49
49: ; preds = %38, %30
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Memory allocation error (%s).\0A\00"
@errno = global i32 0
define i32 @store_char(i8 signext %0, i8** nocapture %1, i32* nocapture %2, i32* nocapture %3) {
%5 = load i32, i32* %3
%6 = load i32, i32* %2
%7 = icmp eq i32 %5, %6
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = load i8*, i8** %1
br label %22
10: ; preds = %4
%11 = add nsw i32 %5, 16
store i32 %11, i32* %2
%12 = load i8*, i8** %1
%13 = tail call i8* @realloc(i8* %12, i32 %11)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %29
20: ; preds = %10
store i8* %13, i8** %1
%21 = load i32, i32* %3
br label %22
22: ; preds = %8, %20
%23 = phi i32 [ %5, %8 ], [ %21, %20 ]
%24 = phi i8* [ %9, %8 ], [ %13, %20 ]
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 %0, i8* %26
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %29
29: ; preds = %22, %15
%30 = phi i32 [ 0, %22 ], [ -1, %15 ]
ret i32 %30
}