#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 fifo_buffer {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ align(size_t);
int /*<<< orphan*/ assert(int);
size_t fifo_buffer_available(struct fifo_buffer *);
size_t fifo_buffer_capacity(struct fifo_buffer *);
struct fifo_buffer *fifo_buffer_realloc(struct fifo_buffer *,
                                        int /*<<< orphan*/);
void *fifo_buffer_write(struct fifo_buffer *, size_t *);
void *growing_fifo_write(struct fifo_buffer **buffer_p, size_t length) {
  assert(buffer_p != NULL);
  struct fifo_buffer *buffer = *buffer_p;
  assert(buffer != NULL);
  size_t max_length;
  void *p = fifo_buffer_write(buffer, &max_length);
  if (p != NULL && max_length >= length)
    return p;
  size_t new_size = fifo_buffer_available(buffer) + length;
  assert(new_size > fifo_buffer_capacity(buffer));
  *buffer_p = buffer = fifo_buffer_realloc(buffer, align(new_size));
  p = fifo_buffer_write(buffer, &max_length);
  assert(p != NULL);
  assert(max_length >= length);
  return p;
}


x86 assembly (clang, -O0):
<growing_fifo_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <fifo_buffer_write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d6 <growing_fifo_write+0x76>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11d6 <growing_fifo_write+0x76>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1279 <growing_fifo_write+0x119>
mov    -0x20(%rbp),%rdi
call   1030 <fifo_buffer_available@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <fifo_buffer_capacity@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <align@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
call   1050 <fifo_buffer_realloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <fifo_buffer_write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<growing_fifo_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1070 <assert@plt>
mov    (%r15),%r12
xor    %edi,%edi
test   %r12,%r12
setne  %dil
call   1070 <assert@plt>
mov    %rsp,%rsi
mov    %r12,%rdi
call   1040 <fifo_buffer_write@plt>
test   %rax,%rax
je     11a6 <growing_fifo_write+0x46>
mov    %rax,%rbx
cmp    %r14,(%rsp)
jae    1208 <growing_fifo_write+0xa8>
mov    %r12,%rdi
call   1030 <fifo_buffer_available@plt>
lea    (%rax,%r14,1),%rbx
mov    %r12,%rdi
call   1080 <fifo_buffer_capacity@plt>
xor    %edi,%edi
cmp    %rax,%rbx
seta   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1060 <align@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <fifo_buffer_realloc@plt>
mov    %rax,(%r15)
mov    %rsp,%rsi
mov    %rax,%rdi
call   1040 <fifo_buffer_write@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
cmp    %r14,(%rsp)
setae  %dil
call   1070 <assert@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.fifo_buffer = type { i32 }
define i8* @growing_fifo_write(%struct.fifo_buffer** %0, i64 %1) {
%3 = alloca i8*
%4 = alloca %struct.fifo_buffer**
%5 = alloca i64
%6 = alloca %struct.fifo_buffer*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store %struct.fifo_buffer** %0, %struct.fifo_buffer*** %4
store i64 %1, i64* %5
%10 = load %struct.fifo_buffer**, %struct.fifo_buffer*** %4
%11 = icmp ne %struct.fifo_buffer** %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load %struct.fifo_buffer**, %struct.fifo_buffer*** %4
%15 = load %struct.fifo_buffer*, %struct.fifo_buffer** %14
store %struct.fifo_buffer* %15, %struct.fifo_buffer** %6
%16 = load %struct.fifo_buffer*, %struct.fifo_buffer** %6
%17 = icmp ne %struct.fifo_buffer* %16, null
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load %struct.fifo_buffer*, %struct.fifo_buffer** %6
%21 = call i8* @fifo_buffer_write(%struct.fifo_buffer* %20, i64* %7)
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i64, i64* %7
%26 = load i64, i64* %5
%27 = icmp uge i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i8*, i8** %8
store i8* %29, i8** %3
br label %58
30: ; preds = %24, %2
%31 = load %struct.fifo_buffer*, %struct.fifo_buffer** %6
%32 = call i64 @fifo_buffer_available(%struct.fifo_buffer* %31)
%33 = load i64, i64* %5
%34 = add i64 %32, %33
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = load %struct.fifo_buffer*, %struct.fifo_buffer** %6
%37 = call i64 @fifo_buffer_capacity(%struct.fifo_buffer* %36)
%38 = icmp ugt i64 %35, %37
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load %struct.fifo_buffer*, %struct.fifo_buffer** %6
%42 = load i64, i64* %9
%43 = call i32 @align(i64 %42)
%44 = call %struct.fifo_buffer* @fifo_buffer_realloc(%struct.fifo_buffer* %41, i32 %43)
store %struct.fifo_buffer* %44, %struct.fifo_buffer** %6
%45 = load %struct.fifo_buffer**, %struct.fifo_buffer*** %4
store %struct.fifo_buffer* %44, %struct.fifo_buffer** %45
%46 = load %struct.fifo_buffer*, %struct.fifo_buffer** %6
%47 = call i8* @fifo_buffer_write(%struct.fifo_buffer* %46, i64* %7)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
%49 = icmp ne i8* %48, null
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i64, i64* %7
%53 = load i64, i64* %5
%54 = icmp uge i64 %52, %53
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i8*, i8** %8
store i8* %57, i8** %3
br label %58
58: ; preds = %30, %28
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
%struct.fifo_buffer = type { i32 }
define i8* @growing_fifo_write(%struct.fifo_buffer** %0, i64 %1) {
%3 = alloca i64
%4 = icmp ne %struct.fifo_buffer** %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = load %struct.fifo_buffer*, %struct.fifo_buffer** %0
%8 = icmp ne %struct.fifo_buffer* %7, null
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = bitcast i64* %3 to i8*
%12 = call i8* @fifo_buffer_write(%struct.fifo_buffer* %7, i64* %3)
%13 = icmp eq i8* %12, null
%14 = load i64, i64* %3
%15 = icmp ult i64 %14, %1
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %17, label %34
17: ; preds = %2
%18 = call i64 @fifo_buffer_available(%struct.fifo_buffer* %7)
%19 = add i64 %18, %1
%20 = call i64 @fifo_buffer_capacity(%struct.fifo_buffer* %7)
%21 = icmp ugt i64 %19, %20
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = call i32 @align(i64 %19)
%25 = call %struct.fifo_buffer* @fifo_buffer_realloc(%struct.fifo_buffer* %7, i32 %24)
store %struct.fifo_buffer* %25, %struct.fifo_buffer** %0
%26 = call i8* @fifo_buffer_write(%struct.fifo_buffer* %25, i64* %3)
%27 = icmp ne i8* %26, null
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i64, i64* %3
%31 = icmp uge i64 %30, %1
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
br label %34
34: ; preds = %2, %17
%35 = phi i8* [ %26, %17 ], [ %12, %2 ]
ret i8* %35
}