; LLVM IR from kala13x/libxutils :: src/crypt (aes)

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

%struct.AESContext = type { [64 x i32], [64 x i32], [16 x i8], i64 }

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

; Function Attrs: nounwind uwtable
declare dso_local void @XAES_DecryptBlock(ptr noundef, ptr noundef, ptr noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local ptr @XAES_Decrypt(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca [16 x i8], align 16
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca [16 x i8], align 16
  %16 = alloca i64, align 8
  %17 = alloca i8, align 1
  %18 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %28, label %21

21:                                               ; preds = %3
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = icmp eq ptr %22, null
  br i1 %23, label %28, label %24

24:                                               ; preds = %21
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = icmp ne i64 %26, 0
  br i1 %27, label %29, label %28

28:                                               ; preds = %24, %21, %3
  store ptr null, ptr %4, align 8
  br label %140

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = load i64, ptr %30, align 8, !tbaa !9
  store i64 %31, ptr %8, align 8, !tbaa !9
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = urem i64 %32, 16
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %29
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %139

36:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %37 = load i64, ptr %8, align 8, !tbaa !9
  %38 = add i64 %37, 1
  %39 = call noalias ptr @malloc(i64 noundef %38) #6
  store ptr %39, ptr %10, align 8, !tbaa !5
  %40 = load ptr, ptr %10, align 8, !tbaa !5
  %41 = icmp ne ptr %40, null
  br i1 %41, label %43, label %42

42:                                               ; preds = %36
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %138

43:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #5
  %44 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 0
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.AESContext, ptr %45, i32 0, i32 2
  %47 = getelementptr inbounds [16 x i8], ptr %46, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %44, ptr align 8 %47, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %48, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %49, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %50 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %50, ptr %14, align 8, !tbaa !9
  br label %51

51:                                               ; preds = %78, %43
  %52 = load i64, ptr %14, align 8, !tbaa !9
  %53 = icmp ugt i64 %52, 0
  br i1 %53, label %54, label %87

54:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 16, ptr %15) #5
  %55 = getelementptr inbounds [16 x i8], ptr %15, i64 0, i64 0
  %56 = load ptr, ptr %12, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %55, ptr align 1 %56, i64 16, i1 false)
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load ptr, ptr %13, align 8, !tbaa !5
  %59 = load ptr, ptr %12, align 8, !tbaa !5
  call void @XAES_DecryptBlock(ptr noundef %57, ptr noundef %58, ptr noundef %59)
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #5
  store i64 0, ptr %16, align 8, !tbaa !9
  br label %60

60:                                               ; preds = %75, %54
  %61 = load i64, ptr %16, align 8, !tbaa !9
  %62 = icmp ult i64 %61, 16
  br i1 %62, label %63, label %78

63:                                               ; preds = %60
  %64 = load i64, ptr %16, align 8, !tbaa !9
  %65 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 %64
  %66 = load i8, ptr %65, align 1, !tbaa !11
  %67 = zext i8 %66 to i32
  %68 = load ptr, ptr %13, align 8, !tbaa !5
  %69 = load i64, ptr %16, align 8, !tbaa !9
  %70 = getelementptr inbounds i8, ptr %68, i64 %69
  %71 = load i8, ptr %70, align 1, !tbaa !11
  %72 = zext i8 %71 to i32
  %73 = xor i32 %72, %67
  %74 = trunc i32 %73 to i8
  store i8 %74, ptr %70, align 1, !tbaa !11
  br label %75

75:                                               ; preds = %63
  %76 = load i64, ptr %16, align 8, !tbaa !9
  %77 = add i64 %76, 1
  store i64 %77, ptr %16, align 8, !tbaa !9
  br label %60, !llvm.loop !12

78:                                               ; preds = %60
  %79 = getelementptr inbounds [16 x i8], ptr %11, i64 0, i64 0
  %80 = getelementptr inbounds [16 x i8], ptr %15, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %79, ptr align 16 %80, i64 16, i1 false)
  %81 = load i64, ptr %14, align 8, !tbaa !9
  %82 = sub i64 %81, 16
  store i64 %82, ptr %14, align 8, !tbaa !9
  %83 = load ptr, ptr %13, align 8, !tbaa !5
  %84 = getelementptr inbounds i8, ptr %83, i64 16
  store ptr %84, ptr %13, align 8, !tbaa !5
  %85 = load ptr, ptr %12, align 8, !tbaa !5
  %86 = getelementptr inbounds i8, ptr %85, i64 16
  store ptr %86, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %15) #5
  br label %51, !llvm.loop !14

87:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 1, ptr %17) #5
  %88 = load ptr, ptr %10, align 8, !tbaa !5
  %89 = load i64, ptr %8, align 8, !tbaa !9
  %90 = sub i64 %89, 1
  %91 = getelementptr inbounds i8, ptr %88, i64 %90
  %92 = load i8, ptr %91, align 1, !tbaa !11
  store i8 %92, ptr %17, align 1, !tbaa !11
  %93 = load i8, ptr %17, align 1, !tbaa !11
  %94 = icmp ne i8 %93, 0
  br i1 %94, label %95, label %99

95:                                               ; preds = %87
  %96 = load i8, ptr %17, align 1, !tbaa !11
  %97 = zext i8 %96 to i32
  %98 = icmp sgt i32 %97, 16
  br i1 %98, label %99, label %101

99:                                               ; preds = %95, %87
  %100 = load ptr, ptr %10, align 8, !tbaa !5
  call void @free(ptr noundef %100) #5
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %137

101:                                              ; preds = %95
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #5
  store i64 0, ptr %18, align 8, !tbaa !9
  br label %102

102:                                              ; preds = %122, %101
  %103 = load i64, ptr %18, align 8, !tbaa !9
  %104 = load i8, ptr %17, align 1, !tbaa !11
  %105 = zext i8 %104 to i64
  %106 = icmp ult i64 %103, %105
  br i1 %106, label %107, label %125

107:                                              ; preds = %102
  %108 = load ptr, ptr %10, align 8, !tbaa !5
  %109 = load i64, ptr %8, align 8, !tbaa !9
  %110 = sub i64 %109, 1
  %111 = load i64, ptr %18, align 8, !tbaa !9
  %112 = sub i64 %110, %111
  %113 = getelementptr inbounds i8, ptr %108, i64 %112
  %114 = load i8, ptr %113, align 1, !tbaa !11
  %115 = zext i8 %114 to i32
  %116 = load i8, ptr %17, align 1, !tbaa !11
  %117 = zext i8 %116 to i32
  %118 = icmp ne i32 %115, %117
  br i1 %118, label %119, label %121

119:                                              ; preds = %107
  %120 = load ptr, ptr %10, align 8, !tbaa !5
  call void @free(ptr noundef %120) #5
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %136

121:                                              ; preds = %107
  br label %122

122:                                              ; preds = %121
  %123 = load i64, ptr %18, align 8, !tbaa !9
  %124 = add i64 %123, 1
  store i64 %124, ptr %18, align 8, !tbaa !9
  br label %102, !llvm.loop !15

125:                                              ; preds = %102
  %126 = load i64, ptr %8, align 8, !tbaa !9
  %127 = load i8, ptr %17, align 1, !tbaa !11
  %128 = zext i8 %127 to i64
  %129 = sub i64 %126, %128
  %130 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %129, ptr %130, align 8, !tbaa !9
  %131 = load ptr, ptr %10, align 8, !tbaa !5
  %132 = load ptr, ptr %7, align 8, !tbaa !5
  %133 = load i64, ptr %132, align 8, !tbaa !9
  %134 = getelementptr inbounds i8, ptr %131, i64 %133
  store i8 0, ptr %134, align 1, !tbaa !11
  %135 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %135, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %136

136:                                              ; preds = %125, %119
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #5
  br label %137

137:                                              ; preds = %136, %99
  call void @llvm.lifetime.end.p0(i64 1, ptr %17) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #5
  br label %138

138:                                              ; preds = %137, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %139

139:                                              ; preds = %138, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %140

140:                                              ; preds = %139, %28
  %141 = load ptr, ptr %4, align 8
  ret ptr %141
}

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

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #4 = { 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 #5 = { nounwind }
attributes #6 = { 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 = !{!"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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}


; 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: nounwind uwtable
declare dso_local void @XAES_DecryptBlock(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @XAES_Decrypt(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %1, null
  %5 = icmp eq ptr %2, null
  %or.cond = or i1 %4, %5
  br i1 %or.cond, label %38, label %6

6:                                                ; preds = %3
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %.not = icmp ne i64 %7, 0
  %8 = and i64 %7, 15
  %.not59 = icmp eq i64 %8, 0
  %or.cond63 = and i1 %.not, %.not59
  br i1 %or.cond63, label %9, label %38

9:                                                ; preds = %6
  %10 = or disjoint i64 %7, 1
  %11 = tail call noalias ptr @malloc(i64 noundef %10) #3
  %.not60 = icmp eq ptr %11, null
  br i1 %.not60, label %38, label %12

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %0, i64 512
  %14 = load <16 x i8>, ptr %13, align 8
  br label %15

15:                                               ; preds = %12, %15
  %.04968 = phi i64 [ %7, %12 ], [ %20, %15 ]
  %.05067 = phi ptr [ %11, %12 ], [ %21, %15 ]
  %.05166 = phi ptr [ %1, %12 ], [ %22, %15 ]
  %16 = phi <16 x i8> [ %14, %12 ], [ %17, %15 ]
  %17 = load <16 x i8>, ptr %.05166, align 1
  tail call void @XAES_DecryptBlock(ptr noundef %0, ptr noundef nonnull %.05067, ptr noundef nonnull %.05166)
  %18 = load <16 x i8>, ptr %.05067, align 1, !tbaa !9
  %19 = xor <16 x i8> %18, %16
  store <16 x i8> %19, ptr %.05067, align 1, !tbaa !9
  %20 = add i64 %.04968, -16
  %21 = getelementptr inbounds i8, ptr %.05067, i64 16
  %22 = getelementptr inbounds i8, ptr %.05166, i64 16
  %.not61 = icmp eq i64 %20, 0
  br i1 %.not61, label %23, label %15, !llvm.loop !10

23:                                               ; preds = %15
  %24 = add i64 %7, -1
  %25 = getelementptr inbounds i8, ptr %11, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = add i8 %26, -17
  %or.cond4 = icmp ult i8 %27, -16
  br i1 %or.cond4, label %29, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %23
  %28 = zext nneg i8 %26 to i64
  br label %.lr.ph

29:                                               ; preds = %23
  tail call void @free(ptr noundef nonnull %11) #4
  br label %38

30:                                               ; preds = %.lr.ph
  %31 = add nuw nsw i64 %.069, 1
  %exitcond.not = icmp eq i64 %31, %28
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

.lr.ph:                                           ; preds = %.lr.ph.preheader, %30
  %.069 = phi i64 [ %31, %30 ], [ 0, %.lr.ph.preheader ]
  %32 = sub i64 %24, %.069
  %33 = getelementptr inbounds i8, ptr %11, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !9
  %.not62 = icmp eq i8 %34, %26
  br i1 %.not62, label %30, label %35

35:                                               ; preds = %.lr.ph
  tail call void @free(ptr noundef nonnull %11) #4
  br label %38

._crit_edge:                                      ; preds = %30
  %36 = sub i64 %7, %28
  store i64 %36, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %11, i64 %36
  store i8 0, ptr %37, align 1, !tbaa !9
  br label %38

38:                                               ; preds = %29, %._crit_edge, %35, %9, %3, %6
  %.4 = phi ptr [ null, %6 ], [ null, %3 ], [ null, %9 ], [ null, %29 ], [ null, %35 ], [ %11, %._crit_edge ]
  ret ptr %.4
}

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

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 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 #2 = { 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 #3 = { nounwind allocsize(0) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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