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

; 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.eax_ctx = type { [2 x i64], [2 x i64], [2 x i64], [2 x i64], [2 x i64], [4 x i64], [1 x %struct.aes_crypt_ctx], i32, i32, i32 }
%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

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

declare i32 @aes_encrypt(ptr noundef, ptr noundef, ptr noundef) #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 @eax_init_message(ptr noundef %0, i64 noundef %1, ptr noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 0, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.eax_ctx, ptr %10, i32 0, i32 4
  %12 = getelementptr inbounds [2 x i64], ptr %11, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %12, i8 0, i64 16, i1 false)
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.eax_ctx, ptr %13, i32 0, i32 2
  %15 = getelementptr inbounds [2 x i64], ptr %14, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %15, i8 0, i64 16, i1 false)
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.eax_ctx, ptr %16, i32 0, i32 3
  %18 = getelementptr inbounds [2 x i64], ptr %17, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %18, i8 0, i64 16, i1 false)
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.eax_ctx, ptr %19, i32 0, i32 2
  %21 = getelementptr inbounds [2 x i64], ptr %20, i64 0, i64 0
  %22 = getelementptr inbounds i8, ptr %21, i64 15
  store i8 1, ptr %22, align 1, !tbaa !13
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.eax_ctx, ptr %23, i32 0, i32 7
  store i32 0, ptr %24, align 16, !tbaa !14
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.eax_ctx, ptr %25, i32 0, i32 3
  %27 = getelementptr inbounds [2 x i64], ptr %26, i64 0, i64 0
  %28 = getelementptr inbounds i8, ptr %27, i64 15
  store i8 2, ptr %28, align 1, !tbaa !13
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.eax_ctx, ptr %29, i32 0, i32 8
  store i32 0, ptr %30, align 4, !tbaa !16
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.eax_ctx, ptr %31, i32 0, i32 9
  store i32 0, ptr %32, align 8, !tbaa !17
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %118

35:                                               ; preds = %3
  store i32 16, ptr %8, align 4, !tbaa !11
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %36

36:                                               ; preds = %55, %35
  %37 = load i32, ptr %7, align 4, !tbaa !11
  %38 = zext i32 %37 to i64
  %39 = load i64, ptr %5, align 8, !tbaa !9
  %40 = icmp ult i64 %38, %39
  br i1 %40, label %41, label %74

41:                                               ; preds = %36
  %42 = load i32, ptr %8, align 4, !tbaa !11
  %43 = icmp eq i32 %42, 16
  br i1 %43, label %44, label %55

44:                                               ; preds = %41
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.eax_ctx, ptr %45, i32 0, i32 4
  %47 = getelementptr inbounds [2 x i64], ptr %46, i64 0, i64 0
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.eax_ctx, ptr %48, i32 0, i32 4
  %50 = getelementptr inbounds [2 x i64], ptr %49, i64 0, i64 0
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.eax_ctx, ptr %51, i32 0, i32 6
  %53 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %52, i64 0, i64 0
  %54 = call i32 @aes_encrypt(ptr noundef %47, ptr noundef %50, ptr noundef %53)
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %55

55:                                               ; preds = %44, %41
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = load i32, ptr %7, align 4, !tbaa !11
  %58 = add i32 %57, 1
  store i32 %58, ptr %7, align 4, !tbaa !11
  %59 = zext i32 %57 to i64
  %60 = getelementptr inbounds i8, ptr %56, i64 %59
  %61 = load i8, ptr %60, align 1, !tbaa !13
  %62 = zext i8 %61 to i32
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.eax_ctx, ptr %63, i32 0, i32 4
  %65 = getelementptr inbounds [2 x i64], ptr %64, i64 0, i64 0
  %66 = load i32, ptr %8, align 4, !tbaa !11
  %67 = add i32 %66, 1
  store i32 %67, ptr %8, align 4, !tbaa !11
  %68 = zext i32 %66 to i64
  %69 = getelementptr inbounds i8, ptr %65, i64 %68
  %70 = load i8, ptr %69, align 1, !tbaa !13
  %71 = zext i8 %70 to i32
  %72 = xor i32 %71, %62
  %73 = trunc i32 %72 to i8
  store i8 %73, ptr %69, align 1, !tbaa !13
  br label %36, !llvm.loop !18

74:                                               ; preds = %36
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.eax_ctx, ptr %75, i32 0, i32 5
  %77 = getelementptr inbounds [4 x i64], ptr %76, i64 0, i64 0
  store ptr %77, ptr %9, align 8, !tbaa !5
  %78 = load i32, ptr %8, align 4, !tbaa !11
  %79 = icmp ult i32 %78, 16
  br i1 %79, label %80, label %93

80:                                               ; preds = %74
  %81 = load ptr, ptr %6, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.eax_ctx, ptr %81, i32 0, i32 4
  %83 = getelementptr inbounds [2 x i64], ptr %82, i64 0, i64 0
  %84 = load i32, ptr %8, align 4, !tbaa !11
  %85 = zext i32 %84 to i64
  %86 = getelementptr inbounds i8, ptr %83, i64 %85
  %87 = load i8, ptr %86, align 1, !tbaa !13
  %88 = zext i8 %87 to i32
  %89 = xor i32 %88, 128
  %90 = trunc i32 %89 to i8
  store i8 %90, ptr %86, align 1, !tbaa !13
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = getelementptr inbounds i8, ptr %91, i64 16
  store ptr %92, ptr %9, align 8, !tbaa !5
  br label %93

93:                                               ; preds = %80, %74
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %94

94:                                               ; preds = %114, %93
  %95 = load i32, ptr %7, align 4, !tbaa !11
  %96 = icmp ult i32 %95, 16
  br i1 %96, label %97, label %117

97:                                               ; preds = %94
  %98 = load ptr, ptr %9, align 8, !tbaa !5
  %99 = load i32, ptr %7, align 4, !tbaa !11
  %100 = zext i32 %99 to i64
  %101 = getelementptr inbounds i8, ptr %98, i64 %100
  %102 = load i8, ptr %101, align 1, !tbaa !13
  %103 = zext i8 %102 to i32
  %104 = load ptr, ptr %6, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.eax_ctx, ptr %104, i32 0, i32 4
  %106 = getelementptr inbounds [2 x i64], ptr %105, i64 0, i64 0
  %107 = load i32, ptr %7, align 4, !tbaa !11
  %108 = zext i32 %107 to i64
  %109 = getelementptr inbounds i8, ptr %106, i64 %108
  %110 = load i8, ptr %109, align 1, !tbaa !13
  %111 = zext i8 %110 to i32
  %112 = xor i32 %111, %103
  %113 = trunc i32 %112 to i8
  store i8 %113, ptr %109, align 1, !tbaa !13
  br label %114

114:                                              ; preds = %97
  %115 = load i32, ptr %7, align 4, !tbaa !11
  %116 = add i32 %115, 1
  store i32 %116, ptr %7, align 4, !tbaa !11
  br label %94, !llvm.loop !20

117:                                              ; preds = %94
  br label %125

118:                                              ; preds = %3
  %119 = load ptr, ptr %6, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.eax_ctx, ptr %119, i32 0, i32 4
  %121 = getelementptr inbounds [2 x i64], ptr %120, i64 0, i64 0
  %122 = load ptr, ptr %6, align 8, !tbaa !5
  %123 = getelementptr inbounds %struct.eax_ctx, ptr %122, i32 0, i32 5
  %124 = getelementptr inbounds [4 x i64], ptr %123, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %121, ptr align 16 %124, i64 16, i1 false)
  br label %125

125:                                              ; preds = %118, %117
  %126 = load ptr, ptr %6, align 8, !tbaa !5
  %127 = getelementptr inbounds %struct.eax_ctx, ptr %126, i32 0, i32 4
  %128 = getelementptr inbounds [2 x i64], ptr %127, i64 0, i64 0
  %129 = load ptr, ptr %6, align 8, !tbaa !5
  %130 = getelementptr inbounds %struct.eax_ctx, ptr %129, i32 0, i32 4
  %131 = getelementptr inbounds [2 x i64], ptr %130, i64 0, i64 0
  %132 = load ptr, ptr %6, align 8, !tbaa !5
  %133 = getelementptr inbounds %struct.eax_ctx, ptr %132, i32 0, i32 6
  %134 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %133, i64 0, i64 0
  %135 = call i32 @aes_encrypt(ptr noundef %128, ptr noundef %131, ptr noundef %134)
  %136 = load ptr, ptr %6, align 8, !tbaa !5
  %137 = getelementptr inbounds %struct.eax_ctx, ptr %136, i32 0, i32 0
  %138 = getelementptr inbounds [2 x i64], ptr %137, i64 0, i64 0
  %139 = load ptr, ptr %6, align 8, !tbaa !5
  %140 = getelementptr inbounds %struct.eax_ctx, ptr %139, i32 0, i32 4
  %141 = getelementptr inbounds [2 x i64], ptr %140, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %138, ptr align 16 %141, i64 16, i1 false)
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  ret i32 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) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { "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 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !12, i64 368}
!15 = !{!"", !7, i64 0, !7, i64 16, !7, i64 32, !7, i64 48, !7, i64 64, !7, i64 80, !7, i64 112, !12, i64 368, !12, i64 372, !12, i64 376}
!16 = !{!15, !12, i64 372}
!17 = !{!15, !12, i64 376}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


; 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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @eax_init_message(ptr nocapture noundef readonly %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %2, i64 64
  %5 = getelementptr inbounds i8, ptr %2, i64 32
  %6 = getelementptr inbounds i8, ptr %2, i64 47
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(48) %5, i8 0, i64 48, i1 false)
  store i8 1, ptr %6, align 1, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %2, i64 368
  store i32 0, ptr %7, align 16, !tbaa !8
  %8 = getelementptr inbounds i8, ptr %2, i64 63
  store i8 2, ptr %8, align 1, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %2, i64 372
  store i32 0, ptr %9, align 4, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %2, i64 376
  store i32 0, ptr %10, align 8, !tbaa !12
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %40, label %.preheader

.preheader:                                       ; preds = %3
  %11 = getelementptr inbounds i8, ptr %2, i64 112
  br label %12

12:                                               ; preds = %.preheader, %17
  %13 = phi i64 [ 0, %.preheader ], [ %26, %17 ]
  %.03642 = phi i32 [ 16, %.preheader ], [ %21, %17 ]
  %.03741 = phi i32 [ 0, %.preheader ], [ %18, %17 ]
  %14 = icmp eq i32 %.03642, 16
  br i1 %14, label %15, label %17

15:                                               ; preds = %12
  %16 = tail call i32 @aes_encrypt(ptr noundef nonnull %4, ptr noundef nonnull %4, ptr noundef nonnull %11) #4
  br label %17

17:                                               ; preds = %15, %12
  %.1 = phi i32 [ 0, %15 ], [ %.03642, %12 ]
  %18 = add i32 %.03741, 1
  %19 = getelementptr inbounds i8, ptr %0, i64 %13
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %21 = add nuw nsw i32 %.1, 1
  %22 = zext nneg i32 %.1 to i64
  %23 = getelementptr inbounds i8, ptr %4, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %25 = xor i8 %24, %20
  store i8 %25, ptr %23, align 1, !tbaa !5
  %26 = zext i32 %18 to i64
  %27 = icmp ult i64 %26, %1
  br i1 %27, label %12, label %28, !llvm.loop !13

28:                                               ; preds = %17
  %29 = icmp ult i32 %.1, 15
  br i1 %29, label %30, label %.loopexit.loopexit

30:                                               ; preds = %28
  %31 = zext nneg i32 %21 to i64
  %32 = getelementptr inbounds i8, ptr %4, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !5
  %34 = xor i8 %33, -128
  store i8 %34, ptr %32, align 1, !tbaa !5
  br label %.loopexit.loopexit

.loopexit.loopexit:                               ; preds = %30, %28
  %35 = phi i64 [ 96, %30 ], [ 80, %28 ]
  %36 = getelementptr inbounds i8, ptr %2, i64 %35
  %37 = load <16 x i8>, ptr %36, align 1, !tbaa !5
  %38 = load <16 x i8>, ptr %4, align 1, !tbaa !5
  %39 = xor <16 x i8> %38, %37
  store <16 x i8> %39, ptr %4, align 1, !tbaa !5
  br label %.loopexit

40:                                               ; preds = %3
  %41 = getelementptr inbounds i8, ptr %2, i64 80
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %4, ptr noundef nonnull align 16 dereferenceable(16) %41, i64 16, i1 false)
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %40
  %42 = getelementptr inbounds i8, ptr %2, i64 112
  %43 = tail call i32 @aes_encrypt(ptr noundef nonnull %4, ptr noundef nonnull %4, ptr noundef nonnull %42) #4
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %2, ptr noundef nonnull align 16 dereferenceable(16) %4, i64 16, i1 false)
  ret i32 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) #3

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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 nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 368}
!9 = !{!"", !6, i64 0, !6, i64 16, !6, i64 32, !6, i64 48, !6, i64 64, !6, i64 80, !6, i64 112, !10, i64 368, !10, i64 372, !10, i64 376}
!10 = !{!"int", !6, i64 0}
!11 = !{!9, !10, i64 372}
!12 = !{!9, !10, i64 376}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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