#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 scalar_t__ uint8_t;
struct fifo_t {
  int dummy;
};

/* Variables and functions */
scalar_t__ fifo_write_one(struct fifo_t *const, scalar_t__ const);
uint8_t fifo_write(struct fifo_t *const fifo, const uint8_t *buffer,
                   uint8_t sz) {
  uint8_t count = 0;
  while (sz && fifo_write_one(fifo, *buffer)) {
    buffer++;
    sz--;
    count++;
  }
  return count;
}


x86 assembly (clang, -O0):
<fifo_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
je     1156 <fifo_write+0x46>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1030 <fifo_write_one@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1166 <fifo_write+0x56>
jmp    118f <fifo_write+0x7f>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112c <fifo_write+0x1c>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fifo_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     114f <fifo_write+0x3f>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,8),%rsi
mov    %r12,%rdi
call   1030 <fifo_write_one@plt>
test   %rax,%rax
je     1151 <fifo_write+0x41>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1130 <fifo_write+0x20>
mov    %r14,%rbx
jmp    1151 <fifo_write+0x41>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.fifo_t = type { i32 }
define i64 @fifo_write(%struct.fifo_t* %0, i64* %1, i64 %2) {
%4 = alloca %struct.fifo_t*
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i64
store %struct.fifo_t* %0, %struct.fifo_t** %4
store i64* %1, i64** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %8
8: ; preds = %19, %3
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load %struct.fifo_t*, %struct.fifo_t** %4
%13 = load i64*, i64** %5
%14 = load i64, i64* %13
%15 = call i64 @fifo_write_one(%struct.fifo_t* %12, i64 %14)
%16 = icmp ne i64 %15, 0
br label %17
17: ; preds = %11, %8
%18 = phi i1 [ false, %8 ], [ %16, %11 ]
br i1 %18, label %19, label %26
19: ; preds = %17
%20 = load i64*, i64** %5
%21 = getelementptr inbounds i64, i64* %20, i32 1
store i64* %21, i64** %5
%22 = load i64, i64* %6
%23 = add nsw i64 %22, -1
store i64 %23, i64* %6
%24 = load i64, i64* %7
%25 = add nsw i64 %24, 1
store i64 %25, i64* %7
br label %8
26: ; preds = %17
%27 = load i64, i64* %7
ret i64 %27
}

LLVM IR (-O2):
%struct.fifo_t = type { i32 }
define i64 @fifo_write(%struct.fifo_t* %0, i64* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %12
%6 = phi i64 [ %15, %12 ], [ 0, %3 ]
%7 = phi i64 [ %14, %12 ], [ %2, %3 ]
%8 = phi i64* [ %13, %12 ], [ %1, %3 ]
%9 = load i64, i64* %8
%10 = tail call i64 @fifo_write_one(%struct.fifo_t* %0, i64 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %5
%13 = getelementptr inbounds i64, i64* %8, i64 1
%14 = add nsw i64 %7, -1
%15 = add nuw nsw i64 %6, 1
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %5
17: ; preds = %5, %12, %3
%18 = phi i64 [ 0, %3 ], [ %2, %12 ], [ %6, %5 ]
ret i64 %18
}