#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 */
unsigned char fputc(unsigned char, int /*<<< orphan*/ *);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  unsigned char *buffer = (unsigned char *)ptr;
  int i, j;
  if (size == 0)
    return 0;
  if (nmemb == 0)
    return 0;
  for (i = 0; i < nmemb; i++) {
    for (j = 0; j < size; j++) {
      if (fputc(*buffer, stream) != *buffer)
        return i;
      buffer++;
    }
  }
  return nmemb;
}


x86 assembly (clang, -O0):
<fwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1148 <fwrite+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <fwrite+0xe0>
cmpq   $0x0,-0x20(%rbp)
jne    1160 <fwrite+0x50>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <fwrite+0xe0>
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11e8 <fwrite+0xd8>
movl   $0x0,-0x38(%rbp)
movslq -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d5 <fwrite+0xc5>
mov    -0x30(%rbp),%rax
mov    (%rax),%al
mov    -0x28(%rbp),%rsi
movzbl %al,%edi
call   1030 <fputc@plt>
movzbl %al,%eax
mov    -0x30(%rbp),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
je     11bb <fwrite+0xab>
movslq -0x34(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f0 <fwrite+0xe0>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    117c <fwrite+0x6c>
jmp    11da <fwrite+0xca>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1167 <fwrite+0x57>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %rsi,%rsi
je     116a <fwrite+0x5a>
mov    %rdx,%r14
test   %rdx,%rdx
je     116a <fwrite+0x5a>
mov    %rcx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %r13d,%r13d
mov    %r15,%rbp
nopl   0x0(%rax)
movzbl (%rbx),%edi
mov    %r12,%rsi
call   1030 <fputc@plt>
cmp    (%rbx),%al
jne    1167 <fwrite+0x57>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    1140 <fwrite+0x30>
add    $0x1,%r13
cmp    %r14,%r13
jne    1136 <fwrite+0x26>
mov    %r14,%rax
jmp    116a <fwrite+0x5a>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @fwrite(i8* nocapture %0, i64 %1, i64 %2, i32* nocapture %3) {
%5 = icmp eq i64 %1, 0
%6 = icmp eq i64 %2, 0
%7 = or i1 %5, %6
br i1 %7, label %27, label %8
8: ; preds = %4, %22
%9 = phi i64 [ %23, %22 ], [ 0, %4 ]
%10 = phi i8* [ %19, %22 ], [ %0, %4 ]
br label %11
11: ; preds = %8, %18
%12 = phi i64 [ 0, %8 ], [ %20, %18 ]
%13 = phi i8* [ %10, %8 ], [ %19, %18 ]
%14 = load i8, i8* %13
%15 = tail call zeroext i8 @fputc(i8 zeroext %14, i32* %3)
%16 = load i8, i8* %13
%17 = icmp eq i8 %15, %16
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = getelementptr inbounds i8, i8* %13, i64 1
%20 = add nuw i64 %12, 1
%21 = icmp eq i64 %20, %1
br i1 %21, label %22, label %11
22: ; preds = %18
%23 = add nuw i64 %9, 1
%24 = icmp eq i64 %23, %2
br i1 %24, label %27, label %8
25: ; preds = %11
%26 = and i64 %9, 4294967295
br label %27
27: ; preds = %22, %25, %4
%28 = phi i64 [ 0, %4 ], [ %26, %25 ], [ %2, %22 ]
ret i64 %28
}