#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 */

/* Variables and functions */
double UCHAR_MAX;
int /*<<< orphan*/ p3dGetElapsedTime_min();
int /*<<< orphan*/ p3dGetElapsedTime_sec();
int /*<<< orphan*/ p3dResetStartTime();
int p3dFrom16To8(unsigned short *in_im, unsigned char *out_im, const int dimx,
                 const int dimy, const int dimz, unsigned short min,
                 unsigned short max, int (*wr_log)(const char *, ...),
                 int (*wr_progress)(const int, ...)) {
  double new_min = 0.0;
  double new_max = UCHAR_MAX * 1.0;
  double tmpval;
  int i;
  if (wr_log != NULL) {
    p3dResetStartTime();
    wr_log("Pore3D - Rescaling and converting image from 16-bit to 8-bit "
           "format...");
    wr_log("\tMin/max values to rescale into [0,255] range: [%d.%d].", min,
           max);
  }
#pragma omp parallel for private(tmpval)
  for (i = 0; i < (dimx * dimy * dimz); i++) {
    tmpval = (in_im[i] - min) / ((max - min) * 1.0) * (new_max - new_min);
    if (tmpval > new_max)
      tmpval = new_max;
    if (tmpval < new_min)
      tmpval = new_min;
    out_im[i] = (unsigned char)(tmpval);
  }
  if (wr_log != NULL) {
    wr_log("Pore3D - Image rescaled and converted successfully in %dm%0.3fs.",
           p3dGetElapsedTime_min(), p3dGetElapsedTime_sec());
  }
  return 2;
}


