; LLVM IR from datocms/fast_thumbhash :: ext/fast_thumbhash (fast_thumbhash)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @decode_channel(i8 noundef zeroext %0, i8 noundef zeroext %1, double noundef %2, ptr noundef %3, i8 noundef zeroext %4, ptr noundef %5) #2 {
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca double, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i8, align 1
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca double, align 8
  store i8 %0, ptr %7, align 1, !tbaa !5
  store i8 %1, ptr %8, align 1, !tbaa !5
  store double %2, ptr %9, align 8, !tbaa !8
  store ptr %3, ptr %10, align 8, !tbaa !10
  store i8 %4, ptr %11, align 1, !tbaa !5
  store ptr %5, ptr %12, align 8, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %19 = load i8, ptr %7, align 1, !tbaa !5
  %20 = zext i8 %19 to i32
  %21 = load i8, ptr %8, align 1, !tbaa !5
  %22 = zext i8 %21 to i32
  %23 = mul nsw i32 %20, %22
  %24 = sext i32 %23 to i64
  %25 = mul i64 %24, 8
  %26 = call noalias ptr @malloc(i64 noundef %25) #4
  store ptr %26, ptr %13, align 8, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  store i32 0, ptr %15, align 4, !tbaa !12
  br label %27

27:                                               ; preds = %88, %6
  %28 = load i32, ptr %15, align 4, !tbaa !12
  %29 = load i8, ptr %8, align 1, !tbaa !5
  %30 = zext i8 %29 to i32
  %31 = icmp slt i32 %28, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %27
  store i32 2, ptr %16, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  br label %91

33:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #3
  %34 = load i32, ptr %15, align 4, !tbaa !12
  %35 = icmp ne i32 %34, 0
  %36 = zext i1 %35 to i64
  %37 = select i1 %35, i32 0, i32 1
  store i32 %37, ptr %17, align 4, !tbaa !12
  br label %38

38:                                               ; preds = %84, %33
  %39 = load i32, ptr %17, align 4, !tbaa !12
  %40 = load i8, ptr %8, align 1, !tbaa !5
  %41 = zext i8 %40 to i32
  %42 = mul nsw i32 %39, %41
  %43 = load i8, ptr %7, align 1, !tbaa !5
  %44 = zext i8 %43 to i32
  %45 = load i8, ptr %8, align 1, !tbaa !5
  %46 = zext i8 %45 to i32
  %47 = load i32, ptr %15, align 4, !tbaa !12
  %48 = sub nsw i32 %46, %47
  %49 = mul nsw i32 %44, %48
  %50 = icmp slt i32 %42, %49
  br i1 %50, label %52, label %51

51:                                               ; preds = %38
  store i32 5, ptr %16, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #3
  br label %87

52:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #3
  %53 = load ptr, ptr %10, align 8, !tbaa !10
  %54 = load i8, ptr %11, align 1, !tbaa !5
  %55 = zext i8 %54 to i32
  %56 = load ptr, ptr %12, align 8, !tbaa !10
  %57 = load i8, ptr %56, align 1, !tbaa !5
  %58 = zext i8 %57 to i32
  %59 = ashr i32 %58, 1
  %60 = add nsw i32 %55, %59
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i8, ptr %53, i64 %61
  %63 = load i8, ptr %62, align 1, !tbaa !5
  %64 = zext i8 %63 to i32
  %65 = load ptr, ptr %12, align 8, !tbaa !10
  %66 = load i8, ptr %65, align 1, !tbaa !5
  %67 = add i8 %66, 1
  store i8 %67, ptr %65, align 1, !tbaa !5
  %68 = zext i8 %66 to i32
  %69 = and i32 %68, 1
  %70 = shl i32 %69, 2
  %71 = ashr i32 %64, %70
  %72 = and i32 %71, 15
  %73 = sitofp i32 %72 to double
  store double %73, ptr %18, align 8, !tbaa !8
  %74 = load double, ptr %18, align 8, !tbaa !8
  %75 = fdiv double %74, 7.500000e+00
  %76 = fsub double %75, 1.000000e+00
  %77 = load double, ptr %9, align 8, !tbaa !8
  %78 = fmul double %76, %77
  %79 = load ptr, ptr %13, align 8, !tbaa !10
  %80 = load i32, ptr %14, align 4, !tbaa !12
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %14, align 4, !tbaa !12
  %82 = sext i32 %80 to i64
  %83 = getelementptr inbounds double, ptr %79, i64 %82
  store double %78, ptr %83, align 8, !tbaa !8
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #3
  br label %84

84:                                               ; preds = %52
  %85 = load i32, ptr %17, align 4, !tbaa !12
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %17, align 4, !tbaa !12
  br label %38, !llvm.loop !14

87:                                               ; preds = %51
  br label %88

88:                                               ; preds = %87
  %89 = load i32, ptr %15, align 4, !tbaa !12
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %15, align 4, !tbaa !12
  br label %27, !llvm.loop !16

91:                                               ; preds = %32
  %92 = load ptr, ptr %13, align 8, !tbaa !10
  store i32 1, ptr %16, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  ret ptr %92
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(0) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"double", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind memory(write, argmem: readwrite, inaccessiblemem: readwrite) uwtable
define dso_local noalias noundef ptr @decode_channel(i8 noundef zeroext %0, i8 noundef zeroext %1, double noundef %2, ptr nocapture noundef readonly %3, i8 noundef zeroext %4, ptr nocapture noundef %5) local_unnamed_addr #1 {
  %7 = zext i8 %0 to i32
  %8 = zext i8 %1 to i32
  %9 = shl nuw nsw i32 %7, 3
  %10 = mul nuw nsw i32 %9, %8
  %11 = zext nneg i32 %10 to i64
  %12 = tail call noalias ptr @malloc(i64 noundef %11) #2
  %.not30 = icmp eq i8 %1, 0
  br i1 %.not30, label %._crit_edge29, label %.lr.ph28

.lr.ph28:                                         ; preds = %6
  %13 = zext i8 %4 to i32
  br label %14

._crit_edge29:                                    ; preds = %._crit_edge, %6
  ret ptr %12

14:                                               ; preds = %.lr.ph28, %._crit_edge
  %.026 = phi i32 [ 0, %.lr.ph28 ], [ %.1.lcssa, %._crit_edge ]
  %.02125 = phi i32 [ 0, %.lr.ph28 ], [ %22, %._crit_edge ]
  %.not = icmp eq i32 %.02125, 0
  %15 = sub nsw i32 %8, %.02125
  %16 = mul nsw i32 %15, %7
  %17 = select i1 %.not, i32 %8, i32 0
  %18 = icmp slt i32 %17, %16
  br i1 %18, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %14
  %19 = zext i1 %.not to i32
  %.promoted = load i8, ptr %5, align 1, !tbaa !5
  %20 = sext i32 %.026 to i64
  br label %23

._crit_edge.loopexit:                             ; preds = %23
  %21 = trunc nsw i64 %indvars.iv.next to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %14
  %.1.lcssa = phi i32 [ %.026, %14 ], [ %21, %._crit_edge.loopexit ]
  %22 = add nuw nsw i32 %.02125, 1
  %exitcond.not = icmp eq i32 %22, %8
  br i1 %exitcond.not, label %._crit_edge29, label %14, !llvm.loop !8

23:                                               ; preds = %.lr.ph, %23
  %indvars.iv = phi i64 [ %20, %.lr.ph ], [ %indvars.iv.next, %23 ]
  %24 = phi i8 [ %.promoted, %.lr.ph ], [ %32, %23 ]
  %.02023 = phi i32 [ %19, %.lr.ph ], [ %42, %23 ]
  %25 = zext i8 %24 to i32
  %26 = lshr i32 %25, 1
  %27 = add nuw nsw i32 %26, %13
  %28 = zext nneg i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %3, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !5
  %31 = zext i8 %30 to i32
  %32 = add i8 %24, 1
  store i8 %32, ptr %5, align 1, !tbaa !5
  %33 = shl nuw nsw i32 %25, 2
  %34 = and i32 %33, 4
  %35 = lshr i32 %31, %34
  %36 = and i32 %35, 15
  %37 = uitofp nneg i32 %36 to double
  %38 = fdiv double %37, 7.500000e+00
  %39 = fadd double %38, -1.000000e+00
  %40 = fmul double %39, %2
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %41 = getelementptr inbounds double, ptr %12, i64 %indvars.iv
  store double %40, ptr %41, align 8, !tbaa !10
  %42 = add nuw nsw i32 %.02023, 1
  %43 = mul nuw nsw i32 %42, %8
  %44 = icmp slt i32 %43, %16
  br i1 %44, label %23, label %._crit_edge.loopexit, !llvm.loop !12
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nofree nounwind memory(write, argmem: readwrite, inaccessiblemem: readwrite) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind allocsize(0) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !11, i64 0}
!11 = !{!"double", !6, i64 0}
!12 = distinct !{!12, !9}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass