; LLVM IR from ahr9n/psolving-paradigms :: 4.dynamic-programming/07.bitmasks/CodeForces 1242C - Sum Balance.ll (Z5solvei)

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

@can = external dso_local global [32768 x i8], align 16
@dp = external dso_local global [32768 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5solvei(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  store i32 1, ptr %2, align 4
  br label %54

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [32768 x i32], ptr @dp, i64 0, i64 %12
  store ptr %13, ptr %4, align 8, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !9
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp ne i32 %15, -1
  br i1 %16, label %17, label %20

17:                                               ; preds = %10
  %18 = load ptr, ptr %4, align 8, !tbaa !9
  %19 = load i32, ptr %18, align 4, !tbaa !5
  store i32 %19, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %53

20:                                               ; preds = %10
  %21 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 0, ptr %21, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %22 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %22, ptr %6, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %45, %20
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %27, label %26

26:                                               ; preds = %23
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %50

27:                                               ; preds = %23
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [32768 x i8], ptr @can, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11, !range !13, !noundef !14
  %32 = trunc i8 %31 to i1
  br i1 %32, label %33, label %39

33:                                               ; preds = %27
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = xor i32 %34, %35
  %37 = call noundef i32 @_Z5solvei(i32 noundef %36)
  %38 = icmp ne i32 %37, 0
  br label %39

39:                                               ; preds = %33, %27
  %40 = phi i1 [ false, %27 ], [ %38, %33 ]
  %41 = zext i1 %40 to i32
  %42 = load ptr, ptr %4, align 8, !tbaa !9
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = or i32 %43, %41
  store i32 %44, ptr %42, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %39
  %46 = load i32, ptr %6, align 4, !tbaa !5
  %47 = sub nsw i32 %46, 1
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = and i32 %47, %48
  store i32 %49, ptr %6, align 4, !tbaa !5
  br label %23, !llvm.loop !15

50:                                               ; preds = %26
  %51 = load ptr, ptr %4, align 8, !tbaa !9
  %52 = load i32, ptr %51, align 4, !tbaa !5
  store i32 %52, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %53

53:                                               ; preds = %50, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %54

54:                                               ; preds = %53, %9
  %55 = load i32, ptr %2, align 4
  ret i32 %55
}

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

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

!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 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@can = external dso_local local_unnamed_addr global [32768 x i8], align 16
@dp = external dso_local local_unnamed_addr global [32768 x i32], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5solvei(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq i32 %0, 0
  br i1 %2, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = sext i32 %0 to i64
  %5 = getelementptr inbounds [32768 x i32], ptr @dp, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not = icmp eq i32 %6, -1
  br i1 %.not, label %7, label %.loopexit

7:                                                ; preds = %3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %7, %18
  %9 = phi i32 [ 0, %7 ], [ %22, %18 ]
  %.018 = phi i32 [ %0, %7 ], [ %24, %18 ]
  %10 = sext i32 %.018 to i64
  %11 = getelementptr inbounds [32768 x i8], ptr @can, i64 0, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !9, !range !11, !noundef !12
  %13 = trunc nuw i8 %12 to i1
  br i1 %13, label %14, label %18

14:                                               ; preds = %8
  %15 = xor i32 %.018, %0
  %16 = tail call noundef i32 @_Z5solvei(i32 noundef %15)
  %17 = icmp ne i32 %16, 0
  %.pre = load i32, ptr %5, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %14, %8
  %19 = phi i32 [ %9, %8 ], [ %.pre, %14 ]
  %20 = phi i1 [ false, %8 ], [ %17, %14 ]
  %21 = zext i1 %20 to i32
  %22 = or i32 %19, %21
  store i32 %22, ptr %5, align 4, !tbaa !5
  %23 = add nsw i32 %.018, -1
  %24 = and i32 %23, %0
  %.not17 = icmp eq i32 %24, 0
  br i1 %.not17, label %.loopexit, label %8, !llvm.loop !13

.loopexit:                                        ; preds = %18, %3, %1
  %.1 = phi i32 [ 1, %1 ], [ %6, %3 ], [ %22, %18 ]
  ret i32 %.1
}

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

!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 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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