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

/* Variables and functions */
int /*<<< orphan*/ buf_pushchar(struct buffer *, int);
int read_char(int /*<<< orphan*/ *);
unsigned buf_freadchars(struct buffer *buf, FILE *in) {
  unsigned i = 0;
  int ch;
  while ((ch = read_char(in))) {
    if (!buf_pushchar(buf, ch))
      break;
    i++;
  }
  return i;
}


x86 assembly (clang, -O0):
<buf_freadchars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <read_char@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1174 <buf_freadchars+0x54>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <buf_pushchar@plt>
cmp    $0x0,%eax
jne    1166 <buf_freadchars+0x46>
jmp    1174 <buf_freadchars+0x54>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1137 <buf_freadchars+0x17>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_freadchars>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <read_char@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     115d <buf_freadchars+0x3d>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <buf_pushchar@plt>
test   %eax,%eax
je     115d <buf_freadchars+0x3d>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1040 <read_char@plt>
test   %eax,%eax
jne    1140 <buf_freadchars+0x20>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
define i32 @buf_freadchars(%struct.buffer* %0, i32* %1) {
%3 = alloca %struct.buffer*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store %struct.buffer* %0, %struct.buffer** %3
store i32* %1, i32** %4
store i32 0, i32* %5
br label %7
7: ; preds = %17, %2
%8 = load i32*, i32** %4
%9 = call i32 @read_char(i32* %8)
store i32 %9, i32* %6
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %7
%12 = load %struct.buffer*, %struct.buffer** %3
%13 = load i32, i32* %6
%14 = call i32 @buf_pushchar(%struct.buffer* %12, i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %11
br label %20
17: ; preds = %11
%18 = load i32, i32* %5
%19 = add i32 %18, 1
store i32 %19, i32* %5
br label %7
20: ; preds = %16, %7
%21 = load i32, i32* %5
ret i32 %21
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
define i32 @buf_freadchars(%struct.buffer* %0, i32* %1) {
%3 = tail call i32 @read_char(i32* %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %14, label %5
5: ; preds = %2, %10
%6 = phi i32 [ %12, %10 ], [ %3, %2 ]
%7 = phi i32 [ %11, %10 ], [ 0, %2 ]
%8 = tail call i32 @buf_pushchar(%struct.buffer* %0, i32 %6)
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %5
%11 = add i32 %7, 1
%12 = tail call i32 @read_char(i32* %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %5
14: ; preds = %10, %5, %2
%15 = phi i32 [ 0, %2 ], [ %7, %5 ], [ %11, %10 ]
ret i32 %15
}