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

; 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.cwc_ctx = type { [4 x i32], [4 x i32], [4 x i32], [1 x %struct.aes_crypt_ctx], [4 x i32], [8 x i32], i32, i32, i32, [4 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

; 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 @cwc_init_and_key(ptr noundef %0, i64 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca [4 x i32], align 16
  %9 = 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 16, ptr %8) #6
  %10 = load i64, ptr %6, align 8, !tbaa !9
  %11 = icmp ne i64 %10, 16
  br i1 %11, label %12, label %19

12:                                               ; preds = %3
  %13 = load i64, ptr %6, align 8, !tbaa !9
  %14 = icmp ne i64 %13, 24
  br i1 %14, label %15, label %19

15:                                               ; preds = %12
  %16 = load i64, ptr %6, align 8, !tbaa !9
  %17 = icmp ne i64 %16, 32
  br i1 %17, label %18, label %19

18:                                               ; preds = %15
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %49

19:                                               ; preds = %15, %12, %3
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 16 %20, i8 0, i64 368, i1 false)
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load i64, ptr %6, align 8, !tbaa !9
  %23 = trunc i64 %22 to i32
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.cwc_ctx, ptr %24, i32 0, i32 3
  %26 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %25, i64 0, i64 0
  %27 = call i32 @aes_encrypt_key(ptr noundef %21, i32 noundef %23, ptr noundef %26)
  %28 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %28, i8 0, i64 16, i1 false)
  %29 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 0
  %30 = getelementptr inbounds i8, ptr %29, i64 0
  store i8 -64, ptr %30, align 16, !tbaa !11
  %31 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 0
  %32 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 0
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.cwc_ctx, ptr %33, i32 0, i32 3
  %35 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %34, i64 0, i64 0
  %36 = call i32 @aes_encrypt(ptr noundef %31, ptr noundef %32, ptr noundef %35)
  %37 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 0
  %38 = getelementptr inbounds i8, ptr %37, i64 0
  %39 = load i8, ptr %38, align 16, !tbaa !11
  %40 = zext i8 %39 to i32
  %41 = and i32 %40, 127
  %42 = trunc i32 %41 to i8
  store i8 %42, ptr %38, align 16, !tbaa !11
  %43 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 0
  %44 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 0
  call void @bswap32_block(ptr noundef %43, ptr noundef %44)
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.cwc_ctx, ptr %45, i32 0, i32 4
  %47 = getelementptr inbounds [4 x i32], ptr %46, i64 0, i64 0
  %48 = getelementptr inbounds [4 x i32], ptr %8, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %47, ptr align 16 %48, i64 16, i1 false)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %49

49:                                               ; preds = %19, %18
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #6
  %50 = load i32, ptr %4, align 4
  ret i32 %50
}

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

declare i32 @aes_encrypt_key(ptr noundef, i32 noundef, ptr noundef) #3

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @bswap32_block(ptr noundef, ptr noundef) #4

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { "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 = { inlinehint 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 #5 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { 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 = !{!7, !7, i64 0}


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

; Function Attrs: mustprogress 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 range(i32 -1, 1) i32 @cwc_init_and_key(ptr noundef %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = alloca [4 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #6
  switch i64 %1, label %13 [
    i64 32, label %5
    i64 24, label %5
    i64 16, label %5
  ]

5:                                                ; preds = %3, %3, %3
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(368) %2, i8 0, i64 368, i1 false)
  %6 = trunc nuw i64 %1 to i32
  %7 = getelementptr inbounds i8, ptr %2, i64 48
  %8 = tail call i32 @aes_encrypt_key(ptr noundef %0, i32 noundef %6, ptr noundef nonnull %7) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  store i8 -64, ptr %4, align 16, !tbaa !5
  %9 = call i32 @aes_encrypt(ptr noundef nonnull %4, ptr noundef nonnull %4, ptr noundef nonnull %7) #6
  %10 = load i8, ptr %4, align 16, !tbaa !5
  %11 = and i8 %10, 127
  store i8 %11, ptr %4, align 16, !tbaa !5
  call void @bswap32_block(ptr noundef nonnull %4, ptr noundef nonnull %4)
  %12 = getelementptr inbounds i8, ptr %2, i64 304
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %12, ptr noundef nonnull align 16 dereferenceable(16) %4, i64 16, i1 false)
  br label %13

13:                                               ; preds = %3, %5
  %.0 = phi i32 [ 0, %5 ], [ -1, %3 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #6
  ret i32 %.0
}

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

declare i32 @aes_encrypt_key(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #3

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @bswap32_block(ptr noundef, ptr noundef) local_unnamed_addr #4

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

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass