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

; 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_ticket_context = type { [2 x %struct.mbedtls_ssl_ticket_key], i8, i32, ptr, ptr }
%struct.mbedtls_ssl_ticket_key = type { [4 x i8], i32, %struct.mbedtls_cipher_context_t }
%struct.mbedtls_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr }
%struct.mbedtls_ssl_session = type { i64, i32, i32, i64, [32 x i8], [48 x i8], ptr, i32, ptr, i64, i32, i8, i32, i32 }

; 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
declare hidden i32 @ssl_ticket_update_keys(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_ticket_parse(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = alloca ptr, align 8
  %18 = alloca i64, align 8
  %19 = alloca i64, align 8
  %20 = alloca i32, align 4
  %21 = alloca i64, align 8
  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 i64 %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %22, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %23, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 4
  store ptr %25, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %26 = load ptr, ptr %14, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 12
  store ptr %27, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %28 = load ptr, ptr %15, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i64 2
  store ptr %29, ptr %16, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #4
  %30 = load ptr, ptr %11, align 8, !tbaa !5
  %31 = icmp eq ptr %30, null
  br i1 %31, label %37, label %32

32:                                               ; preds = %4
  %33 = load ptr, ptr %11, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %33, i32 0, i32 3
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = icmp eq ptr %35, null
  br i1 %36, label %37, label %38

37:                                               ; preds = %32, %4
  store i32 -28928, ptr %5, align 4
  store i32 1, ptr %20, align 4
  br label %127

38:                                               ; preds = %32
  %39 = load i64, ptr %9, align 8, !tbaa !9
  %40 = icmp ult i64 %39, 34
  br i1 %40, label %41, label %42

41:                                               ; preds = %38
  store i32 -28928, ptr %5, align 4
  store i32 1, ptr %20, align 4
  br label %127

42:                                               ; preds = %38
  %43 = load ptr, ptr %11, align 8, !tbaa !5
  %44 = call i32 @ssl_ticket_update_keys(ptr noundef %43)
  store i32 %44, ptr %10, align 4, !tbaa !14
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %47

46:                                               ; preds = %42
  br label %125

47:                                               ; preds = %42
  %48 = load ptr, ptr %15, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %48, i64 0
  %50 = load i8, ptr %49, align 1, !tbaa !15
  %51 = zext i8 %50 to i32
  %52 = shl i32 %51, 8
  %53 = load ptr, ptr %15, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i64 1
  %55 = load i8, ptr %54, align 1, !tbaa !15
  %56 = zext i8 %55 to i32
  %57 = or i32 %52, %56
  %58 = sext i32 %57 to i64
  store i64 %58, ptr %18, align 8, !tbaa !9
  %59 = load ptr, ptr %16, align 8, !tbaa !5
  %60 = load i64, ptr %18, align 8, !tbaa !9
  %61 = getelementptr inbounds i8, ptr %59, i64 %60
  store ptr %61, ptr %17, align 8, !tbaa !5
  %62 = load i64, ptr %9, align 8, !tbaa !9
  %63 = load i64, ptr %18, align 8, !tbaa !9
  %64 = add i64 18, %63
  %65 = add i64 %64, 16
  %66 = icmp ne i64 %62, %65
  br i1 %66, label %67, label %68

67:                                               ; preds = %47
  store i32 -28928, ptr %10, align 4, !tbaa !14
  br label %125

68:                                               ; preds = %47
  %69 = load ptr, ptr %11, align 8, !tbaa !5
  %70 = load ptr, ptr %13, align 8, !tbaa !5
  %71 = call ptr @ssl_ticket_select_key(ptr noundef %69, ptr noundef %70)
  store ptr %71, ptr %12, align 8, !tbaa !5
  %72 = icmp eq ptr %71, null
  br i1 %72, label %73, label %74

73:                                               ; preds = %68
  store i32 -28032, ptr %10, align 4, !tbaa !14
  br label %125

74:                                               ; preds = %68
  %75 = load ptr, ptr %12, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.mbedtls_ssl_ticket_key, ptr %75, i32 0, i32 2
  %77 = load ptr, ptr %14, align 8, !tbaa !5
  %78 = load ptr, ptr %13, align 8, !tbaa !5
  %79 = load ptr, ptr %16, align 8, !tbaa !5
  %80 = load i64, ptr %18, align 8, !tbaa !9
  %81 = load ptr, ptr %16, align 8, !tbaa !5
  %82 = load ptr, ptr %17, align 8, !tbaa !5
  %83 = call i32 @mbedtls_cipher_auth_decrypt(ptr noundef %76, ptr noundef %77, i64 noundef 12, ptr noundef %78, i64 noundef 18, ptr noundef %79, i64 noundef %80, ptr noundef %81, ptr noundef %19, ptr noundef %82, i64 noundef 16)
  store i32 %83, ptr %10, align 4, !tbaa !14
  %84 = icmp ne i32 %83, 0
  br i1 %84, label %85, label %90

85:                                               ; preds = %74
  %86 = load i32, ptr %10, align 4, !tbaa !14
  %87 = icmp eq i32 %86, -25344
  br i1 %87, label %88, label %89

88:                                               ; preds = %85
  store i32 -29056, ptr %10, align 4, !tbaa !14
  br label %89

89:                                               ; preds = %88, %85
  br label %125

90:                                               ; preds = %74
  %91 = load i64, ptr %19, align 8, !tbaa !9
  %92 = load i64, ptr %18, align 8, !tbaa !9
  %93 = icmp ne i64 %91, %92
  br i1 %93, label %94, label %95

94:                                               ; preds = %90
  store i32 -27648, ptr %10, align 4, !tbaa !14
  br label %125

95:                                               ; preds = %90
  %96 = load ptr, ptr %7, align 8, !tbaa !5
  %97 = load ptr, ptr %16, align 8, !tbaa !5
  %98 = load i64, ptr %19, align 8, !tbaa !9
  %99 = call i32 @ssl_load_session(ptr noundef %96, ptr noundef %97, i64 noundef %98)
  store i32 %99, ptr %10, align 4, !tbaa !14
  %100 = icmp ne i32 %99, 0
  br i1 %100, label %101, label %102

101:                                              ; preds = %95
  br label %125

102:                                              ; preds = %95
  call void @llvm.lifetime.start.p0(i64 8, ptr %21) #4
  %103 = call i64 @time(ptr noundef null) #4
  store i64 %103, ptr %21, align 8, !tbaa !9
  %104 = load i64, ptr %21, align 8, !tbaa !9
  %105 = load ptr, ptr %7, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.mbedtls_ssl_session, ptr %105, i32 0, i32 0
  %107 = load i64, ptr %106, align 8, !tbaa !16
  %108 = icmp slt i64 %104, %107
  br i1 %108, label %120, label %109

109:                                              ; preds = %102
  %110 = load i64, ptr %21, align 8, !tbaa !9
  %111 = load ptr, ptr %7, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.mbedtls_ssl_session, ptr %111, i32 0, i32 0
  %113 = load i64, ptr %112, align 8, !tbaa !16
  %114 = sub nsw i64 %110, %113
  %115 = trunc i64 %114 to i32
  %116 = load ptr, ptr %11, align 8, !tbaa !5
  %117 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %116, i32 0, i32 2
  %118 = load i32, ptr %117, align 4, !tbaa !18
  %119 = icmp ugt i32 %115, %118
  br i1 %119, label %120, label %121

120:                                              ; preds = %109, %102
  store i32 -28032, ptr %10, align 4, !tbaa !14
  store i32 2, ptr %20, align 4
  br label %122

121:                                              ; preds = %109
  store i32 0, ptr %20, align 4
  br label %122

122:                                              ; preds = %121, %120
  call void @llvm.lifetime.end.p0(i64 8, ptr %21) #4
  %123 = load i32, ptr %20, align 4
  switch i32 %123, label %127 [
    i32 0, label %124
    i32 2, label %125
  ]

124:                                              ; preds = %122
  br label %125

125:                                              ; preds = %124, %122, %101, %94, %89, %73, %67, %46
  %126 = load i32, ptr %10, align 4, !tbaa !14
  store i32 %126, ptr %5, align 4
  store i32 1, ptr %20, align 4
  br label %127

127:                                              ; preds = %125, %122, %41, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %128 = load i32, ptr %5, align 4
  ret i32 %128
}

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

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

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

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #3

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

!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 200}
!12 = !{!"", !7, i64 0, !7, i64 192, !13, i64 196, !6, i64 200, !6, i64 208}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !10, i64 0}
!17 = !{!"mbedtls_ssl_session", !10, i64 0, !13, i64 8, !13, i64 12, !10, i64 16, !7, i64 24, !7, i64 56, !6, i64 104, !13, i64 112, !6, i64 120, !10, i64 128, !13, i64 136, !7, i64 140, !13, i64 144, !13, i64 148}
!18 = !{!12, !13, i64 196}


; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_ticket_parse(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #1 {
  %5 = alloca i64, align 8
  %6 = getelementptr inbounds i8, ptr %2, i64 4
  %7 = getelementptr inbounds i8, ptr %2, i64 16
  %8 = getelementptr inbounds i8, ptr %2, i64 18
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  %9 = icmp eq ptr %0, null
  br i1 %9, label %49, label %10

10:                                               ; preds = %4
  %11 = getelementptr inbounds i8, ptr %0, i64 200
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  %14 = icmp ult i64 %3, 34
  %or.cond = or i1 %14, %13
  br i1 %or.cond, label %49, label %15

15:                                               ; preds = %10
  %16 = tail call i32 @ssl_ticket_update_keys(ptr noundef nonnull %0)
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %17, label %49

17:                                               ; preds = %15
  %18 = load i8, ptr %7, align 1, !tbaa !11
  %19 = zext i8 %18 to i64
  %20 = shl nuw nsw i64 %19, 8
  %21 = getelementptr inbounds i8, ptr %2, i64 17
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = zext i8 %22 to i64
  %24 = or disjoint i64 %20, %23
  %25 = getelementptr inbounds i8, ptr %8, i64 %24
  %26 = add nuw nsw i64 %24, 34
  %.not47 = icmp eq i64 %26, %3
  br i1 %.not47, label %27, label %49

27:                                               ; preds = %17
  %28 = tail call ptr @ssl_ticket_select_key(ptr noundef nonnull %0, ptr noundef nonnull %2)
  %29 = icmp eq ptr %28, null
  br i1 %29, label %49, label %30

30:                                               ; preds = %27
  %31 = getelementptr inbounds i8, ptr %28, i64 8
  %32 = call i32 @mbedtls_cipher_auth_decrypt(ptr noundef nonnull %31, ptr noundef nonnull %6, i64 noundef 12, ptr noundef nonnull %2, i64 noundef 18, ptr noundef nonnull %8, i64 noundef %24, ptr noundef nonnull %8, ptr noundef nonnull %5, ptr noundef nonnull %25, i64 noundef 16) #4
  %.not48 = icmp eq i32 %32, 0
  br i1 %.not48, label %35, label %33

33:                                               ; preds = %30
  %34 = icmp eq i32 %32, -25344
  %spec.store.select = select i1 %34, i32 -29056, i32 %32
  br label %49

35:                                               ; preds = %30
  %36 = load i64, ptr %5, align 8, !tbaa !12
  %.not49 = icmp eq i64 %36, %24
  br i1 %.not49, label %37, label %49

37:                                               ; preds = %35
  %38 = call i32 @ssl_load_session(ptr noundef %1, ptr noundef nonnull %8, i64 noundef %24)
  %.not50 = icmp eq i32 %38, 0
  br i1 %.not50, label %39, label %49

39:                                               ; preds = %37
  %40 = call i64 @time(ptr noundef null) #4
  %41 = load i64, ptr %1, align 8, !tbaa !14
  %42 = icmp slt i64 %40, %41
  br i1 %42, label %49, label %43

43:                                               ; preds = %39
  %44 = sub nsw i64 %40, %41
  %45 = trunc i64 %44 to i32
  %46 = getelementptr inbounds i8, ptr %0, i64 196
  %47 = load i32, ptr %46, align 4, !tbaa !16
  %48 = icmp ult i32 %47, %45
  %spec.select = select i1 %48, i32 -28032, i32 0
  br label %49

49:                                               ; preds = %43, %33, %15, %37, %17, %27, %35, %39, %4, %10
  %.0 = phi i32 [ -28928, %10 ], [ -28928, %4 ], [ %16, %15 ], [ %spec.store.select, %33 ], [ %38, %37 ], [ -28928, %17 ], [ -28032, %27 ], [ -27648, %35 ], [ -28032, %39 ], [ %spec.select, %43 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  ret i32 %.0
}

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

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

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

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress 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 "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 }

!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, !10, i64 200}
!6 = !{!"", !7, i64 0, !7, i64 192, !9, i64 196, !10, i64 200, !10, i64 208}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !13, i64 0}
!15 = !{!"mbedtls_ssl_session", !13, i64 0, !9, i64 8, !9, i64 12, !13, i64 16, !7, i64 24, !7, i64 56, !10, i64 104, !9, i64 112, !10, i64 120, !13, i64 128, !9, i64 136, !7, i64 140, !9, i64 144, !9, i64 148}
!16 = !{!6, !9, i64 196}


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