#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*/ ByteSwap(void *, int, int);
int /*<<< orphan*/ ErrMsg(char *);
int /*<<< orphan*/ free(void *);
int fwrite(void *, int, int, int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ memcpy(void *, void *, int);
int /*<<< orphan*/ restart(int);
int xfwrite_LE(void *p, int size, int num, FILE *fp) {
  int block;
  void *tmp;
  tmp = malloc(size * num);
  if (!tmp) {
    ErrMsg("* malloc error in xfwrite\n");
    restart(1);
  }
  memcpy(tmp, p, size * num);
  ByteSwap(tmp, size, num);
  block = fwrite(tmp, size, num, fp);
  free(tmp);
  return block;
}


x86 assembly (clang, -O0):
<xfwrite_LE>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
imul   -0x10(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b7 <xfwrite_LE+0x47>
lea    0xe58(%rip),%rdi
call   1060 <ErrMsg@plt>
mov    $0x1,%edi
call   1040 <restart@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
imul   -0x10(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1080 <ByteSwap@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1090 <fwrite@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfwrite_LE>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r13
mov    %edx,%ebx
imul   %esi,%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11b1 <xfwrite_LE+0x41>
lea    0xe5e(%rip),%rdi
call   1060 <ErrMsg@plt>
mov    $0x1,%edi
call   1040 <restart@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r15d,%edx
call   1080 <ByteSwap@plt>
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"* malloc error in xfwrite\0A\00"
define i32 @xfwrite_LE(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = mul nsw i32 %11, %12
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %4
%18 = call i32 @ErrMsg(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @restart(i32 1)
br label %20
20: ; preds = %17, %4
%21 = load i8*, i8** %10
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = mul nsw i32 %23, %24
%26 = call i32 @memcpy(i8* %21, i8* %22, i32 %25)
%27 = load i8*, i8** %10
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i32 @ByteSwap(i8* %27, i32 %28, i32 %29)
%31 = load i8*, i8** %10
%32 = load i32, i32* %6
%33 = load i32, i32* %7
%34 = load i32*, i32** %8
%35 = call i32 @fwrite(i8* %31, i32 %32, i32 %33, i32* %34)
store i32 %35, i32* %9
%36 = load i8*, i8** %10
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %9
ret i32 %38
}

LLVM IR (-O2):
@.str = [27 x i8] c"* malloc error in xfwrite\0A\00"
define i32 @xfwrite_LE(i8* %0, i32 %1, i32 %2, i32* nocapture %3) {
%5 = mul nsw i32 %2, %1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = tail call i32 @ErrMsg(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @restart(i32 1)
br label %11
11: ; preds = %8, %4
%12 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %5)
%13 = tail call i32 @ByteSwap(i8* %6, i32 %1, i32 %2)
%14 = tail call i32 @fwrite(i8* %6, i32 %1, i32 %2, i32* %3)
%15 = tail call i32 @free(i8* %6)
ret i32 %14
}