; 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_context = type { ptr, i32, i32, i32, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i64, i64, i16, i64, i64, i64, i64, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, i32, i64, i64, i8, i32, ptr, ptr, ptr, i64, i32, i64, [12 x i8], [12 x i8] }
%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 }

@.str = external hidden unnamed_addr constant [97 x i8], align 1
@.str.132 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.133 = external hidden unnamed_addr constant [22 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

declare void @mbedtls_debug_print_ret(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef) #1

; 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 i32 @mbedtls_ssl_flush_output(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_ssl_handshake(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_renegotiate(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 -28800, ptr %4, align 4, !tbaa !9
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %13, label %8

8:                                                ; preds = %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %8, %1
  store i32 -28928, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %72

14:                                               ; preds = %8
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.mbedtls_ssl_config, ptr %17, i32 0, i32 46
  %19 = load i32, ptr %18, align 4
  %20 = and i32 %19, 1
  %21 = icmp eq i32 %20, 1
  br i1 %21, label %22, label %41

22:                                               ; preds = %14
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %23, i32 0, i32 1
  %25 = load i32, ptr %24, align 8, !tbaa !15
  %26 = icmp ne i32 %25, 16
  br i1 %26, label %27, label %28

27:                                               ; preds = %22
  store i32 -28928, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %72

28:                                               ; preds = %22
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %29, i32 0, i32 2
  store i32 3, ptr %30, align 4, !tbaa !16
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %31, i32 0, i32 48
  %33 = load i64, ptr %32, align 8, !tbaa !17
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %38

35:                                               ; preds = %28
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = call i32 @mbedtls_ssl_flush_output(ptr noundef %36)
  store i32 %37, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %72

38:                                               ; preds = %28
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = call i32 @ssl_write_hello_request(ptr noundef %39)
  store i32 %40, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %72

41:                                               ; preds = %14
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %42, i32 0, i32 2
  %44 = load i32, ptr %43, align 4, !tbaa !16
  %45 = icmp ne i32 %44, 1
  br i1 %45, label %46, label %61

46:                                               ; preds = %41
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mbedtls_ssl_context, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 8, !tbaa !15
  %50 = icmp ne i32 %49, 16
  br i1 %50, label %51, label %52

51:                                               ; preds = %46
  store i32 -28928, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %72

52:                                               ; preds = %46
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = call i32 @ssl_start_renegotiation(ptr noundef %53)
  store i32 %54, ptr %4, align 4, !tbaa !9
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %56, label %60

56:                                               ; preds = %52
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = load i32, ptr %4, align 4, !tbaa !9
  call void @mbedtls_debug_print_ret(ptr noundef %57, i32 noundef 1, ptr noundef @.str, i32 noundef 6684, ptr noundef @.str.132, i32 noundef %58)
  %59 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %59, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %72

60:                                               ; preds = %52
  br label %70

61:                                               ; preds = %41
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = call i32 @mbedtls_ssl_handshake(ptr noundef %62)
  store i32 %63, ptr %4, align 4, !tbaa !9
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %69

65:                                               ; preds = %61
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = load i32, ptr %4, align 4, !tbaa !9
  call void @mbedtls_debug_print_ret(ptr noundef %66, i32 noundef 1, ptr noundef @.str, i32 noundef 6692, ptr noundef @.str.133, i32 noundef %67)
  %68 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %68, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %72

69:                                               ; preds = %61
  br label %70

70:                                               ; preds = %69, %60
  %71 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %71, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %72

72:                                               ; preds = %70, %65, %56, %51, %38, %35, %27, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %73 = load i32, ptr %2, align 4
  ret i32 %73
}

; Function Attrs: nounwind uwtable
declare hidden i32 @ssl_write_hello_request(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @ssl_start_renegotiation(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"mbedtls_ssl_context", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !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, !10, i64 216, !13, i64 224, !13, i64 232, !14, i64 240, !13, i64 248, !13, i64 256, !13, i64 264, !13, i64 272, !10, i64 280, !10, i64 284, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !10, i64 336, !13, i64 344, !13, i64 352, !7, i64 360, !10, i64 364, !6, i64 368, !6, i64 376, !6, i64 384, !13, i64 392, !10, i64 400, !13, i64 408, !7, i64 416, !7, i64 428}
!13 = !{!"long", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !10, i64 8}
!16 = !{!12, !10, i64 12}
!17 = !{!12, !13, i64 352}


; 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 = external hidden unnamed_addr constant [97 x i8], align 1
@.str.132 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.133 = external hidden unnamed_addr constant [22 x i8], align 1

declare void @mbedtls_debug_print_ret(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_ssl_flush_output(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_ssl_handshake(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_renegotiate(ptr noundef %0) local_unnamed_addr #1 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %33, label %3

3:                                                ; preds = %1
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %33, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %4, i64 372
  %8 = load i32, ptr %7, align 4
  %9 = and i32 %8, 1
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %21, label %10

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load i32, ptr %11, align 8, !tbaa !13
  %.not29 = icmp eq i32 %12, 16
  br i1 %.not29, label %13, label %33

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 3, ptr %14, align 4, !tbaa !14
  %15 = getelementptr inbounds i8, ptr %0, i64 352
  %16 = load i64, ptr %15, align 8, !tbaa !15
  %.not30 = icmp eq i64 %16, 0
  br i1 %.not30, label %19, label %17

17:                                               ; preds = %13
  %18 = tail call i32 @mbedtls_ssl_flush_output(ptr noundef nonnull %0)
  br label %33

19:                                               ; preds = %13
  %20 = tail call i32 @ssl_write_hello_request(ptr noundef nonnull %0)
  br label %33

21:                                               ; preds = %6
  %22 = getelementptr inbounds i8, ptr %0, i64 12
  %23 = load i32, ptr %22, align 4, !tbaa !14
  %.not25 = icmp eq i32 %23, 1
  br i1 %.not25, label %30, label %24

24:                                               ; preds = %21
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  %26 = load i32, ptr %25, align 8, !tbaa !13
  %.not27 = icmp eq i32 %26, 16
  br i1 %.not27, label %27, label %33

27:                                               ; preds = %24
  %28 = tail call i32 @ssl_start_renegotiation(ptr noundef nonnull %0)
  %.not28 = icmp eq i32 %28, 0
  br i1 %.not28, label %33, label %29

29:                                               ; preds = %27
  tail call void @mbedtls_debug_print_ret(ptr noundef nonnull %0, i32 noundef 1, ptr noundef nonnull @.str, i32 noundef 6684, ptr noundef nonnull @.str.132, i32 noundef %28) #2
  br label %33

30:                                               ; preds = %21
  %31 = tail call i32 @mbedtls_ssl_handshake(ptr noundef nonnull %0)
  %.not26 = icmp eq i32 %31, 0
  br i1 %.not26, label %33, label %32

32:                                               ; preds = %30
  tail call void @mbedtls_debug_print_ret(ptr noundef nonnull %0, i32 noundef 1, ptr noundef nonnull @.str, i32 noundef 6692, ptr noundef nonnull @.str.133, i32 noundef %31) #2
  br label %33

33:                                               ; preds = %27, %30, %24, %10, %1, %3, %32, %29, %19, %17
  %.019 = phi i32 [ %18, %17 ], [ %20, %19 ], [ %28, %29 ], [ %31, %32 ], [ -28928, %3 ], [ -28928, %1 ], [ -28928, %10 ], [ -28928, %24 ], [ 0, %30 ], [ 0, %27 ]
  ret i32 %.019
}

; Function Attrs: nounwind uwtable
declare hidden i32 @ssl_write_hello_request(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @ssl_start_renegotiation(ptr noundef) local_unnamed_addr #1

attributes #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" }
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 = !{!"mbedtls_ssl_context", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144, !7, i64 152, !7, i64 160, !7, i64 168, !7, i64 176, !7, i64 184, !7, i64 192, !7, i64 200, !7, i64 208, !10, i64 216, !11, i64 224, !11, i64 232, !12, i64 240, !11, i64 248, !11, i64 256, !11, i64 264, !11, i64 272, !10, i64 280, !10, i64 284, !7, i64 288, !7, i64 296, !7, i64 304, !7, i64 312, !7, i64 320, !7, i64 328, !10, i64 336, !11, i64 344, !11, i64 352, !8, i64 360, !10, i64 364, !7, i64 368, !7, i64 376, !7, i64 384, !11, i64 392, !10, i64 400, !11, i64 408, !8, i64 416, !8, i64 428}
!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 = !{!"short", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!6, !10, i64 12}
!15 = !{!6, !11, i64 352}


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