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

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

36:                                               ; preds = %6
  %37 = load ptr, ptr %15, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %37, i32 0, i32 3
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  %40 = icmp eq ptr %39, null
  br i1 %40, label %41, label %42

41:                                               ; preds = %36, %6
  store i32 -28928, ptr %7, align 4
  store i32 1, ptr %24, align 4
  br label %132

42:                                               ; preds = %36
  %43 = load ptr, ptr %11, align 8, !tbaa !5
  %44 = load ptr, ptr %10, align 8, !tbaa !5
  %45 = ptrtoint ptr %43 to i64
  %46 = ptrtoint ptr %44 to i64
  %47 = sub i64 %45, %46
  %48 = icmp slt i64 %47, 34
  br i1 %48, label %49, label %50

49:                                               ; preds = %42
  store i32 -27136, ptr %7, align 4
  store i32 1, ptr %24, align 4
  br label %132

50:                                               ; preds = %42
  %51 = load ptr, ptr %15, align 8, !tbaa !5
  %52 = call i32 @ssl_ticket_update_keys(ptr noundef %51)
  store i32 %52, ptr %14, align 4, !tbaa !14
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %55

54:                                               ; preds = %50
  br label %130

55:                                               ; preds = %50
  %56 = load ptr, ptr %15, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %15, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %58, i32 0, i32 1
  %60 = load i8, ptr %59, align 8, !tbaa !15
  %61 = zext i8 %60 to i64
  %62 = getelementptr inbounds [2 x %struct.mbedtls_ssl_ticket_key], ptr %57, i64 0, i64 %61
  store ptr %62, ptr %16, align 8, !tbaa !5
  %63 = load ptr, ptr %15, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %63, i32 0, i32 2
  %65 = load i32, ptr %64, align 4, !tbaa !16
  %66 = load ptr, ptr %13, align 8, !tbaa !5
  store i32 %65, ptr %66, align 4, !tbaa !14
  %67 = load ptr, ptr %17, align 8, !tbaa !5
  %68 = load ptr, ptr %16, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.mbedtls_ssl_ticket_key, ptr %68, i32 0, i32 0
  %70 = getelementptr inbounds [4 x i8], ptr %69, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %67, ptr align 8 %70, i64 4, i1 false)
  %71 = load ptr, ptr %15, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %71, i32 0, i32 3
  %73 = load ptr, ptr %72, align 8, !tbaa !11
  %74 = load ptr, ptr %15, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %74, i32 0, i32 4
  %76 = load ptr, ptr %75, align 8, !tbaa !17
  %77 = load ptr, ptr %18, align 8, !tbaa !5
  %78 = call i32 %73(ptr noundef %76, ptr noundef %77, i64 noundef 12)
  store i32 %78, ptr %14, align 4, !tbaa !14
  %79 = icmp ne i32 %78, 0
  br i1 %79, label %80, label %81

80:                                               ; preds = %55
  br label %130

81:                                               ; preds = %55
  %82 = load ptr, ptr %9, align 8, !tbaa !5
  %83 = load ptr, ptr %20, align 8, !tbaa !5
  %84 = load ptr, ptr %11, align 8, !tbaa !5
  %85 = load ptr, ptr %20, align 8, !tbaa !5
  %86 = ptrtoint ptr %84 to i64
  %87 = ptrtoint ptr %85 to i64
  %88 = sub i64 %86, %87
  %89 = call i32 @ssl_save_session(ptr noundef %82, ptr noundef %83, i64 noundef %88, ptr noundef %22)
  store i32 %89, ptr %14, align 4, !tbaa !14
  %90 = icmp ne i32 %89, 0
  br i1 %90, label %94, label %91

91:                                               ; preds = %81
  %92 = load i64, ptr %22, align 8, !tbaa !9
  %93 = icmp ugt i64 %92, 65535
  br i1 %93, label %94, label %95

94:                                               ; preds = %91, %81
  br label %130

