; LLVM IR from square/subzero :: core/external-crypto/modes/ltc (ltc_omac)

; 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.omac_state = type { [1 x %struct.aes_crypt_ctx], [16 x i8], [16 x i8], [2 x [16 x i8]], i32, [12 x i8] }
%struct.aes_crypt_ctx = type { [60 x i32], %union.aes_inf, [12 x i8] }
%union.aes_inf = type { i32 }

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

declare i32 @aes_encrypt(ptr noundef, ptr noundef, ptr noundef) #1

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

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

15:                                               ; preds = %3
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.omac_state, ptr %16, i32 0, i32 4
  %18 = load i32, ptr %17, align 16, !tbaa !11
  %19 = icmp ugt i32 %18, 16
  br i1 %19, label %20, label %21

20:                                               ; preds = %15, %3
  store i32 1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %110

21:                                               ; preds = %15
  br label %22

22:                                               ; preds = %84, %21
  %23 = load i64, ptr %6, align 8, !tbaa !9
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %109

25:                                               ; preds = %22
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.omac_state, ptr %26, i32 0, i32 4
  %28 = load i32, ptr %27, align 16, !tbaa !11
  %29 = icmp eq i32 %28, 16
  br i1 %29, label %30, label %68

30:                                               ; preds = %25
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %49, %30
  %32 = load i64, ptr %9, align 8, !tbaa !9
  %33 = icmp ult i64 %32, 16
  br i1 %33, label %34, label %52

34:                                               ; preds = %31
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.omac_state, ptr %35, i32 0, i32 2
  %37 = load i64, ptr %9, align 8, !tbaa !9
  %38 = getelementptr inbounds [16 x i8], ptr %36, i64 0, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !14
  %40 = zext i8 %39 to i32
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.omac_state, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = getelementptr inbounds [16 x i8], ptr %42, i64 0, i64 %43
  %45 = load i8, ptr %44, align 1, !tbaa !14
  %46 = zext i8 %45 to i32
  %47 = xor i32 %46, %40
  %48 = trunc i32 %47 to i8
  store i8 %48, ptr %44, align 1, !tbaa !14
  br label %49

49:                                               ; preds = %34
  %50 = load i64, ptr %9, align 8, !tbaa !9
  %51 = add i64 %50, 1
  store i64 %51, ptr %9, align 8, !tbaa !9
  br label %31, !llvm.loop !15

52:                                               ; preds = %31
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.omac_state, ptr %53, i32 0, i32 1
  %55 = getelementptr inbounds [16 x i8], ptr %54, i64 0, i64 0
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.omac_state, ptr %56, i32 0, i32 2
  %58 = getelementptr inbounds [16 x i8], ptr %57, i64 0, i64 0
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.omac_state, ptr %59, i32 0, i32 0
  %61 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %60, i64 0, i64 0
  %62 = call i32 @aes_encrypt(ptr noundef %55, ptr noundef %58, ptr noundef %61)
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %65

64:                                               ; preds = %52
  store i32 1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %110

65:                                               ; preds = %52
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.omac_state, ptr %66, i32 0, i32 4
  store i32 0, ptr %67, align 16, !tbaa !11
  br label %68

68:                                               ; preds = %65, %25
  %69 = load i64, ptr %6, align 8, !tbaa !9
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.omac_state, ptr %70, i32 0, i32 4
  %72 = load i32, ptr %71, align 16, !tbaa !11
  %73 = sub i32 16, %72
  %74 = zext i32 %73 to i64
  %75 = icmp ult i64 %69, %74
  br i1 %75, label %76, label %78

76:                                               ; preds = %68
  %77 = load i64, ptr %6, align 8, !tbaa !9
  br label %84

78:                                               ; preds = %68
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.omac_state, ptr %79, i32 0, i32 4
  %81 = load i32, ptr %80, align 16, !tbaa !11
  %82 = sub i32 16, %81
  %83 = zext i32 %82 to i64
  br label %84

84:                                               ; preds = %78, %76
  %85 = phi i64 [ %77, %76 ], [ %83, %78 ]
  store i64 %85, ptr %8, align 8, !tbaa !9
  %86 = load ptr, ptr %7, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.omac_state, ptr %86, i32 0, i32 1
  %88 = getelementptr inbounds [16 x i8], ptr %87, i64 0, i64 0
  %89 = load ptr, ptr %7, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.omac_state, ptr %89, i32 0, i32 4
  %91 = load i32, ptr %90, align 16, !tbaa !11
  %92 = zext i32 %91 to i64
  %93 = getelementptr inbounds i8, ptr %88, i64 %92
  %94 = load ptr, ptr %5, align 8, !tbaa !5
  %95 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %93, ptr align 1 %94, i64 %95, i1 false)
  %96 = load i64, ptr %8, align 8, !tbaa !9
  %97 = load ptr, ptr %7, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.omac_state, ptr %97, i32 0, i32 4
  %99 = load i32, ptr %98, align 16, !tbaa !11
  %100 = zext i32 %99 to i64
  %101 = add i64 %100, %96
  %102 = trunc i64 %101 to i32
  store i32 %102, ptr %98, align 16, !tbaa !11
  %103 = load i64, ptr %8, align 8, !tbaa !9
  %104 = load i64, ptr %6, align 8, !tbaa !9
  %105 = sub i64 %104, %103
  store i64 %105, ptr %6, align 8, !tbaa !9
  %106 = load i64, ptr %8, align 8, !tbaa !9
  %107 = load ptr, ptr %5, align 8, !tbaa !5
  %108 = getelementptr inbounds i8, ptr %107, i64 %106
  store ptr %108, ptr %5, align 8, !tbaa !5
  br label %22, !llvm.loop !17

109:                                              ; preds = %22
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %110

110:                                              ; preds = %109, %64, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %111 = load i32, ptr %4, align 4
  ret i32 %111
}

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

!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 = !{!12, !13, i64 320}
!12 = !{!"", !7, i64 0, !7, i64 256, !7, i64 272, !7, i64 288, !13, i64 320}
!13 = !{!"int", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

declare i32 @aes_encrypt(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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
define dso_local range(i32 0, 2) i32 @omac_process(ptr nocapture noundef readonly %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %2, i64 320
  %5 = load i32, ptr %4, align 16, !tbaa !5
  %6 = icmp ugt i32 %5, 16
  br i1 %6, label %.loopexit, label %.preheader33

.preheader33:                                     ; preds = %3
  %7 = getelementptr inbounds i8, ptr %2, i64 256
  %8 = getelementptr inbounds i8, ptr %2, i64 272
  %.not35 = icmp eq i64 %1, 0
  br i1 %.not35, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader33, %16
  %9 = phi i32 [ %24, %16 ], [ %5, %.preheader33 ]
  %.02937 = phi ptr [ %26, %16 ], [ %0, %.preheader33 ]
  %.03036 = phi i64 [ %25, %16 ], [ %1, %.preheader33 ]
  %10 = icmp eq i32 %9, 16
  br i1 %10, label %.preheader.preheader, label %16

.preheader.preheader:                             ; preds = %.lr.ph
  %11 = load <16 x i8>, ptr %8, align 1, !tbaa !10
  %12 = load <16 x i8>, ptr %7, align 1, !tbaa !10
  %13 = xor <16 x i8> %12, %11
  store <16 x i8> %13, ptr %7, align 1, !tbaa !10
  %14 = tail call i32 @aes_encrypt(ptr noundef nonnull %7, ptr noundef nonnull %8, ptr noundef nonnull %2) #4
  %.not32 = icmp eq i32 %14, 0
  br i1 %.not32, label %15, label %.loopexit

15:                                               ; preds = %.preheader.preheader
  store i32 0, ptr %4, align 16, !tbaa !5
  br label %16

16:                                               ; preds = %15, %.lr.ph
  %17 = phi i32 [ 0, %15 ], [ %9, %.lr.ph ]
  %18 = sub i32 16, %17
  %19 = zext i32 %18 to i64
  %.030. = tail call i64 @llvm.umin.i64(i64 %.03036, i64 %19)
  %20 = zext i32 %17 to i64
  %21 = getelementptr inbounds i8, ptr %7, i64 %20
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %21, ptr align 1 %.02937, i64 %.030., i1 false)
  %22 = load i32, ptr %4, align 16, !tbaa !5
  %23 = trunc nuw i64 %.030. to i32
  %24 = add i32 %22, %23
  store i32 %24, ptr %4, align 16, !tbaa !5
  %25 = sub i64 %.03036, %.030.
  %26 = getelementptr inbounds i8, ptr %.02937, i64 %.030.
  %.not = icmp eq i64 %25, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %.preheader.preheader, %16, %.preheader33, %3
  %.028 = phi i32 [ 1, %3 ], [ 0, %.preheader33 ], [ 1, %.preheader.preheader ], [ 0, %16 ]
  ret i32 %.028
}

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

attributes #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 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !9, i64 320}
!6 = !{!"", !7, i64 0, !7, i64 256, !7, i64 272, !7, i64 288, !9, i64 320}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!7, !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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