; 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: nounwind uwtable
define dso_local i32 @mbedtls_cipher_set_padding_mode(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = icmp eq ptr null, %6
  br i1 %7, label %15, label %8

8:                                                ; preds = %2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %12 = getelementptr inbounds %struct.mbedtls_cipher_info_t, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %14 = icmp ne i32 2, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %8, %2
  store i32 -24832, ptr %3, align 4
  br label %45

16:                                               ; preds = %8
  %17 = load i32, ptr %5, align 4, !tbaa !9
  switch i32 %17, label %43 [
    i32 0, label %18
    i32 1, label %23
    i32 2, label %28
    i32 3, label %33
    i32 4, label %38
  ]

18:                                               ; preds = %16
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %19, i32 0, i32 3
  store ptr @add_pkcs_padding, ptr %20, align 8, !tbaa !16
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %21, i32 0, i32 4
  store ptr @get_pkcs_padding, ptr %22, align 8, !tbaa !17
  br label %44

23:                                               ; preds = %16
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %24, i32 0, i32 3
  store ptr @add_one_and_zeros_padding, ptr %25, align 8, !tbaa !16
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %26, i32 0, i32 4
  store ptr @get_one_and_zeros_padding, ptr %27, align 8, !tbaa !17
  br label %44

28:                                               ; preds = %16
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %29, i32 0, i32 3
  store ptr @add_zeros_and_len_padding, ptr %30, align 8, !tbaa !16
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %31, i32 0, i32 4
  store ptr @get_zeros_and_len_padding, ptr %32, align 8, !tbaa !17
  br label %44

33:                                               ; preds = %16
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %34, i32 0, i32 3
  store ptr @add_zeros_padding, ptr %35, align 8, !tbaa !16
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %36, i32 0, i32 4
  store ptr @get_zeros_padding, ptr %37, align 8, !tbaa !17
  br label %44

38:                                               ; preds = %16
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %39, i32 0, i32 3
  store ptr null, ptr %40, align 8, !tbaa !16
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_cipher_context_t, ptr %41, i32 0, i32 4
  store ptr @get_no_padding, ptr %42, align 8, !tbaa !17
  br label %44

43:                                               ; preds = %16
  store i32 -24704, ptr %3, align 4
  br label %45

44:                                               ; preds = %38, %33, %28, %23, %18
  store i32 0, ptr %3, align 4
  br label %45

45:                                               ; preds = %44, %43, %15
  %46 = load i32, ptr %3, align 4
  ret i32 %46
}

; Function Attrs: nounwind uwtable
declare hidden void @add_pkcs_padding(ptr noundef, i64 noundef, i64 noundef) #0

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

; Function Attrs: nounwind uwtable
declare hidden void @add_one_and_zeros_padding(ptr noundef, i64 noundef, i64 noundef) #0

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

; Function Attrs: nounwind uwtable
declare hidden void @add_zeros_and_len_padding(ptr noundef, i64 noundef, i64 noundef) #0

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

; Function Attrs: nounwind uwtable
declare hidden void @add_zeros_padding(ptr noundef, i64 noundef, i64 noundef) #0

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

; Function Attrs: nounwind uwtable
declare hidden i32 @get_no_padding(ptr noundef, i64 noundef, ptr noundef) #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 = !{!"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 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 24, !7, i64 32, !13, i64 48, !7, i64 56, !13, i64 72, !6, i64 80}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !10, i64 4}
!15 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !6, i64 40}
!16 = !{!12, !6, i64 16}
!17 = !{!12, !6, i64 24}


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

@switch.table.mbedtls_cipher_set_padding_mode = private unnamed_addr constant [5 x ptr] [ptr @add_pkcs_padding, ptr @add_one_and_zeros_padding, ptr @add_zeros_and_len_padding, ptr @add_zeros_padding, ptr null], align 8
@switch.table.mbedtls_cipher_set_padding_mode.1 = private unnamed_addr constant [5 x ptr] [ptr @get_pkcs_padding, ptr @get_one_and_zeros_padding, ptr @get_zeros_and_len_padding, ptr @get_zeros_padding, ptr @get_no_padding], align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 -24832, 1) i32 @mbedtls_cipher_set_padding_mode(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %14, label %4

4:                                                ; preds = %2
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !12
  %.not = icmp eq i32 %7, 2
  br i1 %.not, label %8, label %14

8:                                                ; preds = %4
  %9 = icmp ult i32 %1, 5
  br i1 %9, label %switch.lookup, label %14

switch.lookup:                                    ; preds = %8
  %10 = zext nneg i32 %1 to i64
  %switch.gep = getelementptr inbounds [5 x ptr], ptr @switch.table.mbedtls_cipher_set_padding_mode, i64 0, i64 %10
  %switch.load = load ptr, ptr %switch.gep, align 8
  %11 = zext nneg i32 %1 to i64
  %switch.gep14 = getelementptr inbounds [5 x ptr], ptr @switch.table.mbedtls_cipher_set_padding_mode.1, i64 0, i64 %11
  %switch.load15 = load ptr, ptr %switch.gep14, align 8
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr %switch.load, ptr %12, align 8, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr %switch.load15, ptr %13, align 8, !tbaa !15
  br label %14

14:                                               ; preds = %8, %switch.lookup, %2, %4
  %.0 = phi i32 [ -24832, %4 ], [ -24832, %2 ], [ -24704, %8 ], [ 0, %switch.lookup ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden void @add_pkcs_padding(ptr noundef, i64 noundef, i64 noundef) #1

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

; Function Attrs: nounwind uwtable
declare hidden void @add_one_and_zeros_padding(ptr noundef, i64 noundef, i64 noundef) #1

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

; Function Attrs: nounwind uwtable
declare hidden void @add_zeros_and_len_padding(ptr noundef, i64 noundef, i64 noundef) #1

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

; Function Attrs: nounwind uwtable
declare hidden void @add_zeros_padding(ptr noundef, i64 noundef, i64 noundef) #1

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

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) 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 = { 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, !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 = !{!6, !7, i64 16}
!15 = !{!6, !7, i64 24}


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