; LLVM IR from corentinartaud/graphics :: ThirdParty/FreeImage-3170/Source/OpenEXR/IlmImf (ImfRle)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN7Imf_2_213rleUncompressEiiPKaPc(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %14 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %14, ptr %10, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %86, %4
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = icmp sgt i32 %16, 0
  br i1 %17, label %18, label %87

18:                                               ; preds = %15
  %19 = load ptr, ptr %8, align 8, !tbaa !9
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = sext i8 %20 to i32
  %22 = icmp slt i32 %21, 0
  br i1 %22, label %23, label %54

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %24 = load ptr, ptr %8, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %24, i32 1
  store ptr %25, ptr %8, align 8, !tbaa !9
  %26 = load i8, ptr %24, align 1, !tbaa !11
  %27 = sext i8 %26 to i32
  %28 = sub nsw i32 0, %27
  store i32 %28, ptr %11, align 4, !tbaa !5
  %29 = load i32, ptr %11, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sub nsw i32 %31, %30
  store i32 %32, ptr %6, align 4, !tbaa !5
  %33 = load i32, ptr %11, align 4, !tbaa !5
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = sub nsw i32 %34, %33
  store i32 %35, ptr %7, align 4, !tbaa !5
  %36 = icmp sgt i32 0, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %23
  store i32 0, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %51

38:                                               ; preds = %23
  %39 = load ptr, ptr %9, align 8, !tbaa !9
  %40 = load ptr, ptr %8, align 8, !tbaa !9
  %41 = load i32, ptr %11, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %39, ptr align 1 %40, i64 %42, i1 false)
  %43 = load i32, ptr %11, align 4, !tbaa !5
  %44 = load ptr, ptr %9, align 8, !tbaa !9
  %45 = sext i32 %43 to i64
  %46 = getelementptr inbounds i8, ptr %44, i64 %45
  store ptr %46, ptr %9, align 8, !tbaa !9
  %47 = load i32, ptr %11, align 4, !tbaa !5
  %48 = load ptr, ptr %8, align 8, !tbaa !9
  %49 = sext i32 %47 to i64
  %50 = getelementptr inbounds i8, ptr %48, i64 %49
  store ptr %50, ptr %8, align 8, !tbaa !9
  store i32 0, ptr %12, align 4
  br label %51

51:                                               ; preds = %38, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  %52 = load i32, ptr %12, align 4
  switch i32 %52, label %94 [
    i32 0, label %53
  ]

53:                                               ; preds = %51
  br label %86

54:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %55 = load ptr, ptr %8, align 8, !tbaa !9
  %56 = getelementptr inbounds i8, ptr %55, i32 1
  store ptr %56, ptr %8, align 8, !tbaa !9
  %57 = load i8, ptr %55, align 1, !tbaa !11
  %58 = sext i8 %57 to i32
  store i32 %58, ptr %13, align 4, !tbaa !5
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = sub nsw i32 %59, 2
  store i32 %60, ptr %6, align 4, !tbaa !5
  %61 = load i32, ptr %13, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = sub nsw i32 %63, %62
  store i32 %64, ptr %7, align 4, !tbaa !5
  %65 = icmp sgt i32 0, %64
  br i1 %65, label %66, label %67

66:                                               ; preds = %54
  store i32 0, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %83

67:                                               ; preds = %54
  %68 = load ptr, ptr %9, align 8, !tbaa !9
  %69 = load ptr, ptr %8, align 8, !tbaa !9
  %70 = load i8, ptr %69, align 1, !tbaa !11
  %71 = sext i8 %70 to i32
  %72 = trunc i32 %71 to i8
  %73 = load i32, ptr %13, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  %75 = sext i32 %74 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %68, i8 %72, i64 %75, i1 false)
  %76 = load i32, ptr %13, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  %78 = load ptr, ptr %9, align 8, !tbaa !9
  %79 = sext i32 %77 to i64
  %80 = getelementptr inbounds i8, ptr %78, i64 %79
  store ptr %80, ptr %9, align 8, !tbaa !9
  %81 = load ptr, ptr %8, align 8, !tbaa !9
  %82 = getelementptr inbounds i8, ptr %81, i32 1
  store ptr %82, ptr %8, align 8, !tbaa !9
  store i32 0, ptr %12, align 4
  br label %83

83:                                               ; preds = %67, %66
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  %84 = load i32, ptr %12, align 4
  switch i32 %84, label %94 [
    i32 0, label %85
  ]

85:                                               ; preds = %83
  br label %86

86:                                               ; preds = %85, %53
  br label %15, !llvm.loop !12

87:                                               ; preds = %15
  %88 = load ptr, ptr %9, align 8, !tbaa !9
  %89 = load ptr, ptr %10, align 8, !tbaa !9
  %90 = ptrtoint ptr %88 to i64
  %91 = ptrtoint ptr %89 to i64
  %92 = sub i64 %90, %91
  %93 = trunc i64 %92 to i32
  store i32 %93, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %94

94:                                               ; preds = %87, %83, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %95 = load i32, ptr %5, align 4
  ret i32 %95
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nounwind }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; 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 norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_ZN7Imf_2_213rleUncompressEiiPKaPc(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %0, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4, %26
  %.03255 = phi i32 [ %.133, %26 ], [ %0, %4 ]
  %.03454 = phi i32 [ %.135, %26 ], [ %1, %4 ]
  %.03653 = phi ptr [ %.339, %26 ], [ %3, %4 ]
  %.04052 = phi ptr [ %.343, %26 ], [ %2, %4 ]
  %6 = load i8, ptr %.04052, align 1, !tbaa !5
  %7 = sext i8 %6 to i32
  %8 = icmp slt i8 %6, 0
  br i1 %8, label %9, label %17

9:                                                ; preds = %.lr.ph
  %10 = add nsw i32 %.03454, %7
  %11 = icmp sgt i32 %10, -1
  br i1 %11, label %.thread, label %.loopexit

.thread:                                          ; preds = %9
  %.neg = add nsw i32 %.03255, -1
  %12 = add i32 %.neg, %7
  %13 = getelementptr inbounds i8, ptr %.04052, i64 1
  %14 = sub nsw i32 0, %7
  %15 = zext nneg i32 %14 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.03653, ptr nonnull align 1 %13, i64 %15, i1 false)
  %16 = getelementptr inbounds i8, ptr %13, i64 %15
  br label %26

17:                                               ; preds = %.lr.ph
  %18 = add nuw nsw i32 %7, 1
  %19 = sub nsw i32 %.03454, %18
  %20 = icmp sgt i32 %19, -1
  br i1 %20, label %.thread48, label %.loopexit

.thread48:                                        ; preds = %17
  %21 = add nsw i32 %.03255, -2
  %22 = getelementptr inbounds i8, ptr %.04052, i64 1
  %23 = load i8, ptr %22, align 1, !tbaa !5
  %24 = zext nneg i32 %18 to i64
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %.03653, i8 %23, i64 %24, i1 false)
  %25 = getelementptr inbounds i8, ptr %.04052, i64 2
  br label %26

26:                                               ; preds = %.thread48, %.thread
  %.343 = phi ptr [ %16, %.thread ], [ %25, %.thread48 ]
  %.pn = phi i64 [ %15, %.thread ], [ %24, %.thread48 ]
  %.135 = phi i32 [ %10, %.thread ], [ %19, %.thread48 ]
  %.133 = phi i32 [ %12, %.thread ], [ %21, %.thread48 ]
  %.339 = getelementptr inbounds i8, ptr %.03653, i64 %.pn
  %27 = icmp sgt i32 %.133, 0
  br i1 %27, label %.lr.ph, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %26, %4
  %.036.lcssa = phi ptr [ %3, %4 ], [ %.339, %26 ]
  %28 = ptrtoint ptr %.036.lcssa to i64
  %29 = ptrtoint ptr %3 to i64
  %30 = sub i64 %28, %29
  %31 = trunc i64 %30 to i32
  br label %.loopexit

.loopexit:                                        ; preds = %17, %9, %._crit_edge
  %.4 = phi i32 [ %31, %._crit_edge ], [ 0, %9 ], [ 0, %17 ]
  ret i32 %.4
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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