; LLVM IR from kazunetakahashi/atcoder :: 201510_201609/1024_KUPC2015/H.ll (Z6junbani)

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

@MAX_SIZE = external dso_local global i32, align 4
@N = external dso_local global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z8countonex(i64 noundef) #0

; 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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z6junbani(i32 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %11 = load i64, ptr %4, align 8, !tbaa !9
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = shl i64 %11, %13
  %15 = sub nsw i64 %14, 1
  store i64 %15, ptr %5, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %44, %1
  %17 = load i64, ptr %5, align 8, !tbaa !9
  %18 = load i64, ptr %4, align 8, !tbaa !9
  %19 = load i32, ptr @MAX_SIZE, align 4, !tbaa !5
  %20 = zext i32 %19 to i64
  %21 = shl i64 %18, %20
  %22 = icmp slt i64 %17, %21
  br i1 %22, label %23, label %62

23:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %39, %23
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = load i32, ptr @MAX_SIZE, align 4, !tbaa !5
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 4, ptr %7, align 4
  br label %42

29:                                               ; preds = %24
  %30 = load i64, ptr @N, align 8, !tbaa !9
  %31 = load i64, ptr %5, align 8, !tbaa !9
  %32 = add nsw i64 %30, %31
  %33 = call noundef i32 @_Z8countonex(i64 noundef %32)
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = icmp eq i32 %33, %34
  br i1 %35, label %36, label %38

36:                                               ; preds = %29
  %37 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %37, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %42

38:                                               ; preds = %29
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %6, align 4, !tbaa !5
  br label %24, !llvm.loop !11

42:                                               ; preds = %36, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %43 = load i32, ptr %7, align 4
  switch i32 %43, label %63 [
    i32 4, label %44
  ]

44:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %45 = load i64, ptr %5, align 8, !tbaa !9
  %46 = load i64, ptr %5, align 8, !tbaa !9
  %47 = sub nsw i64 0, %46
  %48 = and i64 %45, %47
  store i64 %48, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %49 = load i64, ptr %5, align 8, !tbaa !9
  %50 = load i64, ptr %8, align 8, !tbaa !9
  %51 = add nsw i64 %49, %50
  store i64 %51, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %52 = load i64, ptr %5, align 8, !tbaa !9
  %53 = load i64, ptr %9, align 8, !tbaa !9
  %54 = xor i64 %53, -1
  %55 = and i64 %52, %54
  store i64 %55, ptr %10, align 8, !tbaa !9
  %56 = load i64, ptr %10, align 8, !tbaa !9
  %57 = load i64, ptr %8, align 8, !tbaa !9
  %58 = sdiv i64 %56, %57
  %59 = ashr i64 %58, 1
  %60 = load i64, ptr %9, align 8, !tbaa !9
  %61 = or i64 %59, %60
  store i64 %61, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %16, !llvm.loop !13

62:                                               ; preds = %16
  store i64 -1, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %63

63:                                               ; preds = %62, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %64 = load i64, ptr %2, align 8
  ret i64 %64
}

attributes #0 = { mustprogress 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 }

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


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

@MAX_SIZE = external dso_local local_unnamed_addr global i32, align 4
@N = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z8countonex(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z6junbani(i32 noundef %0) local_unnamed_addr #0 {
  %2 = zext nneg i32 %0 to i64
  %notmask = shl nsw i64 -1, %2
  %3 = xor i64 %notmask, -1
  %4 = load i32, ptr @MAX_SIZE, align 4, !tbaa !5
  %5 = zext nneg i32 %4 to i64
  %6 = shl nuw i64 1, %5
  %7 = icmp sgt i64 %6, %3
  %8 = icmp sgt i32 %4, 0
  %or.cond = and i1 %7, %8
  br i1 %or.cond, label %.preheader, label %.thread

.preheader:                                       ; preds = %1, %._crit_edge
  %9 = phi i32 [ %17, %._crit_edge ], [ %4, %1 ]
  %.02028 = phi i64 [ %25, %._crit_edge ], [ %3, %1 ]
  %.not26 = icmp sgt i32 %9, 0
  br i1 %.not26, label %.lr.ph, label %._crit_edge

10:                                               ; preds = %.lr.ph
  %11 = add nuw nsw i32 %.02127, 1
  %12 = load i32, ptr @MAX_SIZE, align 4, !tbaa !5
  %.not = icmp slt i32 %11, %12
  br i1 %.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9

.lr.ph:                                           ; preds = %.preheader, %10
  %.02127 = phi i32 [ %11, %10 ], [ 0, %.preheader ]
  %13 = load i64, ptr @N, align 8, !tbaa !11
  %14 = add nsw i64 %13, %.02028
  %15 = tail call noundef i32 @_Z8countonex(i64 noundef %14)
  %16 = icmp eq i32 %15, %0
  br i1 %16, label %.thread, label %10

._crit_edge:                                      ; preds = %10, %.preheader
  %17 = phi i32 [ %9, %.preheader ], [ %12, %10 ]
  %18 = sub nsw i64 0, %.02028
  %19 = and i64 %.02028, %18
  %20 = add nsw i64 %19, %.02028
  %21 = xor i64 %20, -1
  %22 = and i64 %.02028, %21
  %23 = sdiv i64 %22, %19
  %24 = ashr i64 %23, 1
  %25 = or i64 %24, %20
  %26 = zext nneg i32 %17 to i64
  %27 = shl nuw i64 1, %26
  %28 = icmp slt i64 %25, %27
  br i1 %28, label %.preheader, label %.thread, !llvm.loop !13

.thread:                                          ; preds = %._crit_edge, %.lr.ph, %1
  %.2 = phi i64 [ -1, %1 ], [ %.02028, %.lr.ph ], [ -1, %._crit_edge ]
  ret i64 %.2
}

attributes #0 = { mustprogress 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" }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !10, !14}
!14 = !{!"llvm.loop.unswitch.partial.disable"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass