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

; 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.mbedtls_des_context = type { [32 x 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
declare dso_local void @mbedtls_des_setkey(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_des_setkey_dec(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.mbedtls_des_context, ptr %8, i32 0, i32 0
  %10 = getelementptr inbounds [32 x i32], ptr %9, i64 0, i64 0
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  call void @mbedtls_des_setkey(ptr noundef %10, ptr noundef %11)
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %68, %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp slt i32 %13, 16
  br i1 %14, label %15, label %71

15:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mbedtls_des_context, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [32 x i32], ptr %17, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !9
  store i32 %21, ptr %6, align 4, !tbaa !9
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.mbedtls_des_context, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = sub nsw i32 30, %24
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [32 x i32], ptr %23, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mbedtls_des_context, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [32 x i32], ptr %30, i64 0, i64 %32
  store i32 %28, ptr %33, align 4, !tbaa !9
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.mbedtls_des_context, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = sub nsw i32 30, %37
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [32 x i32], ptr %36, i64 0, i64 %39
  store i32 %34, ptr %40, align 4, !tbaa !9
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_des_context, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [32 x i32], ptr %42, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !9
  store i32 %47, ptr %7, align 4, !tbaa !9
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.mbedtls_des_context, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = sub nsw i32 31, %50
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [32 x i32], ptr %49, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !9
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.mbedtls_des_context, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %5, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [32 x i32], ptr %56, i64 0, i64 %59
  store i32 %54, ptr %60, align 4, !tbaa !9
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.mbedtls_des_context, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %5, align 4, !tbaa !9
  %65 = sub nsw i32 31, %64
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [32 x i32], ptr %63, i64 0, i64 %66
  store i32 %61, ptr %67, align 4, !tbaa !9
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %68

68:                                               ; preds = %15
  %69 = load i32, ptr %5, align 4, !tbaa !9
  %70 = add nsw i32 %69, 2
  store i32 %70, ptr %5, align 4, !tbaa !9
  br label %12, !llvm.loop !11

71:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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 void @mbedtls_des_setkey(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @mbedtls_des_setkey_dec(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  tail call void @mbedtls_des_setkey(ptr noundef %0, ptr noundef %1)
  %3 = getelementptr inbounds i8, ptr %0, i64 112
  %4 = load <4 x i32>, ptr %3, align 4, !tbaa !5
  %5 = shufflevector <4 x i32> %4, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
  %6 = load <4 x i32>, ptr %0, align 4, !tbaa !5
  store <4 x i32> %5, ptr %0, align 4, !tbaa !5
  %7 = shufflevector <4 x i32> %6, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
  store <4 x i32> %7, ptr %3, align 4, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = getelementptr inbounds i8, ptr %0, i64 96
  %10 = load <4 x i32>, ptr %9, align 4, !tbaa !5
  %11 = shufflevector <4 x i32> %10, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
  %12 = load <4 x i32>, ptr %8, align 4, !tbaa !5
  store <4 x i32> %11, ptr %8, align 4, !tbaa !5
  %13 = shufflevector <4 x i32> %12, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
  store <4 x i32> %13, ptr %9, align 4, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %0, i64 32
  %15 = getelementptr inbounds i8, ptr %0, i64 80
  %16 = load <4 x i32>, ptr %15, align 4, !tbaa !5
  %17 = shufflevector <4 x i32> %16, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
  %18 = load <4 x i32>, ptr %14, align 4, !tbaa !5
  store <4 x i32> %17, ptr %14, align 4, !tbaa !5
  %19 = shufflevector <4 x i32> %18, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
  store <4 x i32> %19, ptr %15, align 4, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %0, i64 48
  %21 = getelementptr inbounds i8, ptr %0, i64 64
  %22 = load <4 x i32>, ptr %21, align 4, !tbaa !5
  %23 = shufflevector <4 x i32> %22, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
  %24 = load <4 x i32>, ptr %20, align 4, !tbaa !5
  store <4 x i32> %23, ptr %20, align 4, !tbaa !5
  %25 = shufflevector <4 x i32> %24, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
  store <4 x i32> %25, ptr %21, align 4, !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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass