#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 fwrite(void const *, size_t, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
void nonblock_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  int nwritten;
  while (nmemb > 0) {
    nwritten = fwrite(ptr, size, nmemb, stream);
    nmemb -= nwritten;
    printf("nwritten: %d\n", nwritten);
  }
}


x86 assembly (clang, -O0):
<nonblock_fwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    117e <nonblock_fwrite+0x5e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <fwrite@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe8c(%rip),%rdi
call   1030 <printf@plt>
jmp    1138 <nonblock_fwrite+0x18>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nonblock_fwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1173 <nonblock_fwrite+0x53>
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xebd(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <fwrite@plt>
movslq %eax,%rbp
mov    %r13,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
sub    %rbp,%rbx
jne    1150 <nonblock_fwrite+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"nwritten: %d\0A\00"
define void @nonblock_fwrite(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
br label %10
10: ; preds = %13, %4
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %10
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = load i64, i64* %7
%17 = load i32*, i32** %8
%18 = call i32 @fwrite(i8* %14, i64 %15, i64 %16, i32* %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = load i64, i64* %7
%22 = sub i64 %21, %20
store i64 %22, i64* %7
%23 = load i32, i32* %9
%24 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %10
25: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"nwritten: %d\0A\00"
define void @nonblock_fwrite(i8* nocapture %0, i64 %1, i64 %2, i32* nocapture %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %13, label %6
6: ; preds = %4, %6
%7 = phi i64 [ %10, %6 ], [ %2, %4 ]
%8 = tail call i32 @fwrite(i8* %0, i64 %1, i64 %7, i32* %3)
%9 = sext i32 %8 to i64
%10 = sub i64 %7, %9
%11 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %6
13: ; preds = %6, %4
ret void
}