95:                                               ; preds = %91
  %96 = load i64, ptr %22, align 8, !tbaa !9
  %97 = lshr i64 %96, 8
  %98 = and i64 %97, 255
  %99 = trunc i64 %98 to i8
  %100 = load ptr, ptr %19, align 8, !tbaa !5
  %101 = getelementptr inbounds i8, ptr %100, i64 0
  store i8 %99, ptr %101, align 1, !tbaa !18
  %102 = load i64, ptr %22, align 8, !tbaa !9
  %103 = and i64 %102, 255
  %104 = trunc i64 %103 to i8
  %105 = load ptr, ptr %19, align 8, !tbaa !5
  %106 = getelementptr inbounds i8, ptr %105, i64 1
  store i8 %104, ptr %106, align 1, !tbaa !18
  %107 = load ptr, ptr %20, align 8, !tbaa !5
  %108 = load i64, ptr %22, align 8, !tbaa !9
  %109 = getelementptr inbounds i8, ptr %107, i64 %108
  store ptr %109, ptr %21, align 8, !tbaa !5
  %110 = load ptr, ptr %16, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.mbedtls_ssl_ticket_key, ptr %110, i32 0, i32 2
  %112 = load ptr, ptr %18, align 8, !tbaa !5
  %113 = load ptr, ptr %17, align 8, !tbaa !5
  %114 = load ptr, ptr %20, align 8, !tbaa !5
  %115 = load i64, ptr %22, align 8, !tbaa !9
  %116 = load ptr, ptr %20, align 8, !tbaa !5
  %117 = load ptr, ptr %21, align 8, !tbaa !5
  %118 = call i32 @mbedtls_cipher_auth_encrypt(ptr noundef %111, ptr noundef %112, i64 noundef 12, ptr noundef %113, i64 noundef 18, ptr noundef %114, i64 noundef %115, ptr noundef %116, ptr noundef %23, ptr noundef %117, i64 noundef 16)
  store i32 %118, ptr %14, align 4, !tbaa !14
  %119 = icmp ne i32 %118, 0
  br i1 %119, label %120, label %121

120:                                              ; preds = %95
  br label %130

121:                                              ; preds = %95
  %122 = load i64, ptr %23, align 8, !tbaa !9
  %123 = load i64, ptr %22, align 8, !tbaa !9
  %124 = icmp ne i64 %122, %123
  br i1 %124, label %125, label %126

125:                                              ; preds = %121
  store i32 -27648, ptr %14, align 4, !tbaa !14
  br label %130

126:                                              ; preds = %121
  %127 = load i64, ptr %23, align 8, !tbaa !9
  %128 = add i64 34, %127
  %129 = load ptr, ptr %12, align 8, !tbaa !5
  store i64 %128, ptr %129, align 8, !tbaa !9
  br label %130

130:                                              ; preds = %126, %125, %120, %94, %80, %54
  %131 = load i32, ptr %14, align 4, !tbaa !14
  store i32 %131, ptr %7, align 4
  store i32 1, ptr %24, align 4
  br label %132

132:                                              ; preds = %130, %49, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %23) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %22) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %21) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %20) #4
  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 4, ptr %14) #4
  %133 = load i32, ptr %7, align 4
  ret i32 %133
}

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

; 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) #2

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

declare i32 @mbedtls_cipher_auth_encrypt(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef, i64 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 }

!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 = !{!12, !7, i64 192}
!16 = !{!12, !13, i64 196}
!17 = !{!12, !6, i64 208}
!18 = !{!7, !7, i64 0}


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

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

; 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
define dso_local i32 @mbedtls_ssl_ticket_write(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr nocapture noundef writeonly %4, ptr nocapture noundef writeonly %5) local_unnamed_addr #1 {
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = getelementptr inbounds i8, ptr %2, i64 4
  %10 = getelementptr inbounds i8, ptr %2, i64 16
  %11 = getelementptr inbounds i8, ptr %2, i64 18
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #3
  store i64 0, ptr %4, align 8, !tbaa !5
  %12 = icmp eq ptr %0, null
  br i1 %12, label %56, label %13

13:                                               ; preds = %6
  %14 = getelementptr inbounds i8, ptr %0, i64 200
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = icmp eq ptr %15, null
  br i1 %16, label %56, label %17

