; LLVM IR from kojiba/purge :: PurgeEvasionUtils (decryptPurgeEvasion)

; 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"

@.str = external hidden unnamed_addr constant [80 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [78 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @evasionHashData(ptr noundef, i64 noundef, ptr noundef) #0

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

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

; 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) #3

declare void @evasionRand(ptr noundef) #4

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #5

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #5

; Function Attrs: nounwind uwtable
define dso_local ptr @decryptPurgeEvasion(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca ptr, align 8
  %16 = alloca [8 x i64], align 16
  %17 = alloca ptr, align 8
  %18 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #9
  store ptr null, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #9
  store ptr null, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #9
  %19 = load i64, ptr %7, align 8, !tbaa !9
  %20 = sub i64 %19, 64
  %21 = udiv i64 %20, 64
  store i64 %21, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #9
  %22 = call ptr @llvm.stacksave.p0()
  store ptr %22, ptr %15, align 8
  %23 = alloca i8, i64 64, align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr %16) #9
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #9
  store ptr null, ptr %17, align 8, !tbaa !5
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = urem i64 %24, 64
  %26 = icmp ne i64 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %4
  call void @perror(ptr noundef @.str)
  store ptr null, ptr %5, align 8
  store i32 1, ptr %18, align 4
  br label %89

28:                                               ; preds = %4
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = call noalias ptr @malloc(i64 noundef %29) #10
  store ptr %30, ptr %11, align 8, !tbaa !5
  %31 = load ptr, ptr %11, align 8, !tbaa !5
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %87

33:                                               ; preds = %28
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 0, ptr %34, align 8, !tbaa !9
  %35 = load ptr, ptr %11, align 8, !tbaa !5
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %35, ptr align 1 %36, i64 %37, i1 false)
  %38 = load ptr, ptr %11, align 8, !tbaa !5
  %39 = load i64, ptr %7, align 8, !tbaa !9
  %40 = getelementptr inbounds i8, ptr %38, i64 %39
  %41 = getelementptr inbounds i8, ptr %40, i64 -64
  store ptr %41, ptr %17, align 8, !tbaa !5
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %42

42:                                               ; preds = %53, %33
  %43 = load i64, ptr %10, align 8, !tbaa !9
  %44 = load i64, ptr %13, align 8, !tbaa !9
  %45 = icmp ult i64 %43, %44
  br i1 %45, label %46, label %56

46:                                               ; preds = %42
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  call void @evasionRand(ptr noundef %47)
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %23, ptr align 8 %48, i64 64, i1 false)
  %49 = load ptr, ptr %11, align 8, !tbaa !5
  %50 = load i64, ptr %10, align 8, !tbaa !9
  %51 = mul i64 %50, 64
  %52 = getelementptr inbounds i8, ptr %49, i64 %51
  call void @purgeDecrypt(ptr noundef %52, ptr noundef %23)
  call void @llvm.memset.p0.i64(ptr align 16 %23, i8 0, i64 64, i1 false)
  br label %53

53:                                               ; preds = %46
  %54 = load i64, ptr %10, align 8, !tbaa !9
  %55 = add i64 %54, 1
  store i64 %55, ptr %10, align 8, !tbaa !9
  br label %42, !llvm.loop !11

56:                                               ; preds = %42
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  call void @evasionRand(ptr noundef %57)
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %23, ptr align 8 %58, i64 64, i1 false)
  %59 = load ptr, ptr %17, align 8, !tbaa !5
  call void @purgeDecrypt(ptr noundef %59, ptr noundef %23)
  call void @llvm.memset.p0.i64(ptr align 16 %23, i8 0, i64 64, i1 false)
  %60 = load ptr, ptr %11, align 8, !tbaa !5
  %61 = getelementptr inbounds i8, ptr %60, i64 8
  %62 = load i64, ptr %7, align 8, !tbaa !9
  %63 = sub i64 %62, 64
  %64 = sub i64 %63, 8
  %65 = getelementptr inbounds [8 x i64], ptr %16, i64 0, i64 0
  call void @evasionHashData(ptr noundef %61, i64 noundef %64, ptr noundef %65)
  %66 = getelementptr inbounds [8 x i64], ptr %16, i64 0, i64 0
  %67 = load ptr, ptr %17, align 8, !tbaa !5
  %68 = call i32 @memcmp(ptr noundef %66, ptr noundef %67, i64 noundef 64) #11
  %69 = icmp eq i32 %68, 0
  br i1 %69, label %70, label %84

70:                                               ; preds = %56
  %71 = load ptr, ptr %11, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %14, ptr align 1 %71, i64 8, i1 false)
  %72 = load i64, ptr %14, align 8, !tbaa !9
  %73 = call noalias ptr @malloc(i64 noundef %72) #10
  store ptr %73, ptr %12, align 8, !tbaa !5
  %74 = load ptr, ptr %12, align 8, !tbaa !5
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %83

76:                                               ; preds = %70
  %77 = load ptr, ptr %12, align 8, !tbaa !5
  %78 = load ptr, ptr %11, align 8, !tbaa !5
  %79 = getelementptr inbounds i8, ptr %78, i64 8
  %80 = load i64, ptr %14, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %77, ptr align 1 %79, i64 %80, i1 false)
  %81 = load i64, ptr %14, align 8, !tbaa !9
  %82 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 %81, ptr %82, align 8, !tbaa !9
  br label %83

83:                                               ; preds = %76, %70
  br label %85

84:                                               ; preds = %56
  call void @perror(ptr noundef @.str.1)
  br label %85

85:                                               ; preds = %84, %83
  %86 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %86) #9
  br label %87

87:                                               ; preds = %85, %28
  %88 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %88, ptr %5, align 8
  store i32 1, ptr %18, align 4
  br label %89

89:                                               ; preds = %87, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #9
  call void @llvm.lifetime.end.p0(i64 64, ptr %16) #9
  %90 = load ptr, ptr %15, align 8
  call void @llvm.stackrestore.p0(ptr %90)
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #9
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #9
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #9
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #9
  %91 = load ptr, ptr %5, align 8
  ret ptr %91
}

declare void @perror(ptr noundef) #4

declare void @purgeDecrypt(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #7

; Function Attrs: nounwind
declare void @free(ptr noundef) #8

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { nocallback nofree nosync nounwind willreturn }
attributes #6 = { 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 #7 = { nounwind willreturn memory(read) "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 #8 = { nounwind "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 #9 = { nounwind }
attributes #10 = { nounwind allocsize(0) }
attributes #11 = { nounwind willreturn memory(read) }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@.str = external hidden unnamed_addr constant [80 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [78 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @evasionHashData(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

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

; 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) #3

declare void @evasionRand(ptr noundef) local_unnamed_addr #4

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

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

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @decryptPurgeEvasion(ptr nocapture noundef readonly %0, i64 noundef %1, ptr noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = alloca [8 x i64], align 16
  %6 = add i64 %1, -64
  %7 = lshr i64 %6, 6
  %8 = alloca [64 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #10
  %9 = and i64 %1, 63
  %.not = icmp eq i64 %9, 0
  br i1 %.not, label %11, label %10

10:                                               ; preds = %4
  tail call void @perror(ptr noundef nonnull @.str) #11
  br label %27

11:                                               ; preds = %4
  %12 = tail call noalias ptr @malloc(i64 noundef %1) #12
  %.not39 = icmp eq ptr %12, null
  br i1 %.not39, label %27, label %13

13:                                               ; preds = %11
  store i64 0, ptr %3, align 8, !tbaa !5
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %12, ptr align 1 %0, i64 %1, i1 false)
  %14 = getelementptr inbounds i8, ptr %12, i64 %1
  %15 = getelementptr inbounds i8, ptr %14, i64 -64
  %.not42 = icmp ult i64 %6, 64
  br i1 %.not42, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %13
  %umax = tail call i64 @llvm.umax.i64(i64 %7, i64 1)
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.03441 = phi i64 [ %18, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  call void @evasionRand(ptr noundef %2) #10
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %8, ptr noundef nonnull align 8 dereferenceable(64) %2, i64 64, i1 false)
  %16 = shl nuw i64 %.03441, 6
  %17 = getelementptr inbounds i8, ptr %12, i64 %16
  call void @purgeDecrypt(ptr noundef nonnull %17, ptr noundef nonnull %8) #10
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %8, i8 0, i64 64, i1 false)
  %18 = add nuw nsw i64 %.03441, 1
  %exitcond.not = icmp eq i64 %18, %umax
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %13
  call void @evasionRand(ptr noundef %2) #10
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %8, ptr noundef nonnull align 8 dereferenceable(64) %2, i64 64, i1 false)
  call void @purgeDecrypt(ptr noundef nonnull %15, ptr noundef nonnull %8) #10
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %8, i8 0, i64 64, i1 false)
  %19 = getelementptr inbounds i8, ptr %12, i64 8
  %20 = add i64 %1, -72
  call void @evasionHashData(ptr noundef nonnull %19, i64 noundef %20, ptr noundef nonnull %5)
  %bcmp = call i32 @bcmp(ptr noundef nonnull dereferenceable(64) %5, ptr noundef nonnull dereferenceable(64) %15, i64 64)
  %21 = icmp eq i32 %bcmp, 0
  br i1 %21, label %22, label %25

22:                                               ; preds = %._crit_edge
  %.0.copyload = load i64, ptr %12, align 1
  %23 = call noalias ptr @malloc(i64 noundef %.0.copyload) #12
  %.not40 = icmp eq ptr %23, null
  br i1 %.not40, label %26, label %24

24:                                               ; preds = %22
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %23, ptr nonnull align 1 %19, i64 %.0.copyload, i1 false)
  store i64 %.0.copyload, ptr %3, align 8, !tbaa !5
  br label %26

25:                                               ; preds = %._crit_edge
  call void @perror(ptr noundef nonnull @.str.1) #11
  br label %26

26:                                               ; preds = %22, %24, %25
  %.035 = phi ptr [ %23, %24 ], [ null, %22 ], [ null, %25 ]
  call void @free(ptr noundef %12) #10
  br label %27

27:                                               ; preds = %11, %26, %10
  %.0 = phi ptr [ null, %10 ], [ %.035, %26 ], [ null, %11 ]
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #10
  ret ptr %.0
}

; Function Attrs: nofree nounwind
declare void @perror(ptr nocapture noundef readonly) local_unnamed_addr #6

declare void @purgeDecrypt(ptr noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #7

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #8

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #9

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { 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 #6 = { nofree nounwind "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 #7 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #8 = { nofree nounwind willreturn memory(argmem: read) }
attributes #9 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #10 = { nounwind }
attributes #11 = { cold }
attributes #12 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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