; LLVM IR from dnbaker/sketch :: kthread.ll (Z6kt_foriPFvPvliES_l)

; 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.kt_for_t = type { i32, i64, ptr, ptr, ptr }
%struct.ktf_worker_t = type { ptr, i64 }

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6kt_foriPFvPvliES_l(i32 noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca %struct.kt_for_t, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !11
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, 1
  br i1 %14, label %15, label %90

15:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 40, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %16 = load ptr, ptr %6, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.kt_for_t, ptr %10, i32 0, i32 3
  store ptr %16, ptr %17, align 8, !tbaa !13
  %18 = load ptr, ptr %7, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.kt_for_t, ptr %10, i32 0, i32 4
  store ptr %18, ptr %19, align 8, !tbaa !15
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = getelementptr inbounds %struct.kt_for_t, ptr %10, i32 0, i32 0
  store i32 %20, ptr %21, align 8, !tbaa !16
  %22 = load i64, ptr %8, align 8, !tbaa !11
  %23 = getelementptr inbounds %struct.kt_for_t, ptr %10, i32 0, i32 1
  store i64 %22, ptr %23, align 8, !tbaa !17
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = mul i64 %25, 16
  %27 = alloca i8, i64 %26, align 16
  %28 = getelementptr inbounds %struct.kt_for_t, ptr %10, i32 0, i32 2
  store ptr %27, ptr %28, align 8, !tbaa !18
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = mul i64 %30, 8
  %32 = alloca i8, i64 %31, align 16
  store ptr %32, ptr %11, align 8, !tbaa !9
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %52, %15
  %34 = load i32, ptr %9, align 4, !tbaa !5
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = icmp slt i32 %34, %35
  br i1 %36, label %37, label %55

37:                                               ; preds = %33
  %38 = getelementptr inbounds %struct.kt_for_t, ptr %10, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !18
  %40 = load i32, ptr %9, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.ktf_worker_t, ptr %39, i64 %41
  %43 = getelementptr inbounds %struct.ktf_worker_t, ptr %42, i32 0, i32 0
  store ptr %10, ptr %43, align 8, !tbaa !19
  %44 = load i32, ptr %9, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds %struct.kt_for_t, ptr %10, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !18
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds %struct.ktf_worker_t, ptr %47, i64 %49
  %51 = getelementptr inbounds %struct.ktf_worker_t, ptr %50, i32 0, i32 1
  store i64 %45, ptr %51, align 8, !tbaa !21
  br label %52

52:                                               ; preds = %37
  %53 = load i32, ptr %9, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %9, align 4, !tbaa !5
  br label %33, !llvm.loop !22

55:                                               ; preds = %33
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %71, %55
  %57 = load i32, ptr %9, align 4, !tbaa !5
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = icmp slt i32 %57, %58
  br i1 %59, label %60, label %74

60:                                               ; preds = %56
  %61 = load ptr, ptr %11, align 8, !tbaa !9
  %62 = load i32, ptr %9, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i64, ptr %61, i64 %63
  %65 = getelementptr inbounds %struct.kt_for_t, ptr %10, i32 0, i32 2
  %66 = load ptr, ptr %65, align 8, !tbaa !18
  %67 = load i32, ptr %9, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds %struct.ktf_worker_t, ptr %66, i64 %68
  %70 = call i32 @pthread_create(ptr noundef %64, ptr noundef null, ptr noundef @_ZL10ktf_workerPv, ptr noundef %69) #4
  br label %71

71:                                               ; preds = %60
  %72 = load i32, ptr %9, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %9, align 4, !tbaa !5
  br label %56, !llvm.loop !24

74:                                               ; preds = %56
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %75

75:                                               ; preds = %86, %74
  %76 = load i32, ptr %9, align 4, !tbaa !5
  %77 = load i32, ptr %5, align 4, !tbaa !5
  %78 = icmp slt i32 %76, %77
  br i1 %78, label %79, label %89

79:                                               ; preds = %75
  %80 = load ptr, ptr %11, align 8, !tbaa !9
  %81 = load i32, ptr %9, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds i64, ptr %80, i64 %82
  %84 = load i64, ptr %83, align 8, !tbaa !11
  %85 = call i32 @pthread_join(i64 noundef %84, ptr noundef null)
  br label %86

86:                                               ; preds = %79
  %87 = load i32, ptr %9, align 4, !tbaa !5
  %88 = add nsw i32 %87, 1
  store i32 %88, ptr %9, align 4, !tbaa !5
  br label %75, !llvm.loop !25

89:                                               ; preds = %75
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %103

90:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  store i64 0, ptr %12, align 8, !tbaa !11
  br label %91

91:                                               ; preds = %99, %90
  %92 = load i64, ptr %12, align 8, !tbaa !11
  %93 = load i64, ptr %8, align 8, !tbaa !11
  %94 = icmp slt i64 %92, %93
  br i1 %94, label %95, label %102

95:                                               ; preds = %91
  %96 = load ptr, ptr %6, align 8, !tbaa !9
  %97 = load ptr, ptr %7, align 8, !tbaa !9
  %98 = load i64, ptr %12, align 8, !tbaa !11
  call void %96(ptr noundef %97, i64 noundef %98, i32 noundef 0)
  br label %99

99:                                               ; preds = %95
  %100 = load i64, ptr %12, align 8, !tbaa !11
  %101 = add nsw i64 %100, 1
  store i64 %101, ptr %12, align 8, !tbaa !11
  br label %91, !llvm.loop !26

102:                                              ; preds = %91
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %103

103:                                              ; preds = %102, %89
  ret void
}

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

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL10ktf_workerPv(ptr noundef) #0

declare i32 @pthread_join(i64 noundef, ptr noundef) #3

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

attributes #0 = { mustprogress 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 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 24}
!14 = !{!"_ZTS8kt_for_t", !6, i64 0, !12, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!15 = !{!14, !10, i64 32}
!16 = !{!14, !6, i64 0}
!17 = !{!14, !12, i64 8}
!18 = !{!14, !10, i64 16}
!19 = !{!20, !10, i64 0}
!20 = !{!"_ZTS12ktf_worker_t", !10, i64 0, !12, i64 8}
!21 = !{!20, !12, i64 8}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = distinct !{!24, !23}
!25 = distinct !{!25, !23}
!26 = distinct !{!26, !23}


; 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.kt_for_t = type { i32, i64, ptr, ptr, ptr }
%struct.ktf_worker_t = type { ptr, i64 }

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6kt_foriPFvPvliES_l(i32 noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.kt_for_t, align 8
  %6 = icmp sgt i32 %0, 1
  br i1 %6, label %.new, label %.preheader32

.preheader32:                                     ; preds = %4
  %7 = icmp sgt i64 %3, 0
  br i1 %7, label %.lr.ph, label %.loopexit

.new:                                             ; preds = %4
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %5) #5
  %8 = getelementptr inbounds i8, ptr %5, i64 24
  store ptr %1, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %5, i64 32
  store ptr %2, ptr %9, align 8, !tbaa !12
  store i32 %0, ptr %5, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %5, i64 8
  store i64 %3, ptr %10, align 8, !tbaa !14
  %11 = zext nneg i32 %0 to i64
  %12 = shl nuw nsw i64 %11, 4
  %13 = alloca i8, i64 %12, align 16
  %14 = getelementptr inbounds i8, ptr %5, i64 16
  store ptr %13, ptr %14, align 8, !tbaa !15
  %15 = shl nuw nsw i64 %11, 3
  %16 = alloca i8, i64 %15, align 16
  %xtraiter = and i64 %11, 1
  %unroll_iter = and i64 %11, 2147483646
  br label %17

17:                                               ; preds = %17, %.new
  %indvars.iv = phi i64 [ 0, %.new ], [ %indvars.iv.next.1, %17 ]
  %niter = phi i64 [ 0, %.new ], [ %niter.next.1, %17 ]
  %18 = getelementptr inbounds %struct.ktf_worker_t, ptr %13, i64 %indvars.iv
  store ptr %5, ptr %18, align 16, !tbaa !16
  %19 = getelementptr inbounds %struct.ktf_worker_t, ptr %13, i64 %indvars.iv, i32 1
  store i64 %indvars.iv, ptr %19, align 8, !tbaa !18
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %20 = getelementptr inbounds %struct.ktf_worker_t, ptr %13, i64 %indvars.iv.next
  store ptr %5, ptr %20, align 16, !tbaa !16
  %21 = getelementptr inbounds %struct.ktf_worker_t, ptr %13, i64 %indvars.iv.next, i32 1
  store i64 %indvars.iv.next, ptr %21, align 8, !tbaa !18
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.lr.ph36.preheader.unr-lcssa, label %17, !llvm.loop !19

.lr.ph36.preheader.unr-lcssa:                     ; preds = %17
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph36.preheader, label %.lr.ph36.preheader.epilog-lcssa

.lr.ph36.preheader.epilog-lcssa:                  ; preds = %.lr.ph36.preheader.unr-lcssa
  %22 = getelementptr inbounds %struct.ktf_worker_t, ptr %13, i64 %indvars.iv.next.1
  store ptr %5, ptr %22, align 16, !tbaa !16
  %23 = getelementptr inbounds %struct.ktf_worker_t, ptr %13, i64 %indvars.iv.next.1, i32 1
  store i64 %indvars.iv.next.1, ptr %23, align 8, !tbaa !18
  br label %.lr.ph36.preheader

.lr.ph36.preheader:                               ; preds = %.lr.ph36.preheader.unr-lcssa, %.lr.ph36.preheader.epilog-lcssa
  br label %.lr.ph36

.lr.ph38.preheader:                               ; preds = %.lr.ph36
  %smax = call i32 @llvm.smax.i32(i32 %0, i32 1)
  %wide.trip.count49 = zext nneg i32 %smax to i64
  br label %.lr.ph38

.lr.ph36:                                         ; preds = %.lr.ph36.preheader, %.lr.ph36
  %indvars.iv41 = phi i64 [ %indvars.iv.next42, %.lr.ph36 ], [ 0, %.lr.ph36.preheader ]
  %24 = getelementptr inbounds i64, ptr %16, i64 %indvars.iv41
  %25 = load ptr, ptr %14, align 8, !tbaa !15
  %26 = getelementptr inbounds %struct.ktf_worker_t, ptr %25, i64 %indvars.iv41
  %27 = call i32 @pthread_create(ptr noundef nonnull %24, ptr noundef null, ptr noundef nonnull @_ZL10ktf_workerPv, ptr noundef %26) #5
  %indvars.iv.next42 = add nuw nsw i64 %indvars.iv41, 1
  %exitcond45.not = icmp eq i64 %indvars.iv.next42, %11
  br i1 %exitcond45.not, label %.lr.ph38.preheader, label %.lr.ph36, !llvm.loop !21

.lr.ph38:                                         ; preds = %.lr.ph38.preheader, %.lr.ph38
  %indvars.iv46 = phi i64 [ 0, %.lr.ph38.preheader ], [ %indvars.iv.next47, %.lr.ph38 ]
  %28 = getelementptr inbounds i64, ptr %16, i64 %indvars.iv46
  %29 = load i64, ptr %28, align 8, !tbaa !22
  %30 = call i32 @pthread_join(i64 noundef %29, ptr noundef null)
  %indvars.iv.next47 = add nuw nsw i64 %indvars.iv46, 1
  %exitcond50.not = icmp eq i64 %indvars.iv.next47, %wide.trip.count49
  br i1 %exitcond50.not, label %._crit_edge, label %.lr.ph38, !llvm.loop !23

._crit_edge:                                      ; preds = %.lr.ph38
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %5) #5
  br label %.loopexit

.lr.ph:                                           ; preds = %.preheader32, %.lr.ph
  %.033 = phi i64 [ %31, %.lr.ph ], [ 0, %.preheader32 ]
  tail call void %1(ptr noundef %2, i64 noundef %.033, i32 noundef 0)
  %31 = add nuw nsw i64 %.033, 1
  %exitcond.not = icmp eq i64 %31, %3
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !24

.loopexit:                                        ; preds = %.lr.ph, %.preheader32, %._crit_edge
  ret void
}

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

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL10ktf_workerPv(ptr noundef) #0

declare i32 @pthread_join(i64 noundef, ptr noundef) local_unnamed_addr #3

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #4

attributes #0 = { mustprogress 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 = { 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 #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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, !11, i64 24}
!6 = !{!"_ZTS8kt_for_t", !7, i64 0, !10, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !11, i64 32}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !10, i64 8}
!15 = !{!6, !11, i64 16}
!16 = !{!17, !11, i64 0}
!17 = !{!"_ZTS12ktf_worker_t", !11, i64 0, !10, i64 8}
!18 = !{!17, !10, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}
!22 = !{!10, !10, i64 0}
!23 = distinct !{!23, !20}
!24 = distinct !{!24, !20}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass