; LLVM IR from machinefi/psa-crypto-esp32 :: psa_layer (psa_crypto_cipher)

; 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.iotex_psa_cipher_operation_t = type { i32, i8, i8, %union.anon }
%union.anon = type { %struct.iotex_cipher_context_t }
%struct.iotex_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr, ptr }

; 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

declare i32 @iotex_to_psa_error(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @iotex_psa_cipher_update(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5) #2 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i64, align 8
  %16 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store i64 %2, ptr %10, align 8, !tbaa !9
  store ptr %3, ptr %11, align 8, !tbaa !5
  store i64 %4, ptr %12, align 8, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 -151, ptr %14, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !13
  %20 = and i32 %19, 2139095040
  %21 = icmp eq i32 %20, 75497472
  br i1 %21, label %39, label %22

22:                                               ; preds = %6
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %23, i32 0, i32 3
  %25 = getelementptr inbounds %struct.iotex_cipher_context_t, ptr %24, i32 0, i32 6
  %26 = load i64, ptr %25, align 8, !tbaa !15
  %27 = load i64, ptr %10, align 8, !tbaa !9
  %28 = add i64 %26, %27
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %29, i32 0, i32 2
  %31 = load i8, ptr %30, align 1, !tbaa !16
  %32 = zext i8 %31 to i64
  %33 = udiv i64 %28, %32
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %34, i32 0, i32 2
  %36 = load i8, ptr %35, align 1, !tbaa !16
  %37 = zext i8 %36 to i64
  %38 = mul i64 %33, %37
  store i64 %38, ptr %15, align 8, !tbaa !9
  br label %41

39:                                               ; preds = %6
  %40 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %40, ptr %15, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %39, %22
  %42 = load i64, ptr %12, align 8, !tbaa !9
  %43 = load i64, ptr %15, align 8, !tbaa !9
  %44 = icmp ult i64 %42, %43
  br i1 %44, label %45, label %46

45:                                               ; preds = %41
  store i32 -138, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %76

46:                                               ; preds = %41
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !13
  %50 = icmp eq i32 %49, 71320576
  br i1 %50, label %51, label %59

51:                                               ; preds = %46
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %52, i32 0, i32 3
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  %55 = load i64, ptr %10, align 8, !tbaa !9
  %56 = load ptr, ptr %11, align 8, !tbaa !5
  %57 = load ptr, ptr %13, align 8, !tbaa !5
  %58 = call i32 @psa_cipher_update_ecb(ptr noundef %53, ptr noundef %54, i64 noundef %55, ptr noundef %56, ptr noundef %57)
  store i32 %58, ptr %14, align 4, !tbaa !11
  br label %74

59:                                               ; preds = %46
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.iotex_psa_cipher_operation_t, ptr %60, i32 0, i32 3
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = load i64, ptr %10, align 8, !tbaa !9
  %64 = load ptr, ptr %11, align 8, !tbaa !5
  %65 = load ptr, ptr %13, align 8, !tbaa !5
  %66 = call i32 @iotex_cipher_update(ptr noundef %61, ptr noundef %62, i64 noundef %63, ptr noundef %64, ptr noundef %65)
  %67 = call i32 @iotex_to_psa_error(i32 noundef %66)
  store i32 %67, ptr %14, align 4, !tbaa !11
  %68 = load ptr, ptr %13, align 8, !tbaa !5
  %69 = load i64, ptr %68, align 8, !tbaa !9
  %70 = load i64, ptr %12, align 8, !tbaa !9
  %71 = icmp ugt i64 %69, %70
  br i1 %71, label %72, label %73

72:                                               ; preds = %59
  store i32 -151, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %76

73:                                               ; preds = %59
  br label %74

74:                                               ; preds = %73, %51
  %75 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %75, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %76

76:                                               ; preds = %74, %72, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  %77 = load i32, ptr %7, align 4
  ret i32 %77
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_cipher_update_ecb(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #2

declare i32 @iotex_cipher_update(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #1

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 = { 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 }

!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 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0, !7, i64 4, !7, i64 5, !7, i64 8}
!15 = !{!7, !7, i64 0}
!16 = !{!14, !7, i64 5}


; 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 @iotex_to_psa_error(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @iotex_psa_cipher_update(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5) local_unnamed_addr #1 {
  %7 = load i32, ptr %0, align 8, !tbaa !5
  %8 = and i32 %7, 2139095040
  %9 = icmp eq i32 %8, 75497472
  br i1 %9, label %19, label %10

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %0, i64 56
  %12 = load i64, ptr %11, align 8, !tbaa !10
  %.fr25 = freeze i64 %12
  %13 = add i64 %.fr25, %2
  %14 = getelementptr inbounds i8, ptr %0, i64 5
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = zext i8 %15 to i64
  %17 = urem i64 %13, %16
  %18 = sub nuw i64 %13, %17
  br label %19

19:                                               ; preds = %6, %10
  %.0 = phi i64 [ %18, %10 ], [ %2, %6 ]
  %20 = icmp ugt i64 %.0, %4
  br i1 %20, label %31, label %21

21:                                               ; preds = %19
  %22 = icmp eq i32 %7, 71320576
  %23 = getelementptr inbounds i8, ptr %0, i64 8
  br i1 %22, label %24, label %26

24:                                               ; preds = %21
  %25 = tail call i32 @psa_cipher_update_ecb(ptr noundef nonnull %23, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %5)
  br label %31

26:                                               ; preds = %21
  %27 = tail call i32 @iotex_cipher_update(ptr noundef nonnull %23, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %5) #2
  %28 = tail call i32 @iotex_to_psa_error(i32 noundef %27) #2
  %29 = load i64, ptr %5, align 8, !tbaa !12
  %30 = icmp ugt i64 %29, %4
  %spec.select = select i1 %30, i32 -151, i32 %28
  br label %31

31:                                               ; preds = %26, %24, %19
  %.023 = phi i32 [ -138, %19 ], [ %25, %24 ], [ %spec.select, %26 ]
  ret i32 %.023
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_cipher_update_ecb(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare i32 @iotex_cipher_update(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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 = { 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 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !8, i64 4, !8, i64 5, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!8, !8, i64 0}
!11 = !{!6, !8, i64 5}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !8, i64 0}


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