x86 assembly (clang, -O0):
<p3dFrom16To8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %r9w,%ax
mov    0x20(%rbp),%r9
mov    0x18(%rbp),%r9
mov    0x10(%rbp),%r9w
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %ax,-0x1e(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe91(%rip),%xmm0
mov    0x2e7a(%rip),%rax
mulsd  (%rax),%xmm0
movsd  %xmm0,-0x30(%rbp)
cmpq   $0x0,0x18(%rbp)
je     11b4 <p3dFrom16To8+0x84>
mov    $0x0,%al
call   1050 <p3dResetStartTime@plt>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   *0x18(%rbp)
mov    0x18(%rbp),%rcx
movzwl -0x1e(%rbp),%esi
movzwl 0x10(%rbp),%edx
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   *%rcx
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
cmp    %ecx,%eax
jge    1270 <p3dFrom16To8+0x140>
mov    -0x8(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movzwl (%rax,%rcx,2),%eax
movzwl -0x1e(%rbp),%ecx
sub    %ecx,%eax
cvtsi2sd %eax,%xmm0
movzwl 0x10(%rbp),%eax
movzwl -0x1e(%rbp),%ecx
sub    %ecx,%eax
cvtsi2sd %eax,%xmm1
movsd  0xe03(%rip),%xmm2
mulsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  -0x30(%rbp),%xmm1
subsd  -0x28(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    1232 <p3dFrom16To8+0x102>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1250 <p3dFrom16To8+0x120>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
cvttsd2si -0x38(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11bb <p3dFrom16To8+0x8b>
cmpq   $0x0,0x18(%rbp)
je     12a8 <p3dFrom16To8+0x178>
mov    0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <p3dGetElapsedTime_min@plt>
mov    %eax,-0x4c(%rbp)
mov    $0x0,%al
call   1030 <p3dGetElapsedTime_sec@plt>
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rcx
mov    %eax,%edx
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   *%rcx
mov    $0x2,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p3dFrom16To8>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x68(%rsp),%r13
mov    0x2e9d(%rip),%rax
movsd  (%rax),%xmm0
movzwl %r9w,%ebp
movzwl 0x60(%rsp),%r12d
test   %r13,%r13
je     11a8 <p3dFrom16To8+0x78>
xor    %eax,%eax
movapd %xmm0,0x10(%rsp)
mov    %r8d,0xc(%rsp)
mov    %edx,0x8(%rsp)
call   1050 <p3dResetStartTime@plt>
lea    0xe7d(%rip),%rdi
xor    %eax,%eax
call   *%r13
lea    0xeb8(%rip),%rdi
mov    %ebp,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   *%r13
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%r8d
movapd 0x10(%rsp),%xmm0
imul   %edx,%r15d
imul   %r8d,%r15d
test   %r15d,%r15d
jle    1327 <p3dFrom16To8+0x1f7>
sub    %ebp,%r12d
cvtsi2sd %r12d,%xmm10
mov    %r15d,%eax
cmp    $0x8,%r15d
jae    11d1 <p3dFrom16To8+0xa1>
xor    %ecx,%ecx
jmp    12f0 <p3dFrom16To8+0x1c0>
lea    (%rbx,%rax,2),%rcx
cmp    %r14,%rcx
jbe    11ea <p3dFrom16To8+0xba>
lea    (%r14,%rax,1),%rcx
cmp    %rbx,%rcx
jbe    11ea <p3dFrom16To8+0xba>
xor    %ecx,%ecx
jmp    12f0 <p3dFrom16To8+0x1c0>
mov    %eax,%ecx
and    $0xfffffff8,%ecx
movd   %ebp,%xmm1
pshufd $0x0,%xmm1,%xmm9
movapd %xmm10,%xmm2
unpcklpd %xmm10,%xmm2
movapd %xmm0,%xmm1
unpcklpd %xmm0,%xmm1
xor    %edx,%edx
pxor   %xmm8,%xmm8
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movdqu (%rbx,%rdx,2),%xmm5
movdqa %xmm5,%xmm6
punpcklwd %xmm8,%xmm6
punpckhwd %xmm8,%xmm5
psubd  %xmm9,%xmm6
pshufd $0xee,%xmm6,%xmm7
cvtdq2pd %xmm7,%xmm7
psubd  %xmm9,%xmm5
cvtdq2pd %xmm6,%xmm6
pshufd $0xee,%xmm5,%xmm4
cvtdq2pd %xmm4,%xmm4
cvtdq2pd %xmm5,%xmm5
divpd  %xmm2,%xmm5
divpd  %xmm2,%xmm4
divpd  %xmm2,%xmm6
divpd  %xmm2,%xmm7
mulpd  %xmm1,%xmm7
mulpd  %xmm1,%xmm6
mulpd  %xmm1,%xmm4
mulpd  %xmm1,%xmm5
movapd %xmm1,%xmm3
minpd  %xmm5,%xmm3
movapd %xmm1,%xmm5
minpd  %xmm4,%xmm5
movapd %xmm1,%xmm4
minpd  %xmm6,%xmm4
movapd %xmm1,%xmm6
minpd  %xmm7,%xmm6
xorpd  %xmm7,%xmm7
maxpd  %xmm6,%xmm7
xorpd  %xmm6,%xmm6
maxpd  %xmm5,%xmm6
xorpd  %xmm5,%xmm5
maxpd  %xmm3,%xmm5
xorpd  %xmm3,%xmm3
cvttpd2dq %xmm5,%xmm5
cvttpd2dq %xmm6,%xmm6
maxpd  %xmm4,%xmm3
unpcklpd %xmm6,%xmm5
cvttpd2dq %xmm3,%xmm3
cvttpd2dq %xmm7,%xmm4
unpcklpd %xmm4,%xmm3
packssdw %xmm5,%xmm3
packuswb %xmm3,%xmm3
movq   %xmm3,(%r14,%rdx,1)
add    $0x8,%rdx
cmp    %rdx,%rcx
jne    1220 <p3dFrom16To8+0xf0>
cmp    %rax,%rcx
je     1327 <p3dFrom16To8+0x1f7>
nop
movzwl (%rbx,%rcx,2),%edx
sub    %ebp,%edx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
divsd  %xmm10,%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm0,%xmm2
minsd  %xmm1,%xmm2
xorpd  %xmm1,%xmm1
maxsd  %xmm2,%xmm1
cvttsd2si %xmm1,%edx
mov    %dl,(%r14,%rcx,1)
add    $0x1,%rcx
cmp    %rcx,%rax
jne    12f0 <p3dFrom16To8+0x1c0>
test   %r13,%r13
je     134c <p3dFrom16To8+0x21c>
xor    %eax,%eax
call   1040 <p3dGetElapsedTime_min@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <p3dGetElapsedTime_sec@plt>
lea    0xd3c(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   *%r13
mov    $0x2,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@UCHAR_MAX = global double 0.000000e+00
@.str = [71 x i8] c"Pore3D - Rescaling and converting image from 16-bit to 8-bit format...\00"
@.str.1 = [56 x i8] c"\09Min/max values to rescale into [0,255] range: [%d.%d].\00"
@.str.2 = [65 x i8] c"Pore3D - Image rescaled and converted successfully in %dm%0.3fs.\00"
define i32 @p3dFrom16To8(i16* %0, i8* %1, i32 %2, i32 %3, i32 %4, i16 zeroext %5, i16 zeroext %6, i32 (i8*, ...)* %7, i32 (i32, ...)* %8) {
%10 = alloca i16*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i16
%16 = alloca i16
%17 = alloca i32 (i8*, ...)*
%18 = alloca i32 (i32, ...)*
%19 = alloca double
%20 = alloca double
%21 = alloca double
%22 = alloca i32
store i16* %0, i16** %10
store i8* %1, i8** %11
store i32 %2, i32* %12
store i32 %3, i32* %13
store i32 %4, i32* %14
store i16 %5, i16* %15
store i16 %6, i16* %16
store i32 (i8*, ...)* %7, i32 (i8*, ...)** %17
store i32 (i32, ...)* %8, i32 (i32, ...)** %18
store double 0.000000e+00, double* %19
%23 = load double, double* @UCHAR_MAX
%24 = fmul double %23, 1.000000e+00
store double %24, double* %20
%25 = load i32 (i8*, ...)*, i32 (i8*, ...)** %17
%26 = icmp ne i32 (i8*, ...)* %25, null
br i1 %26, label %27, label %37
27: ; preds = %9
%28 = call i32 (...) @p3dResetStartTime()
%29 = load i32 (i8*, ...)*, i32 (i8*, ...)** %17
%30 = call i32 (i8*, ...) %29(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0))
%31 = load i32 (i8*, ...)*, i32 (i8*, ...)** %17
%32 = load i16, i16* %15
%33 = zext i16 %32 to i32
%34 = load i16, i16* %16
%35 = zext i16 %34 to i32
%36 = call i32 (i8*, ...) %31(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %35)
br label %37
37: ; preds = %27, %9
store i32 0, i32* %22
br label %38
38: ; preds = %87, %37
%39 = load i32, i32* %22
%40 = load i32, i32* %12
%41 = load i32, i32* %13
%42 = mul nsw i32 %40, %41
%43 = load i32, i32* %14
%44 = mul nsw i32 %42, %43
%45 = icmp slt i32 %39, %44
br i1 %45, label %46, label %90
46: ; preds = %38
%47 = load i16*, i16** %10
%48 = load i32, i32* %22
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i16, i16* %47, i64 %49
%51 = load i16, i16* %50
%52 = zext i16 %51 to i32
%53 = load i16, i16* %15
%54 = zext i16 %53 to i32
%55 = sub nsw i32 %52, %54
%56 = sitofp i32 %55 to double
%57 = load i16, i16* %16
%58 = zext i16 %57 to i32
%59 = load i16, i16* %15
%60 = zext i16 %59 to i32
%61 = sub nsw i32 %58, %60
%62 = sitofp i32 %61 to double
%63 = fmul double %62, 1.000000e+00
%64 = fdiv double %56, %63
%65 = load double, double* %20
%66 = load double, double* %19
%67 = fsub double %65, %66
%68 = fmul double %64, %67
store double %68, double* %21
%69 = load double, double* %21
%70 = load double, double* %20
%71 = fcmp ogt double %69, %70
br i1 %71, label %72, label %74
72: ; preds = %46
%73 = load double, double* %20
store double %73, double* %21
br label %74
74: ; preds = %72, %46
%75 = load double, double* %21
%76 = load double, double* %19
%77 = fcmp olt double %75, %76
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = load double, double* %19
store double %79, double* %21
br label %80
80: ; preds = %78, %74
%81 = load double, double* %21
%82 = fptoui double %81 to i8
%83 = load i8*, i8** %11
%84 = load i32, i32* %22
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %83, i64 %85
store i8 %82, i8* %86
br label %87
87: ; preds = %80
%88 = load i32, i32* %22
%89 = add nsw i32 %88, 1
store i32 %89, i32* %22
br label %38
90: ; preds = %38
%91 = load i32 (i8*, ...)*, i32 (i8*, ...)** %17
%92 = icmp ne i32 (i8*, ...)* %91, null
br i1 %92, label %93, label %98
93: ; preds = %90
%94 = load i32 (i8*, ...)*, i32 (i8*, ...)** %17
%95 = call i32 (...) @p3dGetElapsedTime_min()
%96 = call i32 (...) @p3dGetElapsedTime_sec()
%97 = call i32 (i8*, ...) %94(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %95, i32 %96)
br label %98
98: ; preds = %93, %90
ret i32 2
}

LLVM IR (-O2):
@UCHAR_MAX = global double 0.000000e+00
@.str = [71 x i8] c"Pore3D - Rescaling and converting image from 16-bit to 8-bit format...\00"
@.str.1 = [56 x i8] c"\09Min/max values to rescale into [0,255] range: [%d.%d].\00"
@.str.2 = [65 x i8] c"Pore3D - Image rescaled and converted successfully in %dm%0.3fs.\00"
define i32 @p3dFrom16To8(i16* nocapture %0, i8* nocapture writeonly %1, i32 %2, i32 %3, i32 %4, i16 zeroext %5, i16 zeroext %6, i32 (i8*, ...)* %7, i32 (i32, ...)* nocapture readnone %8) {
%10 = bitcast i16* %0 to i8*
%11 = load double, double* @UCHAR_MAX
%12 = icmp eq i32 (i8*, ...)* %7, null
br i1 %12, label %19, label %13
13: ; preds = %9
%14 = tail call i32 (...) @p3dResetStartTime()
%15 = tail call i32 (i8*, ...) %7(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0))
%16 = zext i16 %5 to i32
%17 = zext i16 %6 to i32
%18 = tail call i32 (i8*, ...) %7(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17)
br label %19
19: ; preds = %13, %9
%20 = mul nsw i32 %3, %2
%21 = mul i32 %20, %4
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %85
23: ; preds = %19
%24 = zext i16 %5 to i32
%25 = zext i16 %6 to i32
%26 = sub nsw i32 %25, %24
%27 = sitofp i32 %26 to double
%28 = zext i32 %21 to i64
%29 = icmp ult i32 %21, 8
br i1 %29, label %66, label %30
30: ; preds = %23
%31 = getelementptr i8, i8* %1, i64 %28
%32 = getelementptr i16, i16* %0, i64 %28
%33 = bitcast i16* %32 to i8*
%34 = icmp ugt i8* %33, %1
%35 = icmp ugt i8* %31, %10
%36 = and i1 %34, %35
br i1 %36, label %66, label %37
37: ; preds = %30
%38 = and i64 %28, 4294967288
%39 = insertelement <8 x i32> poison, i32 %24, i64 0
%40 = shufflevector <8 x i32> %39, <8 x i32> poison, <8 x i32> zeroinitializer
%41 = insertelement <8 x double> poison, double %27, i64 0
%42 = shufflevector <8 x double> %41, <8 x double> poison, <8 x i32> zeroinitializer
%43 = insertelement <8 x double> poison, double %11, i64 0
%44 = shufflevector <8 x double> %43, <8 x double> poison, <8 x i32> zeroinitializer
br label %45
45: ; preds = %45, %37
%46 = phi i64 [ 0, %37 ], [ %62, %45 ]
%47 = getelementptr inbounds i16, i16* %0, i64 %46
%48 = bitcast i16* %47 to <8 x i16>*
%49 = load <8 x i16>, <8 x i16>* %48
%50 = zext <8 x i16> %49 to <8 x i32>
%51 = sub nsw <8 x i32> %50, %40
%52 = sitofp <8 x i32> %51 to <8 x double>
%53 = fdiv <8 x double> %52, %42
%54 = fmul <8 x double> %44, %53
%55 = fcmp ogt <8 x double> %54, %44
%56 = select <8 x i1> %55, <8 x double> %44, <8 x double> %54
%57 = fcmp olt <8 x double> %56, zeroinitializer
%58 = select <8 x i1> %57, <8 x double> zeroinitializer, <8 x double> %56
%59 = fptoui <8 x double> %58 to <8 x i8>
%60 = getelementptr inbounds i8, i8* %1, i64 %46
%61 = bitcast i8* %60 to <8 x i8>*
store <8 x i8> %59, <8 x i8>* %61
%62 = add nuw i64 %46, 8
%63 = icmp eq i64 %62, %38
br i1 %63, label %64, label %45
64: ; preds = %45
%65 = icmp eq i64 %38, %28
br i1 %65, label %85, label %66
66: ; preds = %30, %23, %64
%67 = phi i64 [ 0, %30 ], [ 0, %23 ], [ %38, %64 ]
br label %68
68: ; preds = %66, %68
%69 = phi i64 [ %83, %68 ], [ %67, %66 ]
%70 = getelementptr inbounds i16, i16* %0, i64 %69
%71 = load i16, i16* %70
%72 = zext i16 %71 to i32
%73 = sub nsw i32 %72, %24
%74 = sitofp i32 %73 to double
%75 = fdiv double %74, %27
%76 = fmul double %11, %75
%77 = fcmp ogt double %76, %11
%78 = select i1 %77, double %11, double %76
%79 = fcmp olt double %78, 0.000000e+00
%80 = select i1 %79, double 0.000000e+00, double %78
%81 = fptoui double %80 to i8
%82 = getelementptr inbounds i8, i8* %1, i64 %69
store i8 %81, i8* %82
%83 = add nuw nsw i64 %69, 1
%84 = icmp eq i64 %83, %28
br i1 %84, label %85, label %68
85: ; preds = %68, %64, %19
br i1 %12, label %90, label %86
86: ; preds = %85
%87 = tail call i32 (...) @p3dGetElapsedTime_min()
%88 = tail call i32 (...) @p3dGetElapsedTime_sec()
%89 = tail call i32 (i8*, ...) %7(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %87, i32 %88)
br label %90
90: ; preds = %86, %85
ret i32 2
}