; LLVM IR from ryanlayer/giggle :: src (wah)

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

@WAH_SIZE = external dso_local global i32, align 4
@WAH_MAX_FILL_WORDS = external dso_local global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local ptr @wah_init(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %10 = load i32, ptr @WAH_SIZE, align 4, !tbaa !5
  %11 = sub i32 %10, 1
  store i32 %11, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = add i32 %12, %13
  %15 = sub i32 %14, 1
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = udiv i32 %15, %16
  store i32 %17, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = icmp ugt i32 %18, 1
  br i1 %19, label %20, label %27

20:                                               ; preds = %1
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = load i32, ptr @WAH_MAX_FILL_WORDS, align 4, !tbaa !5
  %23 = add i32 %21, %22
  %24 = sub i32 %23, 1
  %25 = load i32, ptr @WAH_MAX_FILL_WORDS, align 4, !tbaa !5
  %26 = udiv i32 %24, %25
  br label %28

27:                                               ; preds = %1
  br label %28

28:                                               ; preds = %27, %20
  %29 = phi i32 [ %26, %20 ], [ 0, %27 ]
  %30 = add i32 1, %29
  store i32 %30, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = load i32, ptr @WAH_SIZE, align 4, !tbaa !5
  %33 = udiv i32 %32, 8
  %34 = mul i32 %31, %33
  %35 = zext i32 %34 to i64
  %36 = mul i64 %35, 1
  %37 = add i64 4, %36
  %38 = call noalias ptr @malloc(i64 noundef %37) #4
  store ptr %38, ptr %6, align 8, !tbaa !9
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  %41 = getelementptr inbounds i32, ptr %40, i64 0
  store i32 %39, ptr %41, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  br label %42

42:                                               ; preds = %56, %28
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = icmp ugt i32 %43, %44
  br i1 %45, label %46, label %75

46:                                               ; preds = %42
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = sub i32 %47, 1
  %49 = load i32, ptr @WAH_MAX_FILL_WORDS, align 4, !tbaa !5
  %50 = icmp ult i32 %48, %49
  br i1 %50, label %51, label %54

51:                                               ; preds = %46
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sub i32 %52, 1
  br label %56

54:                                               ; preds = %46
  %55 = load i32, ptr @WAH_MAX_FILL_WORDS, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %54, %51
  %57 = phi i32 [ %53, %51 ], [ %55, %54 ]
  store i32 %57, ptr %9, align 4, !tbaa !5
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = shl i32 1, %58
  %60 = load i32, ptr %9, align 4, !tbaa !5
  %61 = add i32 %59, %60
  store i32 %61, ptr %7, align 4, !tbaa !5
  %62 = load ptr, ptr %6, align 8, !tbaa !9
  %63 = load i32, ptr @WAH_SIZE, align 4, !tbaa !5
  %64 = load i32, ptr %8, align 4, !tbaa !5
  call void @set_wah_i(ptr noundef %62, ptr noundef %7, i32 noundef %63, i32 noundef %64)
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = load i32, ptr %3, align 4, !tbaa !5
  %67 = mul i32 %65, %66
  %68 = load i32, ptr %2, align 4, !tbaa !5
  %69 = sub i32 %68, %67
  store i32 %69, ptr %2, align 4, !tbaa !5
  %70 = load i32, ptr %9, align 4, !tbaa !5
  %71 = load i32, ptr %4, align 4, !tbaa !5
  %72 = sub i32 %71, %70
  store i32 %72, ptr %4, align 4, !tbaa !5
  %73 = load i32, ptr %8, align 4, !tbaa !5
  %74 = add i32 %73, 1
  store i32 %74, ptr %8, align 4, !tbaa !5
  br label %42, !llvm.loop !11

75:                                               ; preds = %42
  %76 = load i32, ptr %2, align 4, !tbaa !5
  %77 = icmp ugt i32 %76, 0
  br i1 %77, label %78, label %86

78:                                               ; preds = %75
  %79 = load i32, ptr %3, align 4, !tbaa !5
  %80 = load i32, ptr %2, align 4, !tbaa !5
  %81 = sub i32 %79, %80
  %82 = shl i32 1, %81
  store i32 %82, ptr %7, align 4, !tbaa !5
  %83 = load ptr, ptr %6, align 8, !tbaa !9
  %84 = load i32, ptr @WAH_SIZE, align 4, !tbaa !5
  %85 = load i32, ptr %8, align 4, !tbaa !5
  call void @set_wah_i(ptr noundef %83, ptr noundef %7, i32 noundef %84, i32 noundef %85)
  br label %90

86:                                               ; preds = %75
  store i32 0, ptr %7, align 4, !tbaa !5
  %87 = load ptr, ptr %6, align 8, !tbaa !9
  %88 = load i32, ptr @WAH_SIZE, align 4, !tbaa !5
  %89 = load i32, ptr %8, align 4, !tbaa !5
  call void @set_wah_i(ptr noundef %87, ptr noundef %7, i32 noundef %88, i32 noundef %89)
  br label %90

90:                                               ; preds = %86, %78
  %91 = load ptr, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret ptr %91
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; 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 = { nounwind allocsize(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 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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

@WAH_SIZE = external dso_local local_unnamed_addr global i32, align 4
@WAH_MAX_FILL_WORDS = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @wah_init(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  %3 = load i32, ptr @WAH_SIZE, align 4, !tbaa !5
  %4 = add i32 %3, -1
  %5 = add i32 %0, -1
  %6 = add i32 %5, %4
  %7 = udiv i32 %6, %4
  %8 = icmp ugt i32 %7, 1
  br i1 %8, label %9, label %15

9:                                                ; preds = %1
  %10 = load i32, ptr @WAH_MAX_FILL_WORDS, align 4, !tbaa !5
  %11 = add i32 %7, -1
  %12 = add i32 %11, %10
  %13 = udiv i32 %12, %10
  %14 = add i32 %13, 1
  br label %15

15:                                               ; preds = %1, %9
  %16 = phi i32 [ %14, %9 ], [ 1, %1 ]
  %17 = lshr i32 %3, 3
  %18 = mul i32 %16, %17
  %19 = zext i32 %18 to i64
  %20 = add nuw nsw i64 %19, 4
  %21 = tail call noalias ptr @malloc(i64 noundef %20) #4
  store i32 %16, ptr %21, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #5
  %22 = icmp ult i32 %4, %0
  br i1 %22, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %15
  %23 = shl nuw i32 1, %4
  br label %24

24:                                               ; preds = %.lr.ph, %24
  %.035 = phi i32 [ %0, %.lr.ph ], [ %31, %24 ]
  %.02934 = phi i32 [ 0, %.lr.ph ], [ %33, %24 ]
  %.03033 = phi i32 [ %7, %.lr.ph ], [ %32, %24 ]
  %25 = add i32 %.03033, -1
  %26 = load i32, ptr @WAH_MAX_FILL_WORDS, align 4
  %27 = call i32 @llvm.umin.i32(i32 %25, i32 %26)
  %28 = add i32 %27, %23
  store i32 %28, ptr %2, align 4, !tbaa !5
  %29 = load i32, ptr @WAH_SIZE, align 4, !tbaa !5
  call void @set_wah_i(ptr noundef nonnull %21, ptr noundef nonnull %2, i32 noundef %29, i32 noundef %.02934)
  %30 = mul i32 %27, %4
  %31 = sub i32 %.035, %30
  %32 = sub i32 %.03033, %27
  %33 = add i32 %.02934, 1
  %34 = icmp ugt i32 %31, %4
  br i1 %34, label %24, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %24, %15
  %.029.lcssa = phi i32 [ 0, %15 ], [ %33, %24 ]
  %.0.lcssa = phi i32 [ %0, %15 ], [ %31, %24 ]
  %.not = icmp eq i32 %.0.lcssa, 0
  %35 = sub i32 %4, %.0.lcssa
  %36 = shl nuw i32 1, %35
  %storemerge = select i1 %.not, i32 0, i32 %36
  store i32 %storemerge, ptr %2, align 4, !tbaa !5
  %37 = load i32, ptr @WAH_SIZE, align 4, !tbaa !5
  call void @set_wah_i(ptr noundef nonnull %21, ptr noundef nonnull %2, i32 noundef %37, i32 noundef %.029.lcssa)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #5
  ret ptr %21
}

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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

; 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.umin.i32(i32, i32) #3

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind allocsize(0) }
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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass