; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (blowfish)

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

; 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
declare dso_local i32 @mbedtls_blowfish_crypt_ecb(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

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

20:                                               ; preds = %51, %7
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = add i64 %21, -1
  store i64 %22, ptr %9, align 8, !tbaa !9
  %23 = icmp ne i64 %21, 0
  br i1 %23, label %24, label %69

24:                                               ; preds = %20
  %25 = load i64, ptr %17, align 8, !tbaa !9
  %26 = icmp eq i64 %25, 0
  br i1 %26, label %27, label %51

27:                                               ; preds = %24
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = load ptr, ptr %11, align 8, !tbaa !5
  %30 = load ptr, ptr %12, align 8, !tbaa !5
  %31 = call i32 @mbedtls_blowfish_crypt_ecb(ptr noundef %28, i32 noundef 1, ptr noundef %29, ptr noundef %30)
  store i32 8, ptr %16, align 4, !tbaa !11
  br label %32

32:                                               ; preds = %47, %27
  %33 = load i32, ptr %16, align 4, !tbaa !11
  %34 = icmp sgt i32 %33, 0
  br i1 %34, label %35, label %50

35:                                               ; preds = %32
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = load i32, ptr %16, align 4, !tbaa !11
  %38 = sub nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i8, ptr %36, i64 %39
  %41 = load i8, ptr %40, align 1, !tbaa !13
  %42 = add i8 %41, 1
  store i8 %42, ptr %40, align 1, !tbaa !13
  %43 = zext i8 %42 to i32
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %46

45:                                               ; preds = %35
  br label %50

46:                                               ; preds = %35
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %16, align 4, !tbaa !11
  %49 = add nsw i32 %48, -1
  store i32 %49, ptr %16, align 4, !tbaa !11
  br label %32, !llvm.loop !14

50:                                               ; preds = %45, %32
  br label %51

51:                                               ; preds = %50, %24
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %52, i32 1
  store ptr %53, ptr %13, align 8, !tbaa !5
  %54 = load i8, ptr %52, align 1, !tbaa !13
  %55 = zext i8 %54 to i32
  store i32 %55, ptr %15, align 4, !tbaa !11
  %56 = load i32, ptr %15, align 4, !tbaa !11
  %57 = load ptr, ptr %12, align 8, !tbaa !5
  %58 = load i64, ptr %17, align 8, !tbaa !9
  %59 = getelementptr inbounds i8, ptr %57, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !13
  %61 = zext i8 %60 to i32
  %62 = xor i32 %56, %61
  %63 = trunc i32 %62 to i8
  %64 = load ptr, ptr %14, align 8, !tbaa !5
  %65 = getelementptr inbounds i8, ptr %64, i32 1
  store ptr %65, ptr %14, align 8, !tbaa !5
  store i8 %63, ptr %64, align 1, !tbaa !13
  %66 = load i64, ptr %17, align 8, !tbaa !9
  %67 = add i64 %66, 1
  %68 = urem i64 %67, 8
  store i64 %68, ptr %17, align 8, !tbaa !9
  br label %20, !llvm.loop !16

69:                                               ; preds = %20
  %70 = load i64, ptr %17, align 8, !tbaa !9
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  store i64 %70, ptr %71, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  ret i32 0
}

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 = { 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 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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: nounwind uwtable
declare dso_local i32 @mbedtls_blowfish_crypt_ecb(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @mbedtls_blowfish_crypt_ctr(ptr noundef %0, i64 noundef %1, ptr nocapture noundef %2, ptr noundef %3, ptr noundef %4, ptr nocapture noundef readonly %5, ptr nocapture noundef writeonly %6) local_unnamed_addr #0 {
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %.not23 = icmp eq i64 %1, 0
  br i1 %.not23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7
  %gep = getelementptr i8, ptr %3, i64 7
  %gep.1 = getelementptr i8, ptr %3, i64 6
  %gep.2 = getelementptr i8, ptr %3, i64 5
  %gep.3 = getelementptr i8, ptr %3, i64 4
  %gep.4 = getelementptr i8, ptr %3, i64 3
  %gep.5 = getelementptr i8, ptr %3, i64 2
  %gep.6 = getelementptr i8, ptr %3, i64 1
  br label %9

9:                                                ; preds = %.lr.ph, %.loopexit
  %.in = phi i64 [ %1, %.lr.ph ], [ %10, %.loopexit ]
  %.026 = phi i64 [ %8, %.lr.ph ], [ %44, %.loopexit ]
  %.01925 = phi ptr [ %6, %.lr.ph ], [ %42, %.loopexit ]
  %.02024 = phi ptr [ %5, %.lr.ph ], [ %37, %.loopexit ]
  %10 = add i64 %.in, -1
  %11 = icmp eq i64 %.026, 0
  br i1 %11, label %12, label %.loopexit

12:                                               ; preds = %9
  %13 = tail call i32 @mbedtls_blowfish_crypt_ecb(ptr noundef %0, i32 noundef 1, ptr noundef %3, ptr noundef %4)
  %14 = load i8, ptr %gep, align 1, !tbaa !9
  %15 = add i8 %14, 1
  store i8 %15, ptr %gep, align 1, !tbaa !9
  %.not21 = icmp eq i8 %15, 0
  br i1 %.not21, label %16, label %.loopexit, !llvm.loop !10

16:                                               ; preds = %12
  %17 = load i8, ptr %gep.1, align 1, !tbaa !9
  %18 = add i8 %17, 1
  store i8 %18, ptr %gep.1, align 1, !tbaa !9
  %.not21.1 = icmp eq i8 %18, 0
  br i1 %.not21.1, label %19, label %.loopexit, !llvm.loop !10

19:                                               ; preds = %16
  %20 = load i8, ptr %gep.2, align 1, !tbaa !9
  %21 = add i8 %20, 1
  store i8 %21, ptr %gep.2, align 1, !tbaa !9
  %.not21.2 = icmp eq i8 %21, 0
  br i1 %.not21.2, label %22, label %.loopexit, !llvm.loop !10

22:                                               ; preds = %19
  %23 = load i8, ptr %gep.3, align 1, !tbaa !9
  %24 = add i8 %23, 1
  store i8 %24, ptr %gep.3, align 1, !tbaa !9
  %.not21.3 = icmp eq i8 %24, 0
  br i1 %.not21.3, label %25, label %.loopexit, !llvm.loop !10

25:                                               ; preds = %22
  %26 = load i8, ptr %gep.4, align 1, !tbaa !9
  %27 = add i8 %26, 1
  store i8 %27, ptr %gep.4, align 1, !tbaa !9
  %.not21.4 = icmp eq i8 %27, 0
  br i1 %.not21.4, label %28, label %.loopexit, !llvm.loop !10

28:                                               ; preds = %25
  %29 = load i8, ptr %gep.5, align 1, !tbaa !9
  %30 = add i8 %29, 1
  store i8 %30, ptr %gep.5, align 1, !tbaa !9
  %.not21.5 = icmp eq i8 %30, 0
  br i1 %.not21.5, label %31, label %.loopexit, !llvm.loop !10

31:                                               ; preds = %28
  %32 = load i8, ptr %gep.6, align 1, !tbaa !9
  %33 = add i8 %32, 1
  store i8 %33, ptr %gep.6, align 1, !tbaa !9
  %.not21.6 = icmp eq i8 %33, 0
  br i1 %.not21.6, label %34, label %.loopexit, !llvm.loop !10

34:                                               ; preds = %31
  %35 = load i8, ptr %3, align 1, !tbaa !9
  %36 = add i8 %35, 1
  store i8 %36, ptr %3, align 1, !tbaa !9
  br label %.loopexit

.loopexit:                                        ; preds = %12, %16, %19, %22, %25, %28, %31, %34, %9
  %37 = getelementptr inbounds i8, ptr %.02024, i64 1
  %38 = load i8, ptr %.02024, align 1, !tbaa !9
  %39 = getelementptr inbounds i8, ptr %4, i64 %.026
  %40 = load i8, ptr %39, align 1, !tbaa !9
  %41 = xor i8 %40, %38
  %42 = getelementptr inbounds i8, ptr %.01925, i64 1
  store i8 %41, ptr %.01925, align 1, !tbaa !9
  %43 = add i64 %.026, 1
  %44 = and i64 %43, 7
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %._crit_edge, label %9, !llvm.loop !12

._crit_edge:                                      ; preds = %.loopexit, %7
  %.0.lcssa = phi i64 [ %8, %7 ], [ %44, %.loopexit ]
  store i64 %.0.lcssa, ptr %2, align 8, !tbaa !5
  ret i32 0
}

attributes #0 = { 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" }

!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"}
!12 = distinct !{!12, !11}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass