#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 */
struct c_buffer {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ c_buffer_add(struct c_buffer *, char *, int);
char *c_buffer_extract(struct c_buffer *, size_t *);
int /*<<< orphan*/ c_buffer_remove(struct c_buffer *, int);
char *c_buffer_extract_string(struct c_buffer *buf, size_t *plen) {
  char *string;
  size_t len;
  c_buffer_add(buf, "\0", 1);
  string = c_buffer_extract(buf, &len);
  if (!string) {
    c_buffer_remove(buf, 1);
    return NULL;
  }
  if (plen)
    *plen = len - 1;
  return string;
}


x86 assembly (clang, -O0):
<c_buffer_extract_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
mov    $0x1,%edx
call   1040 <c_buffer_add@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <c_buffer_extract@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118c <c_buffer_extract_string+0x5c>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <c_buffer_remove@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ae <c_buffer_extract_string+0x7e>
cmpq   $0x0,-0x18(%rbp)
je     11a6 <c_buffer_extract_string+0x76>
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_buffer_extract_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeba(%rip),%rsi
mov    $0x1,%edx
call   1040 <c_buffer_add@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <c_buffer_extract@plt>
mov    %rax,%r15
test   %rax,%rax
je     1178 <c_buffer_extract_string+0x48>
test   %r14,%r14
je     1185 <c_buffer_extract_string+0x55>
mov    0x8(%rsp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,(%r14)
jmp    1185 <c_buffer_extract_string+0x55>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <c_buffer_remove@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.c_buffer = type { i32 }
@.str = [2 x i8] zeroinitializer
define i8* @c_buffer_extract_string(%struct.c_buffer* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca %struct.c_buffer*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
store %struct.c_buffer* %0, %struct.c_buffer** %4
store i64* %1, i64** %5
%8 = load %struct.c_buffer*, %struct.c_buffer** %4
%9 = call i32 @c_buffer_add(%struct.c_buffer* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%10 = load %struct.c_buffer*, %struct.c_buffer** %4
%11 = call i8* @c_buffer_extract(%struct.c_buffer* %10, i64* %7)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load %struct.c_buffer*, %struct.c_buffer** %4
%16 = call i32 @c_buffer_remove(%struct.c_buffer* %15, i32 1)
store i8* null, i8** %3
br label %26
17: ; preds = %2
%18 = load i64*, i64** %5
%19 = icmp ne i64* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i64, i64* %7
%22 = sub i64 %21, 1
%23 = load i64*, i64** %5
store i64 %22, i64* %23
br label %24
24: ; preds = %20, %17
%25 = load i8*, i8** %6
store i8* %25, i8** %3
br label %26
26: ; preds = %24, %14
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
%struct.c_buffer = type { i32 }
@.str = [2 x i8] zeroinitializer
define i8* @c_buffer_extract_string(%struct.c_buffer* %0, i64* writeonly %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i32 @c_buffer_add(%struct.c_buffer* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%6 = call i8* @c_buffer_extract(%struct.c_buffer* %0, i64* %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @c_buffer_remove(%struct.c_buffer* %0, i32 1)
br label %15
10: ; preds = %2
%11 = icmp eq i64* %1, null
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = load i64, i64* %3
%14 = add i64 %13, -1
store i64 %14, i64* %1
br label %15
15: ; preds = %10, %12, %8
ret i8* %6
}