; 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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @XAES_Encrypt(ptr noundef %0, ptr noundef %1, ptr noundef %2) #3 {
  %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 i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca [16 x i8], align 16
  %13 = alloca i8, align 1
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = 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
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = icmp eq ptr %17, null
  br i1 %18, label %26, label %19

19:                                               ; preds = %3
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = icmp eq ptr %20, null
  br i1 %21, label %26, label %22

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

26:                                               ; preds = %22, %19, %3
  store ptr null, ptr %4, align 8
  br label %104

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = load i64, ptr %28, align 8, !tbaa !9
  %30 = udiv i64 %29, 16
  %31 = add i64 %30, 1
  %32 = mul i64 %31, 16
  store i64 %32, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = load i64, ptr %33, align 8, !tbaa !9
  store i64 %34, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %36 = add i64 %35, 1
  %37 = call noalias ptr @malloc(i64 noundef %36) #6
  store ptr %37, ptr %10, align 8, !tbaa !5
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %41, label %40

40:                                               ; preds = %27
  store ptr null, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %103

41:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #5
  %42 = getelementptr inbounds [16 x i8], ptr %12, i64 0, i64 0
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.AESContext, ptr %43, i32 0, i32 2
  %45 = getelementptr inbounds [16 x i8], ptr %44, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %42, ptr align 8 %45, i64 16, i1 false)
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load i64, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %46, ptr align 1 %47, i64 %48, i1 false)
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #5
  %49 = load i64, ptr %8, align 8, !tbaa !9
  %50 = load i64, ptr %9, align 8, !tbaa !9
  %51 = sub i64 %49, %50
  %52 = trunc i64 %51 to i8
  store i8 %52, ptr %13, align 1, !tbaa !11
  %53 = load ptr, ptr %10, align 8, !tbaa !5
  %54 = load i64, ptr %9, align 8, !tbaa !9
  %55 = getelementptr inbounds i8, ptr %53, i64 %54
  %56 = load i8, ptr %13, align 1, !tbaa !11
  %57 = zext i8 %56 to i32
  %58 = trunc i32 %57 to i8
  %59 = load i8, ptr %13, align 1, !tbaa !11
  %60 = zext i8 %59 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %55, i8 %58, i64 %60, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %61, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %62 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %62, ptr %15, align 8, !tbaa !9
  br label %63

63:                                               ; preds = %85, %41
  %64 = load i64, ptr %15, align 8, !tbaa !9
  %65 = icmp ugt i64 %64, 0
  br i1 %65, label %66, label %95

66:                                               ; preds = %63
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #5
  store i64 0, ptr %16, align 8, !tbaa !9
  br label %67

67:                                               ; preds = %82, %66
  %68 = load i64, ptr %16, align 8, !tbaa !9
  %69 = icmp ult i64 %68, 16
  br i1 %69, label %70, label %85

70:                                               ; preds = %67
  %71 = load i64, ptr %16, align 8, !tbaa !9
  %72 = getelementptr inbounds [16 x i8], ptr %12, i64 0, i64 %71
  %73 = load i8, ptr %72, align 1, !tbaa !11
  %74 = zext i8 %73 to i32
  %75 = load ptr, ptr %14, align 8, !tbaa !5
  %76 = load i64, ptr %16, align 8, !tbaa !9
  %77 = getelementptr inbounds i8, ptr %75, i64 %76
  %78 = load i8, ptr %77, align 1, !tbaa !11
  %79 = zext i8 %78 to i32
  %80 = xor i32 %79, %74
  %81 = trunc i32 %80 to i8
  store i8 %81, ptr %77, align 1, !tbaa !11
  br label %82

82:                                               ; preds = %70
  %83 = load i64, ptr %16, align 8, !tbaa !9
  %84 = add i64 %83, 1
  store i64 %84, ptr %16, align 8, !tbaa !9
  br label %67, !llvm.loop !12

