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

; 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_ssl_config = type { [4 x ptr], ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, %struct.mbedtls_mpi, %struct.mbedtls_mpi, ptr, i64, ptr, i64, ptr, i32, i32, i32, i32, [8 x i8], i32, i32, i8, i8, i8, i8, i32 }
%struct.mbedtls_mpi = type { i32, i64, ptr }

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #1

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_conf_psk(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) #3 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i64 %4, ptr %11, align 8, !tbaa !9
  %12 = load ptr, ptr %8, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %17, label %14

14:                                               ; preds = %5
  %15 = load ptr, ptr %10, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %14, %5
  store i32 -28928, ptr %6, align 4
  br label %95

18:                                               ; preds = %14
  %19 = load i64, ptr %9, align 8, !tbaa !9
  %20 = icmp ugt i64 %19, 32
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  store i32 -28928, ptr %6, align 4
  br label %95

22:                                               ; preds = %18
  %23 = load i64, ptr %11, align 8, !tbaa !9
  %24 = lshr i64 %23, 16
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %29, label %26

26:                                               ; preds = %22
  %27 = load i64, ptr %11, align 8, !tbaa !9
  %28 = icmp ugt i64 %27, 16384
  br i1 %28, label %29, label %30

29:                                               ; preds = %26, %22
  store i32 -28928, ptr %6, align 4
  br label %95

30:                                               ; preds = %26
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %31, i32 0, i32 30
  %33 = load ptr, ptr %32, align 8, !tbaa !11
  %34 = icmp ne ptr %33, null
  br i1 %34, label %40, label %35

35:                                               ; preds = %30
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %36, i32 0, i32 32
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %51

40:                                               ; preds = %35, %30
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %41, i32 0, i32 30
  %43 = load ptr, ptr %42, align 8, !tbaa !11
  call void @free(ptr noundef %43) #4
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %44, i32 0, i32 32
  %46 = load ptr, ptr %45, align 8, !tbaa !15
  call void @free(ptr noundef %46) #4
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %47, i32 0, i32 30
  store ptr null, ptr %48, align 8, !tbaa !11
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %49, i32 0, i32 32
  store ptr null, ptr %50, align 8, !tbaa !15
  br label %51

51:                                               ; preds = %40, %35
  %52 = load i64, ptr %9, align 8, !tbaa !9
  %53 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %52) #5
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %54, i32 0, i32 30
  store ptr %53, ptr %55, align 8, !tbaa !11
  %56 = icmp eq ptr %53, null
  br i1 %56, label %63, label %57

57:                                               ; preds = %51
  %58 = load i64, ptr %11, align 8, !tbaa !9
  %59 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %58) #5
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %60, i32 0, i32 32
  store ptr %59, ptr %61, align 8, !tbaa !15
  %62 = icmp eq ptr %59, null
  br i1 %62, label %63, label %74

63:                                               ; preds = %57, %51
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %64, i32 0, i32 30
  %66 = load ptr, ptr %65, align 8, !tbaa !11
  call void @free(ptr noundef %66) #4
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %67, i32 0, i32 32
  %69 = load ptr, ptr %68, align 8, !tbaa !15
  call void @free(ptr noundef %69) #4
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %70, i32 0, i32 30
  store ptr null, ptr %71, align 8, !tbaa !11
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %72, i32 0, i32 32
  store ptr null, ptr %73, align 8, !tbaa !15
  store i32 -32512, ptr %6, align 4
  br label %95

74:                                               ; preds = %57
  %75 = load i64, ptr %9, align 8, !tbaa !9
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %76, i32 0, i32 31
  store i64 %75, ptr %77, align 8, !tbaa !16
  %78 = load i64, ptr %11, align 8, !tbaa !9
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %79, i32 0, i32 33
  store i64 %78, ptr %80, align 8, !tbaa !17
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %81, i32 0, i32 30
  %83 = load ptr, ptr %82, align 8, !tbaa !11
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = load ptr, ptr %7, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %85, i32 0, i32 31
  %87 = load i64, ptr %86, align 8, !tbaa !16
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %83, ptr align 1 %84, i64 %87, i1 false)
  %88 = load ptr, ptr %7, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %88, i32 0, i32 32
  %90 = load ptr, ptr %89, align 8, !tbaa !15
  %91 = load ptr, ptr %10, align 8, !tbaa !5
  %92 = load ptr, ptr %7, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %92, i32 0, i32 33
  %94 = load i64, ptr %93, align 8, !tbaa !17
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %90, ptr align 1 %91, i64 %94, i1 false)
  store i32 0, ptr %6, align 4
  br label %95

95:                                               ; preds = %74, %63, %29, %21, %17
  %96 = load i32, ptr %6, align 4
  ret i32 %96
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 allocsize(0,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 #3 = { 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0,1) }

!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 296}
!12 = !{!"mbedtls_ssl_config", !7, i64 0, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !13, i64 248, !13, i64 272, !6, i64 296, !10, i64 304, !6, i64 312, !10, i64 320, !6, i64 328, !14, i64 336, !14, i64 340, !14, i64 344, !14, i64 348, !7, i64 352, !14, i64 360, !14, i64 364, !7, i64 368, !7, i64 369, !7, i64 370, !7, i64 371, !14, i64 372, !14, i64 372, !14, i64 372, !14, i64 372, !14, i64 372, !14, i64 372, !14, i64 373, !14, i64 373, !14, i64 373, !14, i64 373, !14, i64 373, !14, i64 373, !14, i64 374, !14, i64 374, !14, i64 374}
!13 = !{!"", !14, i64 0, !10, i64 8, !6, i64 16}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 312}
!16 = !{!12, !10, i64 304}
!17 = !{!12, !10, i64 320}


; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn uwtable
define dso_local range(i32 -32512, 1) i32 @mbedtls_ssl_conf_psk(ptr nocapture noundef %0, ptr noundef readonly %1, i64 noundef %2, ptr noundef readonly %3, i64 noundef %4) local_unnamed_addr #3 {
  %6 = icmp eq ptr %1, null
  %7 = icmp eq ptr %3, null
  %or.cond = or i1 %6, %7
  %8 = icmp ugt i64 %2, 32
  %or.cond37 = or i1 %8, %or.cond
  %9 = icmp ugt i64 %4, 16384
  %or.cond38 = or i1 %9, %or.cond37
  br i1 %or.cond38, label %31, label %10

10:                                               ; preds = %5
  %11 = getelementptr inbounds i8, ptr %0, i64 296
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %13, label %16

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %0, i64 312
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %.not36 = icmp eq ptr %15, null
  br i1 %.not36, label %19, label %16

16:                                               ; preds = %13, %10
  tail call void @free(ptr noundef %12) #4
  %17 = getelementptr inbounds i8, ptr %0, i64 312
  %18 = load ptr, ptr %17, align 8, !tbaa !13
  tail call void @free(ptr noundef %18) #4
  br label %19

19:                                               ; preds = %16, %13
  %20 = tail call noalias ptr @calloc(i64 noundef 1, i64 noundef %2) #5
  store ptr %20, ptr %11, align 8, !tbaa !5
  %21 = icmp eq ptr %20, null
  br i1 %21, label %26, label %22

22:                                               ; preds = %19
  %23 = tail call noalias ptr @calloc(i64 noundef 1, i64 noundef %4) #5
  %24 = getelementptr inbounds i8, ptr %0, i64 312
  store ptr %23, ptr %24, align 8, !tbaa !13
  %25 = icmp eq ptr %23, null
  br i1 %25, label %26, label %28

26:                                               ; preds = %22, %19
  tail call void @free(ptr noundef %20) #4
  %27 = getelementptr inbounds i8, ptr %0, i64 312
  store ptr null, ptr %11, align 8, !tbaa !5
  store ptr null, ptr %27, align 8, !tbaa !13
  br label %31

28:                                               ; preds = %22
  %29 = getelementptr inbounds i8, ptr %0, i64 304
  store i64 %2, ptr %29, align 8, !tbaa !14
  %30 = getelementptr inbounds i8, ptr %0, i64 320
  store i64 %4, ptr %30, align 8, !tbaa !15
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %20, ptr nonnull align 1 %1, i64 %2, i1 false)
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %23, ptr nonnull align 1 %3, i64 %4, i1 false)
  br label %31

31:                                               ; preds = %5, %28, %26
  %.0 = phi i32 [ -32512, %26 ], [ 0, %28 ], [ -28928, %5 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0,1) }

!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, !9, i64 296}
!6 = !{!"mbedtls_ssl_config", !7, i64 0, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56, !9, i64 64, !9, i64 72, !9, i64 80, !9, i64 88, !9, i64 96, !9, i64 104, !9, i64 112, !9, i64 120, !9, i64 128, !9, i64 136, !9, i64 144, !9, i64 152, !9, i64 160, !9, i64 168, !9, i64 176, !9, i64 184, !9, i64 192, !9, i64 200, !9, i64 208, !9, i64 216, !9, i64 224, !9, i64 232, !9, i64 240, !10, i64 248, !10, i64 272, !9, i64 296, !12, i64 304, !9, i64 312, !12, i64 320, !9, i64 328, !11, i64 336, !11, i64 340, !11, i64 344, !11, i64 348, !7, i64 352, !11, i64 360, !11, i64 364, !7, i64 368, !7, i64 369, !7, i64 370, !7, i64 371, !11, i64 372, !11, i64 372, !11, i64 372, !11, i64 372, !11, i64 372, !11, i64 372, !11, i64 373, !11, i64 373, !11, i64 373, !11, i64 373, !11, i64 373, !11, i64 373, !11, i64 374, !11, i64 374, !11, i64 374}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!"", !11, i64 0, !12, i64 8, !9, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!6, !9, i64 312}
!14 = !{!6, !12, i64 304}
!15 = !{!6, !12, i64 320}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass