; LLVM IR from machinefi/psa-crypto-esp32 :: crypto (hmac)

; 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.tc_hmac_state_struct = type { %struct.tc_sha256_state_struct, [128 x i8] }
%struct.tc_sha256_state_struct = type { [8 x i32], i64, [64 x i8], i64 }

; Function Attrs: nounwind uwtable
define dso_local i32 @tc_hmac_set_key(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca [64 x i8], align 16
  %9 = alloca %struct.tc_hmac_state_struct, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %18, label %12

12:                                               ; preds = %3
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %18, label %15

15:                                               ; preds = %12
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %15, %12, %3
  store i32 0, ptr %4, align 4
  br label %62

19:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 64, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 240, ptr %9) #3
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = icmp ule i32 %20, 64
  br i1 %21, label %22, label %39

22:                                               ; preds = %19
  %23 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %9, i32 0, i32 0
  %24 = call i32 @tc_sha256_init(ptr noundef %23)
  %25 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %9, i32 0, i32 0
  %26 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = call i32 @tc_sha256_update(ptr noundef %25, ptr noundef %26, i64 noundef %28)
  %30 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %9, i32 0, i32 1
  %31 = getelementptr inbounds [128 x i8], ptr %30, i64 0, i64 32
  %32 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %9, i32 0, i32 0
  %33 = call i32 @tc_sha256_final(ptr noundef %31, ptr noundef %32)
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %34, i32 0, i32 1
  %36 = getelementptr inbounds [128 x i8], ptr %35, i64 0, i64 0
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load i32, ptr %7, align 4, !tbaa !9
  call void @rekey(ptr noundef %36, ptr noundef %37, i32 noundef %38)
  br label %61

39:                                               ; preds = %19
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %40, i32 0, i32 0
  %42 = call i32 @tc_sha256_init(ptr noundef %41)
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = call i32 @tc_sha256_update(ptr noundef %44, ptr noundef %45, i64 noundef %47)
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %49, i32 0, i32 1
  %51 = getelementptr inbounds [128 x i8], ptr %50, i64 0, i64 32
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %52, i32 0, i32 0
  %54 = call i32 @tc_sha256_final(ptr noundef %51, ptr noundef %53)
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %55, i32 0, i32 1
  %57 = getelementptr inbounds [128 x i8], ptr %56, i64 0, i64 0
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.tc_hmac_state_struct, ptr %58, i32 0, i32 1
  %60 = getelementptr inbounds [128 x i8], ptr %59, i64 0, i64 32
  call void @rekey(ptr noundef %57, ptr noundef %60, i32 noundef 32)
  br label %61

61:                                               ; preds = %39, %22
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 240, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 64, ptr %8) #3
  br label %62

62:                                               ; preds = %61, %18
  %63 = load i32, ptr %4, align 4
  ret i32 %63
}

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

declare i32 @tc_sha256_init(ptr noundef) #2

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

declare i32 @tc_sha256_final(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @rekey(ptr noundef, ptr noundef, i32 noundef) #0

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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}


; 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.tc_hmac_state_struct = type { %struct.tc_sha256_state_struct, [128 x i8] }
%struct.tc_sha256_state_struct = type { [8 x i32], i64, [64 x i8], i64 }

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @tc_hmac_set_key(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca [64 x i8], align 16
  %5 = alloca %struct.tc_hmac_state_struct, align 8
  %6 = icmp eq ptr %0, null
  %7 = icmp eq ptr %1, null
  %or.cond = or i1 %6, %7
  %8 = icmp eq i32 %2, 0
  %or.cond3 = or i1 %or.cond, %8
  br i1 %or.cond3, label %25, label %9

9:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 240, ptr nonnull %5) #3
  %10 = icmp ult i32 %2, 65
  %11 = zext i32 %2 to i64
  br i1 %10, label %12, label %18

12:                                               ; preds = %9
  %13 = call i32 @tc_sha256_init(ptr noundef nonnull %5) #3
  %14 = call i32 @tc_sha256_update(ptr noundef nonnull %5, ptr noundef nonnull %4, i64 noundef %11) #3
  %15 = getelementptr inbounds i8, ptr %5, i64 144
  %16 = call i32 @tc_sha256_final(ptr noundef nonnull %15, ptr noundef nonnull %5) #3
  %17 = getelementptr inbounds i8, ptr %0, i64 112
  call void @rekey(ptr noundef nonnull %17, ptr noundef nonnull %1, i32 noundef %2)
  br label %24

18:                                               ; preds = %9
  %19 = tail call i32 @tc_sha256_init(ptr noundef nonnull %0) #3
  %20 = tail call i32 @tc_sha256_update(ptr noundef nonnull %0, ptr noundef nonnull %1, i64 noundef %11) #3
  %21 = getelementptr inbounds i8, ptr %0, i64 112
  %22 = getelementptr inbounds i8, ptr %0, i64 144
  %23 = tail call i32 @tc_sha256_final(ptr noundef nonnull %22, ptr noundef nonnull %0) #3
  tail call void @rekey(ptr noundef nonnull %21, ptr noundef nonnull %22, i32 noundef 32)
  br label %24

24:                                               ; preds = %18, %12
  call void @llvm.lifetime.end.p0(i64 240, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %4) #3
  br label %25

25:                                               ; preds = %3, %24
  %.0 = phi i32 [ 1, %24 ], [ 0, %3 ]
  ret i32 %.0
}

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

declare i32 @tc_sha256_init(ptr noundef) local_unnamed_addr #2

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

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

; Function Attrs: nounwind uwtable
declare hidden void @rekey(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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)"}


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