85:                                               ; preds = %67
  %86 = load ptr, ptr %5, align 8, !tbaa !5
  %87 = load ptr, ptr %14, align 8, !tbaa !5
  %88 = load ptr, ptr %14, align 8, !tbaa !5
  call void @XAES_EncryptBlock(ptr noundef %86, ptr noundef %87, ptr noundef %88)
  %89 = getelementptr inbounds [16 x i8], ptr %12, i64 0, i64 0
  %90 = load ptr, ptr %14, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %89, ptr align 1 %90, i64 16, i1 false)
  %91 = load i64, ptr %15, align 8, !tbaa !9
  %92 = sub i64 %91, 16
  store i64 %92, ptr %15, align 8, !tbaa !9
  %93 = load ptr, ptr %14, align 8, !tbaa !5
  %94 = getelementptr inbounds i8, ptr %93, i64 16
  store ptr %94, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #5
  br label %63, !llvm.loop !14

95:                                               ; preds = %63
  %96 = load i64, ptr %8, align 8, !tbaa !9
  %97 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %96, ptr %97, align 8, !tbaa !9
  %98 = load ptr, ptr %10, align 8, !tbaa !5
  %99 = load ptr, ptr %7, align 8, !tbaa !5
  %100 = load i64, ptr %99, align 8, !tbaa !9
  %101 = getelementptr inbounds i8, ptr %98, i64 %100
  store i8 0, ptr %101, align 1, !tbaa !11
  %102 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %102, ptr %4, align 8
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #5
  br label %103

103:                                              ; preds = %95, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %104

104:                                              ; preds = %103, %26
  %105 = load ptr, ptr %4, align 8
  ret ptr %105
}

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

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


; 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 nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

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

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

6:                                                ; preds = %3
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %27, label %8

8:                                                ; preds = %6
  %9 = and i64 %7, -16
  %10 = add i64 %9, 16
  %11 = add i64 %9, 17
  %12 = tail call noalias ptr @malloc(i64 noundef %11) #4
  %.not44 = icmp eq ptr %12, null
  br i1 %.not44, label %27, label %13

13:                                               ; preds = %8
  %14 = getelementptr inbounds i8, ptr %0, i64 512
  %15 = load <16 x i8>, ptr %14, align 8
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %12, ptr nonnull align 1 %1, i64 %7, i1 false)
  %16 = sub i64 %10, %7
  %17 = trunc i64 %16 to i8
  %18 = getelementptr inbounds i8, ptr %12, i64 %7
  %19 = and i64 %16, 255
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %18, i8 %17, i64 %19, i1 false)
  %.not4547 = icmp eq i64 %10, 0
  br i1 %.not4547, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %13, %.preheader
  %.03749 = phi i64 [ %24, %.preheader ], [ %10, %13 ]
  %.03848 = phi ptr [ %25, %.preheader ], [ %12, %13 ]
  %20 = phi <16 x i8> [ %23, %.preheader ], [ %15, %13 ]
  %21 = load <16 x i8>, ptr %.03848, align 1, !tbaa !9
  %22 = xor <16 x i8> %21, %20
  store <16 x i8> %22, ptr %.03848, align 1, !tbaa !9
  tail call void @XAES_EncryptBlock(ptr noundef %0, ptr noundef nonnull %.03848, ptr noundef nonnull %.03848)
  %23 = load <16 x i8>, ptr %.03848, align 1
  %24 = add i64 %.03749, -16
  %25 = getelementptr inbounds i8, ptr %.03848, i64 16
  %.not45 = icmp eq i64 %24, 0
  br i1 %.not45, label %._crit_edge, label %.preheader, !llvm.loop !10

._crit_edge:                                      ; preds = %.preheader, %13
  store i64 %10, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %12, i64 %10
  store i8 0, ptr %26, align 1, !tbaa !9
  br label %27

27:                                               ; preds = %._crit_edge, %8, %3, %6
  %.1 = phi ptr [ null, %6 ], [ null, %3 ], [ %12, %._crit_edge ], [ null, %8 ]
  ret ptr %.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 #3

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { mustprogress nocallback nofree 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 = { 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 #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 = !{!"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"}


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