; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (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.mbedtls_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr }
%struct.mbedtls_cipher_info_t = type { i32, i32, i32, ptr, i32, i32, i32, 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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_cipher_auth_decrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, i64 noundef %6, ptr noundef %7, ptr noundef %8, ptr noundef %9, i64 noundef %10) #1 {
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i64, align 8
  %18 = alloca ptr, align 8
  %19 = alloca i64, align 8
  %20 = alloca ptr, align 8
  %21 = alloca ptr, align 8
  %22 = alloca ptr, align 8
  %23 = alloca i64, align 8
  %24 = alloca i32, align 4
  %25 = alloca i32, align 4
  store ptr %0, ptr %13, align 8, !tbaa !5
  store ptr %1, ptr %14, align 8, !tbaa !5
  store i64 %2, ptr %15, align 8, !tbaa !9
  store ptr %3, ptr %16, align 8, !tbaa !5
  store i64 %4, ptr %17, align 8, !tbaa !9
  store ptr %5, ptr %18, align 8, !tbaa !5
  store i64 %6, ptr %19, align 8, !tbaa !9
  store ptr %7, ptr %20, align 8, !tbaa !5
  store ptr %8, ptr %21, align 8, !tbaa !5
  store ptr %9, ptr %22, align 8, !tbaa !5
  store i64 %10, ptr %23, align 8, !tbaa !9
  %26 = load ptr, ptr %13, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = getelementptr inbounds %struct.mbedtls_cipher_info_t, ptr %28, i32 0, i32 1
  %30 = load i32, ptr %29, align 4, !tbaa !14
  %31 = icmp eq i32 6, %30
  br i1 %31, label %32, label %53

32:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %24) #3
  %33 = load i64, ptr %19, align 8, !tbaa !9
  %34 = load ptr, ptr %21, align 8, !tbaa !5
  store i64 %33, ptr %34, align 8, !tbaa !9
  %35 = load ptr, ptr %13, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %35, i32 0, i32 9
  %37 = load ptr, ptr %36, align 8, !tbaa !16
  %38 = load i64, ptr %19, align 8, !tbaa !9
  %39 = load ptr, ptr %14, align 8, !tbaa !5
  %40 = load i64, ptr %15, align 8, !tbaa !9
  %41 = load ptr, ptr %16, align 8, !tbaa !5
  %42 = load i64, ptr %17, align 8, !tbaa !9
  %43 = load ptr, ptr %22, align 8, !tbaa !5
  %44 = load i64, ptr %23, align 8, !tbaa !9
  %45 = load ptr, ptr %18, align 8, !tbaa !5
  %46 = load ptr, ptr %20, align 8, !tbaa !5
  %47 = call i32 @mbedtls_gcm_auth_decrypt(ptr noundef %37, i64 noundef %38, ptr noundef %39, i64 noundef %40, ptr noundef %41, i64 noundef %42, ptr noundef %43, i64 noundef %44, ptr noundef %45, ptr noundef %46)
  store i32 %47, ptr %24, align 4, !tbaa !17
  %48 = load i32, ptr %24, align 4, !tbaa !17
  %49 = icmp eq i32 %48, -18
  br i1 %49, label %50, label %51

50:                                               ; preds = %32
  store i32 -25344, ptr %24, align 4, !tbaa !17
  br label %51

51:                                               ; preds = %50, %32
  %52 = load i32, ptr %24, align 4, !tbaa !17
  store i32 %52, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %24) #3
  br label %82

53:                                               ; preds = %11
  %54 = load ptr, ptr %13, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !11
  %57 = getelementptr inbounds %struct.mbedtls_cipher_info_t, ptr %56, i32 0, i32 1
  %58 = load i32, ptr %57, align 4, !tbaa !14
  %59 = icmp eq i32 8, %58
  br i1 %59, label %60, label %81

60:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 4, ptr %25) #3
  %61 = load i64, ptr %19, align 8, !tbaa !9
  %62 = load ptr, ptr %21, align 8, !tbaa !5
  store i64 %61, ptr %62, align 8, !tbaa !9
  %63 = load ptr, ptr %13, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %63, i32 0, i32 9
  %65 = load ptr, ptr %64, align 8, !tbaa !16
  %66 = load i64, ptr %19, align 8, !tbaa !9
  %67 = load ptr, ptr %14, align 8, !tbaa !5
  %68 = load i64, ptr %15, align 8, !tbaa !9
  %69 = load ptr, ptr %16, align 8, !tbaa !5
  %70 = load i64, ptr %17, align 8, !tbaa !9
  %71 = load ptr, ptr %18, align 8, !tbaa !5
  %72 = load ptr, ptr %20, align 8, !tbaa !5
  %73 = load ptr, ptr %22, align 8, !tbaa !5
  %74 = load i64, ptr %23, align 8, !tbaa !9
  %75 = call i32 @mbedtls_ccm_auth_decrypt(ptr noundef %65, i64 noundef %66, ptr noundef %67, i64 noundef %68, ptr noundef %69, i64 noundef %70, ptr noundef %71, ptr noundef %72, ptr noundef %73, i64 noundef %74)
  store i32 %75, ptr %25, align 4, !tbaa !17
  %76 = load i32, ptr %25, align 4, !tbaa !17
  %77 = icmp eq i32 %76, -15
  br i1 %77, label %78, label %79

78:                                               ; preds = %60
  store i32 -25344, ptr %25, align 4, !tbaa !17
  br label %79

79:                                               ; preds = %78, %60
  %80 = load i32, ptr %25, align 4, !tbaa !17
  store i32 %80, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %25) #3
  br label %82

81:                                               ; preds = %53
  store i32 -24704, ptr %12, align 4
  br label %82

82:                                               ; preds = %81, %79, %51
  %83 = load i32, ptr %12, align 4
  ret i32 %83
}

declare i32 @mbedtls_gcm_auth_decrypt(ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #2

declare i32 @mbedtls_ccm_auth_decrypt(ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef) #2

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 = { "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, !6, i64 0}
!12 = !{!"", !6, i64 0, !13, i64 8, !13, i64 12, !6, i64 16, !6, i64 24, !7, i64 32, !10, i64 48, !7, i64 56, !10, i64 72, !6, i64 80}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !13, i64 4}
!15 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8, !6, i64 16, !13, i64 24, !13, i64 28, !13, i64 32, !6, i64 40}
!16 = !{!12, !6, i64 80}
!17 = !{!13, !13, 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: nounwind uwtable
define dso_local i32 @mbedtls_cipher_auth_decrypt(ptr nocapture noundef readonly %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, i64 noundef %6, ptr noundef %7, ptr nocapture noundef writeonly %8, ptr noundef %9, i64 noundef %10) local_unnamed_addr #0 {
  %12 = load ptr, ptr %0, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !12
  switch i32 %14, label %25 [
    i32 6, label %15
    i32 8, label %20
  ]

15:                                               ; preds = %11
  store i64 %6, ptr %8, align 8, !tbaa !14
  %16 = getelementptr inbounds i8, ptr %0, i64 80
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %18 = tail call i32 @mbedtls_gcm_auth_decrypt(ptr noundef %17, i64 noundef %6, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %9, i64 noundef %10, ptr noundef %5, ptr noundef %7) #2
  %19 = icmp eq i32 %18, -18
  %spec.store.select = select i1 %19, i32 -25344, i32 %18
  br label %25

20:                                               ; preds = %11
  store i64 %6, ptr %8, align 8, !tbaa !14
  %21 = getelementptr inbounds i8, ptr %0, i64 80
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = tail call i32 @mbedtls_ccm_auth_decrypt(ptr noundef %22, i64 noundef %6, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, ptr noundef %7, ptr noundef %9, i64 noundef %10) #2
  %24 = icmp eq i32 %23, -15
  %spec.store.select1 = select i1 %24, i32 -25344, i32 %23
  br label %25

25:                                               ; preds = %11, %20, %15
  %.0 = phi i32 [ %spec.store.select, %15 ], [ %spec.store.select1, %20 ], [ -24704, %11 ]
  ret i32 %.0
}

declare i32 @mbedtls_gcm_auth_decrypt(ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare i32 @mbedtls_ccm_auth_decrypt(ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

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

!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, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 24, !8, i64 32, !11, i64 48, !8, i64 56, !11, i64 72, !7, i64 80}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!13, !10, i64 4}
!13 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !7, i64 40}
!14 = !{!11, !11, i64 0}
!15 = !{!6, !7, i64 80}


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