17:                                               ; preds = %13
  %18 = ptrtoint ptr %3 to i64
  %19 = ptrtoint ptr %2 to i64
  %20 = sub i64 %18, %19
  %21 = icmp slt i64 %20, 34
  br i1 %21, label %56, label %22

22:                                               ; preds = %17
  %23 = tail call i32 @ssl_ticket_update_keys(ptr noundef nonnull %0)
  %.not = icmp eq i32 %23, 0
  br i1 %.not, label %24, label %56

24:                                               ; preds = %22
  %25 = getelementptr inbounds i8, ptr %0, i64 192
  %26 = load i8, ptr %25, align 8, !tbaa !13
  %27 = zext i8 %26 to i64
  %28 = getelementptr inbounds [2 x %struct.mbedtls_ssl_ticket_key], ptr %0, i64 0, i64 %27
  %29 = getelementptr inbounds i8, ptr %0, i64 196
  %30 = load i32, ptr %29, align 4, !tbaa !14
  store i32 %30, ptr %5, align 4, !tbaa !15
  %31 = load i32, ptr %28, align 8
  store i32 %31, ptr %2, align 1
  %32 = load ptr, ptr %14, align 8, !tbaa !9
  %33 = getelementptr inbounds i8, ptr %0, i64 208
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  %35 = tail call i32 %32(ptr noundef %34, ptr noundef nonnull %9, i64 noundef 12) #3
  %.not43 = icmp eq i32 %35, 0
  br i1 %.not43, label %36, label %56

36:                                               ; preds = %24
  %37 = ptrtoint ptr %11 to i64
  %38 = sub i64 %18, %37
  %39 = call i32 @ssl_save_session(ptr noundef %1, ptr noundef nonnull %11, i64 noundef %38, ptr noundef nonnull %7)
  %40 = icmp ne i32 %39, 0
  %41 = load i64, ptr %7, align 8
  %42 = icmp ugt i64 %41, 65535
  %or.cond = select i1 %40, i1 true, i1 %42
  br i1 %or.cond, label %56, label %43

43:                                               ; preds = %36
  %44 = lshr i64 %41, 8
  %45 = trunc nuw i64 %44 to i8
  store i8 %45, ptr %10, align 1, !tbaa !17
  %46 = trunc i64 %41 to i8
  %47 = getelementptr inbounds i8, ptr %2, i64 17
  store i8 %46, ptr %47, align 1, !tbaa !17
  %48 = getelementptr inbounds i8, ptr %11, i64 %41
  %49 = getelementptr inbounds i8, ptr %28, i64 8
  %50 = call i32 @mbedtls_cipher_auth_encrypt(ptr noundef nonnull %49, ptr noundef nonnull %9, i64 noundef 12, ptr noundef nonnull %2, i64 noundef 18, ptr noundef nonnull %11, i64 noundef %41, ptr noundef nonnull %11, ptr noundef nonnull %8, ptr noundef nonnull %48, i64 noundef 16) #3
  %.not44 = icmp eq i32 %50, 0
  br i1 %.not44, label %51, label %56

51:                                               ; preds = %43
  %52 = load i64, ptr %8, align 8, !tbaa !5
  %53 = load i64, ptr %7, align 8, !tbaa !5
  %.not45 = icmp eq i64 %52, %53
  br i1 %.not45, label %54, label %56

54:                                               ; preds = %51
  %55 = add i64 %52, 34
  store i64 %55, ptr %4, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %54, %22, %24, %36, %43, %51, %17, %6, %13
  %.0 = phi i32 [ -28928, %13 ], [ -28928, %6 ], [ -27136, %17 ], [ %23, %22 ], [ %35, %24 ], [ %39, %36 ], [ %50, %43 ], [ 0, %54 ], [ -27648, %51 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #3
  ret i32 %.0
}

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

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

declare i32 @mbedtls_cipher_auth_encrypt(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

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 }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 200}
!10 = !{!"", !7, i64 0, !7, i64 192, !11, i64 196, !12, i64 200, !12, i64 208}
!11 = !{!"int", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!10, !7, i64 192}
!14 = !{!10, !11, i64 196}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !12, i64 208}
!17 = !{!7, !7, i64 0}


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