#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char const *, int const);
scalar_t__ strlen(char const *);
void buffer_output(FILE *fout, const char *str, const int len, const int fd_i) {
  static char buf[4 + 1][131072];
  static int count[4 + 1] = {0};
  if (count[fd_i] + len >= 131072 || str == NULL || (int)(strlen(str)) != len ||
      len == 0) {
    fwrite(buf[fd_i], 1, count[fd_i], fout);
    count[fd_i] = 0;
  }
  memcpy(buf[fd_i] + count[fd_i], str, len);
  count[fd_i] += len;
}


x86 assembly (clang, -O0):
<buffer_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movslq -0x18(%rbp),%rcx
lea    0xa2eff(%rip),%rax
mov    (%rax,%rcx,4),%eax
add    -0x14(%rbp),%eax
cmp    $0x20000,%eax
jge    1189 <buffer_output+0x59>
cmpq   $0x0,-0x10(%rbp)
je     1189 <buffer_output+0x59>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    -0x14(%rbp),%eax
jne    1189 <buffer_output+0x59>
cmpl   $0x0,-0x14(%rbp)
jne    11c9 <buffer_output+0x99>
movslq -0x18(%rbp),%rax
lea    0x2ebc(%rip),%rdi
shl    $0x11,%rax
add    %rax,%rdi
movslq -0x18(%rbp),%rcx
lea    0xa2eaa(%rip),%rax
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
movslq -0x18(%rbp),%rcx
lea    0xa2e8e(%rip),%rax
movl   $0x0,(%rax,%rcx,4)
movslq -0x18(%rbp),%rax
lea    0x2e7c(%rip),%rdi
shl    $0x11,%rax
add    %rax,%rdi
movslq -0x18(%rbp),%rcx
lea    0xa2e6a(%rip),%rax
movslq (%rax,%rcx,4),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x14(%rbp),%edx
movslq -0x18(%rbp),%rcx
lea    0xa2e49(%rip),%rax
add    (%rax,%rcx,4),%edx
lea    0xa2e3f(%rip),%rax
mov    %edx,(%rax,%rcx,4)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_output>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
movslq %ecx,%r13
lea    0xa2f02(%rip),%rbp
mov    0x0(%rbp,%r13,4),%ebx
test   %rsi,%rsi
je     1175 <buffer_output+0x45>
lea    (%rbx,%r15,1),%eax
cmp    $0x1ffff,%eax
jg     1175 <buffer_output+0x45>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %r15d,%r15d
je     1175 <buffer_output+0x45>
cmp    %r15d,%eax
je     11a0 <buffer_output+0x70>
mov    %r13,%rax
shl    $0x11,%rax
lea    0x2ecd(%rip),%rdi
add    %rax,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r12,%rcx
call   1050 <fwrite@plt>
movl   $0x0,0x0(%rbp,%r13,4)
xor    %ebx,%ebx
movslq %ebx,%rax
mov    %r13,%rcx
shl    $0x11,%rcx
lea    0x2e9f(%rip),%rdi
add    %rcx,%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
add    %r15d,0x0(%rbp,%r13,4)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@buffer_output.buf = internal global [5 x [131072 x i8]] zeroinitializer
@buffer_output.count = internal global [5 x i32] zeroinitializer
define void @buffer_output(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%9 = load i32, i32* %8
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [5 x i32], [5 x i32]* @buffer_output.count, i64 0, i64 %10
%12 = load i32, i32* %11
%13 = load i32, i32* %7
%14 = add nsw i32 %12, %13
%15 = icmp sge i32 %14, 131072
br i1 %15, label %28, label %16
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %28, label %19
19: ; preds = %16
%20 = load i8*, i8** %6
%21 = call i64 @strlen(i8* %20)
%22 = trunc i64 %21 to i32
%23 = load i32, i32* %7
%24 = icmp ne i32 %22, %23
br i1 %24, label %28, label %25
25: ; preds = %19
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %25, %19, %16, %4
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [5 x [131072 x i8]], [5 x [131072 x i8]]* @buffer_output.buf, i64 0, i64 %30
%32 = getelementptr inbounds [131072 x i8], [131072 x i8]* %31, i64 0, i64 0
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [5 x i32], [5 x i32]* @buffer_output.count, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = load i32*, i32** %5
%38 = call i32 @fwrite(i8* %32, i32 1, i32 %36, i32* %37)
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [5 x i32], [5 x i32]* @buffer_output.count, i64 0, i64 %40
store i32 0, i32* %41
br label %42
42: ; preds = %28, %25
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [5 x [131072 x i8]], [5 x [131072 x i8]]* @buffer_output.buf, i64 0, i64 %44
%46 = getelementptr inbounds [131072 x i8], [131072 x i8]* %45, i64 0, i64 0
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [5 x i32], [5 x i32]* @buffer_output.count, i64 0, i64 %48
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %46, i64 %51
%53 = load i8*, i8** %6
%54 = load i32, i32* %7
%55 = call i32 @memcpy(i8* %52, i8* %53, i32 %54)
%56 = load i32, i32* %7
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [5 x i32], [5 x i32]* @buffer_output.count, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = add nsw i32 %60, %56
store i32 %61, i32* %59
ret void
}

LLVM IR (-O2):
@buffer_output.buf = internal global [5 x [131072 x i8]] zeroinitializer
@buffer_output.count = internal unnamed_addr global [5 x i32] zeroinitializer
define void @buffer_output(i32* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = sext i32 %3 to i64
%6 = getelementptr inbounds [5 x i32], [5 x i32]* @buffer_output.count, i64 0, i64 %5
%7 = load i32, i32* %6
%8 = add nsw i32 %7, %2
%9 = icmp sgt i32 %8, 131071
%10 = icmp eq i8* %1, null
%11 = or i1 %10, %9
br i1 %11, label %18, label %12
12: ; preds = %4
%13 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%14 = trunc i64 %13 to i32
%15 = icmp ne i32 %14, %2
%16 = icmp eq i32 %2, 0
%17 = or i1 %16, %15
br i1 %17, label %18, label %21
18: ; preds = %12, %4
%19 = getelementptr inbounds [5 x [131072 x i8]], [5 x [131072 x i8]]* @buffer_output.buf, i64 0, i64 %5, i64 0
%20 = tail call i32 @fwrite(i8* %19, i32 1, i32 %7, i32* %0)
store i32 0, i32* %6
br label %21
21: ; preds = %12, %18
%22 = phi i32 [ %7, %12 ], [ 0, %18 ]
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [5 x [131072 x i8]], [5 x [131072 x i8]]* @buffer_output.buf, i64 0, i64 %5, i64 %23
%25 = tail call i32 @memcpy(i8* %24, i8* %1, i32 %2)
%26 = load i32, i32* %6
%27 = add nsw i32 %26, %2
store i32 %27, i32* %6
ret void
}