; LLVM IR from PacktPublishing/Demystifying-Cryptography-with-OpenSSL-3 :: Chapter11 (tls-client-memory-bio)

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

@.str.3 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [124 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [8 x i8], align 1
@__PRETTY_FUNCTION__.service_bios = external hidden unnamed_addr constant [43 x i8], align 1

; 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 allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

declare i64 @BIO_ctrl(ptr noundef, i32 noundef, i64 noundef, ptr noundef) #3

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

21:                                               ; preds = %4
  br label %23

22:                                               ; preds = %4
  call void @__assert_fail(ptr noundef @.str.3, ptr noundef @.str.4, i32 noundef 318, ptr noundef @__PRETTY_FUNCTION__.service_bios) #8
  unreachable

23:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %24 = call noalias ptr @malloc(i64 noundef 16384) #7
  store ptr %24, ptr %12, align 8, !tbaa !5
  %25 = load ptr, ptr %12, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %28

27:                                               ; preds = %23
  br label %29

28:                                               ; preds = %23
  call void @__assert_fail(ptr noundef @.str.5, ptr noundef @.str.4, i32 noundef 320, ptr noundef @__PRETTY_FUNCTION__.service_bios) #8
  unreachable

29:                                               ; preds = %27
  br label %30

30:                                               ; preds = %60, %29
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = call i64 @BIO_ctrl(ptr noundef %31, i32 noundef 10, i64 noundef 0, ptr noundef null)
  %33 = trunc i64 %32 to i32
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %61

35:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #6
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load ptr, ptr %12, align 8, !tbaa !5
  %38 = call i32 @BIO_read(ptr noundef %36, ptr noundef %37, i32 noundef 16384)
  store i32 %38, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #6
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %55, %35
  %40 = load i32, ptr %14, align 4, !tbaa !9
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %43, label %57

43:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #6
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = load ptr, ptr %12, align 8, !tbaa !5
  %46 = load i32, ptr %13, align 4, !tbaa !9
  %47 = call i32 @BIO_write(ptr noundef %44, ptr noundef %45, i32 noundef %46)
  store i32 %47, ptr %15, align 4, !tbaa !9
  %48 = load i32, ptr %15, align 4, !tbaa !9
  %49 = icmp sgt i32 %48, 0
  br i1 %49, label %50, label %54

50:                                               ; preds = %43
  %51 = load i32, ptr %15, align 4, !tbaa !9
  %52 = load i32, ptr %14, align 4, !tbaa !9
  %53 = add nsw i32 %52, %51
  store i32 %53, ptr %14, align 4, !tbaa !9
  store i32 4, ptr %16, align 4
  br label %55, !llvm.loop !11

54:                                               ; preds = %43
  store i32 6, ptr %16, align 4
  br label %55

55:                                               ; preds = %54, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #6
  %56 = load i32, ptr %16, align 4
  switch i32 %56, label %58 [
    i32 4, label %39
  ]

57:                                               ; preds = %39
  store i32 0, ptr %16, align 4
  br label %58

58:                                               ; preds = %57, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #6
  %59 = load i32, ptr %16, align 4
  switch i32 %59, label %86 [
    i32 0, label %60
    i32 6, label %81
  ]

60:                                               ; preds = %58
  br label %30, !llvm.loop !13

61:                                               ; preds = %30
  %62 = load i32, ptr %9, align 4, !tbaa !9
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %80

64:                                               ; preds = %61
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #6
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = load ptr, ptr %11, align 8, !tbaa !5
  %67 = call i32 @BIO_read(ptr noundef %65, ptr noundef %66, i32 noundef 16384)
  store i32 %67, ptr %17, align 4, !tbaa !9
  %68 = load i32, ptr %17, align 4, !tbaa !9
  %69 = icmp sgt i32 %68, 0
  br i1 %69, label %70, label %75

70:                                               ; preds = %64
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = load ptr, ptr %11, align 8, !tbaa !5
  %73 = load i32, ptr %17, align 4, !tbaa !9
  %74 = call i32 @BIO_write(ptr noundef %71, ptr noundef %72, i32 noundef %73)
  br label %76

75:                                               ; preds = %64
  store i32 6, ptr %16, align 4
  br label %77

76:                                               ; preds = %70
  store i32 0, ptr %16, align 4
  br label %77

77:                                               ; preds = %76, %75
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #6
  %78 = load i32, ptr %16, align 4
  switch i32 %78, label %86 [
    i32 0, label %79
    i32 6, label %81
  ]

79:                                               ; preds = %77
  br label %80

80:                                               ; preds = %79, %61
  br label %82

81:                                               ; preds = %77, %58
  store i32 -1, ptr %10, align 4, !tbaa !9
  br label %82

82:                                               ; preds = %81, %80
  %83 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free(ptr noundef %83) #6
  %84 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %84) #6
  %85 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %85, ptr %5, align 4
  store i32 1, ptr %16, align 4
  br label %86

86:                                               ; preds = %82, %77, %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #6
  %87 = load i32, ptr %5, align 4
  ret i32 %87
}

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

declare i32 @BIO_read(ptr noundef, ptr noundef, i32 noundef) #3

declare i32 @BIO_write(ptr noundef, ptr noundef, i32 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 = { noreturn 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 #3 = { "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 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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { noreturn 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"}
!13 = distinct !{!13, !12}


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

@.str.3 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [124 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [8 x i8], align 1
@__PRETTY_FUNCTION__.service_bios = external hidden unnamed_addr constant [43 x i8], align 1

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare i64 @BIO_ctrl(ptr noundef, i32 noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @service_bios(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #3 {
  %5 = tail call noalias dereferenceable_or_null(16384) ptr @malloc(i64 noundef 16384) #5
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %6, label %7

6:                                                ; preds = %4
  tail call void @__assert_fail(ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.4, i32 noundef 318, ptr noundef nonnull @__PRETTY_FUNCTION__.service_bios) #6
  unreachable

7:                                                ; preds = %4
  %8 = tail call noalias dereferenceable_or_null(16384) ptr @malloc(i64 noundef 16384) #5
  %.not34 = icmp eq ptr %8, null
  br i1 %.not34, label %11, label %.preheader.preheader

.preheader.preheader:                             ; preds = %7
  %9 = tail call i64 @BIO_ctrl(ptr noundef %1, i32 noundef 10, i64 noundef 0, ptr noundef null) #7
  %10 = and i64 %9, 4294967295
  %.not3541 = icmp eq i64 %10, 0
  br i1 %.not3541, label %.preheader._crit_edge, label %.lr.ph

11:                                               ; preds = %7
  tail call void @__assert_fail(ptr noundef nonnull @.str.5, ptr noundef nonnull @.str.4, i32 noundef 320, ptr noundef nonnull @__PRETTY_FUNCTION__.service_bios) #6
  unreachable

.preheader.loopexit:                              ; preds = %15
  %12 = tail call i64 @BIO_ctrl(ptr noundef %1, i32 noundef 10, i64 noundef 0, ptr noundef null) #7
  %13 = and i64 %12, 4294967295
  %.not35 = icmp eq i64 %13, 0
  br i1 %.not35, label %.preheader._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader.preheader, %.preheader.loopexit
  %14 = tail call i32 @BIO_read(ptr noundef %1, ptr noundef nonnull %8, i32 noundef 16384) #7
  br label %15

15:                                               ; preds = %17, %.lr.ph
  %.028 = phi i32 [ 0, %.lr.ph ], [ %20, %17 ]
  %16 = icmp slt i32 %.028, %14
  br i1 %16, label %17, label %.preheader.loopexit

17:                                               ; preds = %15
  %18 = tail call i32 @BIO_write(ptr noundef %2, ptr noundef nonnull %8, i32 noundef %14) #7
  %19 = icmp sgt i32 %18, 0
  %20 = add nuw nsw i32 %18, %.028
  br i1 %19, label %15, label %.loopexit

.preheader._crit_edge:                            ; preds = %.preheader.loopexit, %.preheader.preheader
  %.not36 = icmp eq i32 %3, 0
  br i1 %.not36, label %.loopexit, label %21

21:                                               ; preds = %.preheader._crit_edge
  %22 = tail call i32 @BIO_read(ptr noundef %2, ptr noundef nonnull %5, i32 noundef 16384) #7
  %23 = icmp sgt i32 %22, 0
  br i1 %23, label %.thread, label %.loopexit

.thread:                                          ; preds = %21
  %24 = tail call i32 @BIO_write(ptr noundef %0, ptr noundef nonnull %5, i32 noundef %22) #7
  br label %.loopexit

.loopexit:                                        ; preds = %17, %.preheader._crit_edge, %.thread, %21
  %.027 = phi i32 [ 1, %.preheader._crit_edge ], [ 1, %.thread ], [ -1, %21 ], [ -1, %17 ]
  tail call void @free(ptr noundef %8) #7
  tail call void @free(ptr noundef %5) #7
  ret i32 %.027
}

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

declare i32 @BIO_read(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @BIO_write(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) 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 #1 = { noreturn 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 = { "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 = { 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 #5 = { nounwind allocsize(0) }
attributes #6 = { noreturn nounwind }
attributes #7 = { 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)"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass