#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int const fwrite(unsigned char *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ p3dGetElapsedTime_min();
int /*<<< orphan*/ p3dGetElapsedTime_sec();
int /*<<< orphan*/ p3dResetStartTime();
int p3dWriteRaw8(unsigned char *in_im, char *filename, const int dimx,
                 const int dimy, const int dimz,
                 int (*wr_log)(const char *, ...),
                 int (*wr_progress)(const int, ...)) {
  FILE *fvol;
  if (wr_log != NULL) {
    p3dResetStartTime();
    wr_log("Pore3D - Writing 8-bit RAW file %s ...", filename);
  }
  if ((fvol = fopen(filename, "wb")) == NULL) {
    wr_log("Pore3D - IO error: cannot open output file %s. Program will exit.",
           filename);
    return 1;
  }
  if (fwrite(in_im, sizeof(unsigned char), dimx * dimy * dimz, fvol) <
      (dimx * dimy * dimz)) {
    wr_log("Pore3D - IO error: error on writing file %s. Program will exit.",
           filename);
    return 1;
  }
  fclose(fvol);
  if (wr_log != NULL) {
    wr_log("Pore3D - RAW file written successfully in %dm%0.3fs.",
           p3dGetElapsedTime_min(), p3dGetElapsedTime_sec());
  }
  return 2;
}


x86 assembly (clang, -O0):
<p3dWriteRaw8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11a7 <p3dWriteRaw8+0x47>
mov    $0x0,%al
call   1060 <p3dResetStartTime@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   *%rcx
mov    -0x18(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11e4 <p3dWriteRaw8+0x84>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   *%rcx
movl   $0x1,-0x4(%rbp)
jmp    127b <p3dWriteRaw8+0x11b>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
imul   -0x20(%rbp),%edx
imul   -0x24(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x1c(%rbp),%ecx
imul   -0x20(%rbp),%ecx
imul   -0x24(%rbp),%ecx
cmp    %ecx,%eax
jge    1233 <p3dWriteRaw8+0xd3>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   *%rcx
movl   $0x1,-0x4(%rbp)
jmp    127b <p3dWriteRaw8+0x11b>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
je     1274 <p3dWriteRaw8+0x114>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1050 <p3dGetElapsedTime_min@plt>
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1040 <p3dGetElapsedTime_sec@plt>
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rcx
mov    %eax,%edx
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   *%rcx
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p3dWriteRaw8>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r14
mov    %r8d,%r15d
mov    %ecx,%ebp
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,(%rsp)
test   %r9,%r9
je     1198 <p3dWriteRaw8+0x38>
xor    %eax,%eax
call   1060 <p3dResetStartTime@plt>
lea    0xe70(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   *%r14
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e5 <p3dWriteRaw8+0x85>
mov    %rax,%r12
imul   %r13d,%ebp
imul   %r15d,%ebp
mov    $0x1,%r15d
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
cmp    %ebp,%eax
jge    11fc <p3dWriteRaw8+0x9c>
lea    0xe91(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   *%r14
jmp    122f <p3dWriteRaw8+0xcf>
lea    0xe3e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   *%r14
mov    $0x1,%r15d
jmp    122f <p3dWriteRaw8+0xcf>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    $0x2,%r15d
test   %r14,%r14
je     122f <p3dWriteRaw8+0xcf>
xor    %eax,%eax
call   1050 <p3dGetElapsedTime_min@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <p3dGetElapsedTime_sec@plt>
lea    0xe86(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   *%r14
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"Pore3D - Writing 8-bit RAW file %s ...\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [66 x i8] c"Pore3D - IO error: cannot open output file %s. Program will exit.\00"
@.str.3 = [64 x i8] c"Pore3D - IO error: error on writing file %s. Program will exit.\00"
@.str.4 = [53 x i8] c"Pore3D - RAW file written successfully in %dm%0.3fs.\00"
define i32 @p3dWriteRaw8(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 (i8*, ...)* %5, i32 (i32, ...)* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32 (i8*, ...)*
%15 = alloca i32 (i32, ...)*
%16 = alloca i32*
store i8* %0, i8** %9
store i8* %1, i8** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store i32 (i8*, ...)* %5, i32 (i8*, ...)** %14
store i32 (i32, ...)* %6, i32 (i32, ...)** %15
%17 = load i32 (i8*, ...)*, i32 (i8*, ...)** %14
%18 = icmp ne i32 (i8*, ...)* %17, null
br i1 %18, label %19, label %24
19: ; preds = %7
%20 = call i32 (...) @p3dResetStartTime()
%21 = load i32 (i8*, ...)*, i32 (i8*, ...)** %14
%22 = load i8*, i8** %10
%23 = call i32 (i8*, ...) %21(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %19, %7
%25 = load i8*, i8** %10
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %16
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32 (i8*, ...)*, i32 (i8*, ...)** %14
%30 = load i8*, i8** %10
%31 = call i32 (i8*, ...) %29(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i8* %30)
store i32 1, i32* %8
br label %62
32: ; preds = %24
%33 = load i8*, i8** %9
%34 = load i32, i32* %11
%35 = load i32, i32* %12
%36 = mul nsw i32 %34, %35
%37 = load i32, i32* %13
%38 = mul nsw i32 %36, %37
%39 = load i32*, i32** %16
%40 = call i32 @fwrite(i8* %33, i32 1, i32 %38, i32* %39)
%41 = load i32, i32* %11
%42 = load i32, i32* %12
%43 = mul nsw i32 %41, %42
%44 = load i32, i32* %13
%45 = mul nsw i32 %43, %44
%46 = icmp slt i32 %40, %45
br i1 %46, label %47, label %51
47: ; preds = %32
%48 = load i32 (i8*, ...)*, i32 (i8*, ...)** %14
%49 = load i8*, i8** %10
%50 = call i32 (i8*, ...) %48(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i8* %49)
store i32 1, i32* %8
br label %62
51: ; preds = %32
%52 = load i32*, i32** %16
%53 = call i32 @fclose(i32* %52)
%54 = load i32 (i8*, ...)*, i32 (i8*, ...)** %14
%55 = icmp ne i32 (i8*, ...)* %54, null
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load i32 (i8*, ...)*, i32 (i8*, ...)** %14
%58 = call i32 (...) @p3dGetElapsedTime_min()
%59 = call i32 (...) @p3dGetElapsedTime_sec()
%60 = call i32 (i8*, ...) %57(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %58, i32 %59)
br label %61
61: ; preds = %56, %51
store i32 2, i32* %8
br label %62
62: ; preds = %61, %47, %28
%63 = load i32, i32* %8
ret i32 %63
}

LLVM IR (-O2):
@.str = [39 x i8] c"Pore3D - Writing 8-bit RAW file %s ...\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [66 x i8] c"Pore3D - IO error: cannot open output file %s. Program will exit.\00"
@.str.3 = [64 x i8] c"Pore3D - IO error: error on writing file %s. Program will exit.\00"
@.str.4 = [53 x i8] c"Pore3D - RAW file written successfully in %dm%0.3fs.\00"
define i32 @p3dWriteRaw8(i8* nocapture %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 (i8*, ...)* %5, i32 (i32, ...)* nocapture readnone %6) {
%8 = icmp eq i32 (i8*, ...)* %5, null
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = tail call i32 (...) @p3dResetStartTime()
%11 = tail call i32 (i8*, ...) %5(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %12
12: ; preds = %9, %7
%13 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 (i8*, ...) %5(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %30
17: ; preds = %12
%18 = mul nsw i32 %3, %2
%19 = mul nsw i32 %18, %4
%20 = tail call i32 @fwrite(i8* %0, i32 1, i32 %19, i32* %13)
%21 = icmp slt i32 %20, %19
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 (i8*, ...) %5(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %30
24: ; preds = %17
%25 = tail call i32 @fclose(i32* %13)
br i1 %8, label %30, label %26
26: ; preds = %24
%27 = tail call i32 (...) @p3dGetElapsedTime_min()
%28 = tail call i32 (...) @p3dGetElapsedTime_sec()
%29 = tail call i32 (i8*, ...) %5(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %27, i32 %28)
br label %30
30: ; preds = %24, %26, %22, %15
%31 = phi i32 [ 1, %15 ], [ 1, %22 ], [ 2, %26 ], [ 2, %24 ]
ret i32 %31
}