; LLVM IR from attractivechaos/klib :: kurl (sha1_init_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.sha1nfo = type { %union.anon, i8, %union.anon.0, i32, [64 x i8], [20 x i8] }
%union.anon = type { [16 x i32] }
%union.anon.0 = type { [5 x 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 void @sha1_init(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @sha1_write1(ptr noundef, i8 noundef zeroext) #2

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

; Function Attrs: nounwind uwtable
define dso_local void @sha1_init_hmac(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.sha1nfo, ptr %8, i32 0, i32 4
  %10 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 4 %10, i8 0, i64 64, i1 false)
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = icmp sgt i32 %11, 64
  br i1 %12, label %13, label %30

13:                                               ; preds = %3
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sha1_init(ptr noundef %14)
  br label %15

15:                                               ; preds = %19, %13
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = add nsw i32 %16, -1
  store i32 %17, ptr %6, align 4, !tbaa !9
  %18 = icmp ne i32 %16, 0
  br i1 %18, label %19, label %24

19:                                               ; preds = %15
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i32 1
  store ptr %22, ptr %5, align 8, !tbaa !5
  %23 = load i8, ptr %21, align 1, !tbaa !11
  call void @sha1_write1(ptr noundef %20, i8 noundef zeroext %23)
  br label %15, !llvm.loop !12

24:                                               ; preds = %15
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.sha1nfo, ptr %25, i32 0, i32 4
  %27 = getelementptr inbounds [64 x i8], ptr %26, i64 0, i64 0
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = call ptr @sha1_final(ptr noundef %28)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %27, ptr align 1 %29, i64 20, i1 false)
  br label %37

30:                                               ; preds = %3
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.sha1nfo, ptr %31, i32 0, i32 4
  %33 = getelementptr inbounds [64 x i8], ptr %32, i64 0, i64 0
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %33, ptr align 1 %34, i64 %36, i1 false)
  br label %37

37:                                               ; preds = %30, %24
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sha1_init(ptr noundef %38)
  store i8 0, ptr %7, align 1, !tbaa !11
  br label %39

39:                                               ; preds = %54, %37
  %40 = load i8, ptr %7, align 1, !tbaa !11
  %41 = zext i8 %40 to i32
  %42 = icmp slt i32 %41, 64
  br i1 %42, label %43, label %57

43:                                               ; preds = %39
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.sha1nfo, ptr %45, i32 0, i32 4
  %47 = load i8, ptr %7, align 1, !tbaa !11
  %48 = zext i8 %47 to i64
  %49 = getelementptr inbounds [64 x i8], ptr %46, i64 0, i64 %48
  %50 = load i8, ptr %49, align 1, !tbaa !11
  %51 = zext i8 %50 to i32
  %52 = xor i32 %51, 54
  %53 = trunc i32 %52 to i8
  call void @sha1_write1(ptr noundef %44, i8 noundef zeroext %53)
  br label %54

54:                                               ; preds = %43
  %55 = load i8, ptr %7, align 1, !tbaa !11
  %56 = add i8 %55, 1
  store i8 %56, ptr %7, align 1, !tbaa !11
  br label %39, !llvm.loop !14

57:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @sha1_write1(ptr noundef, i8 noundef zeroext) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @sha1_init_hmac(ptr noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %0, i64 92
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(64) %4, i8 0, i64 64, i1 false)
  %5 = icmp sgt i32 %2, 64
  br i1 %5, label %6, label %13

6:                                                ; preds = %3
  tail call void @sha1_init(ptr noundef nonnull %0)
  br label %7

7:                                                ; preds = %6, %7
  %.01620 = phi i32 [ %2, %6 ], [ %8, %7 ]
  %.01719 = phi ptr [ %1, %6 ], [ %9, %7 ]
  %8 = add nsw i32 %.01620, -1
  %9 = getelementptr inbounds i8, ptr %.01719, i64 1
  %10 = load i8, ptr %.01719, align 1, !tbaa !5
  tail call void @sha1_write1(ptr noundef %0, i8 noundef zeroext %10)
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %11, label %7, !llvm.loop !8

11:                                               ; preds = %7
  %12 = tail call ptr @sha1_final(ptr noundef %0)
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(20) %4, ptr noundef nonnull align 1 dereferenceable(20) %12, i64 20, i1 false)
  br label %15

13:                                               ; preds = %3
  %14 = sext i32 %2 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 4 %4, ptr align 1 %1, i64 %14, i1 false)
  br label %15

15:                                               ; preds = %13, %11
  tail call void @sha1_init(ptr noundef nonnull %0)
  br label %16

16:                                               ; preds = %15, %16
  %indvars.iv = phi i64 [ 0, %15 ], [ %indvars.iv.next, %16 ]
  %17 = getelementptr inbounds [64 x i8], ptr %4, i64 0, i64 %indvars.iv
  %18 = load i8, ptr %17, align 1, !tbaa !5
  %19 = xor i8 %18, 54
  tail call void @sha1_write1(ptr noundef nonnull %0, i8 noundef zeroext %19)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 64
  br i1 %exitcond.not, label %20, label %16, !llvm.loop !10

20:                                               ; preds = %16
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


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