; LLVM IR from VastoLorde95/Competitive-Programming :: Codejam/2015/Round 1B/Problem B (B)

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

@t = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@c = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@res = external dso_local global i32, align 4
@tot = external dso_local global i32, align 4
@num = external dso_local global i32, align 4
@grid = external dso_local global [16 x [16 x i32]], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z5checki(i32 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 norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @t)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #6
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %60, %0
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = load i32, ptr @t, align 4, !tbaa !5
  %11 = icmp sle i32 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  store i32 2, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #6
  br label %63

13:                                               ; preds = %8
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @r, ptr noundef @c, ptr noundef @n)
  store i32 10000, ptr @res, align 4, !tbaa !5
  %15 = load i32, ptr @r, align 4, !tbaa !5
  %16 = load i32, ptr @c, align 4, !tbaa !5
  %17 = mul nsw i32 %15, %16
  store i32 %17, ptr @tot, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %53, %13
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = load i32, ptr @tot, align 4, !tbaa !5
  %21 = shl i32 1, %20
  %22 = icmp slt i32 %19, %21
  br i1 %22, label %24, label %23

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

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

25:                                               ; preds = %40, %24
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = load i32, ptr @tot, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  store i32 8, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  br label %43

30:                                               ; preds = %25
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = shl i32 1, %32
  %34 = and i32 %31, %33
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %36, label %39

36:                                               ; preds = %30
  %37 = load i32, ptr @num, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr @num, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %36, %30
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %5, align 4, !tbaa !5
  br label %25, !llvm.loop !9

43:                                               ; preds = %29
  %44 = load i32, ptr @num, align 4, !tbaa !5
  %45 = load i32, ptr @n, align 4, !tbaa !5
  %46 = icmp eq i32 %44, %45
  br i1 %46, label %47, label %52

47:                                               ; preds = %43
  call void @llvm.memset.p0.i64(ptr align 16 @grid, i8 0, i64 1024, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = call noundef i32 @_Z5checki(i32 noundef %48)
  store i32 %49, ptr %6, align 4, !tbaa !5
  %50 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @res, ptr noundef nonnull align 4 dereferenceable(4) %6)
  %51 = load i32, ptr %50, align 4, !tbaa !5
  store i32 %51, ptr @res, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  br label %52

52:                                               ; preds = %47, %43
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %4, align 4, !tbaa !5
  br label %18, !llvm.loop !11

56:                                               ; preds = %23
  %57 = load i32, ptr %2, align 4, !tbaa !5
  %58 = load i32, ptr @res, align 4, !tbaa !5
  %59 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %57, i32 noundef %58)
  br label %60

60:                                               ; preds = %56
  %61 = load i32, ptr %2, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %2, align 4, !tbaa !5
  br label %8, !llvm.loop !12

63:                                               ; preds = %12
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #5

declare i32 @printf(ptr noundef, ...) #3

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 = { mustprogress norecurse 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 = { "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 nounwind willreturn memory(argmem: write) }
attributes #5 = { inlinehint 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 #6 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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

@t = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@c = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@res = external dso_local global i32, align 4
@tot = external dso_local local_unnamed_addr global i32, align 4
@num = external dso_local local_unnamed_addr global i32, align 4
@grid = external dso_local local_unnamed_addr global [16 x [16 x i32]], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [14 x i8], align 1

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

; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = alloca i32, align 4
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @t)
  %3 = load i32, ptr @t, align 4, !tbaa !5
  %.not18 = icmp slt i32 %3, 1
  br i1 %.not18, label %._crit_edge22, label %.lr.ph21

._crit_edge22:                                    ; preds = %._crit_edge17, %0
  ret i32 0

.lr.ph21:                                         ; preds = %0, %._crit_edge17
  %.01119 = phi i32 [ %10, %._crit_edge17 ], [ 1, %0 ]
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @r, ptr noundef nonnull @c, ptr noundef nonnull @n)
  store i32 10000, ptr @res, align 4, !tbaa !5
  %5 = load i32, ptr @r, align 4, !tbaa !5
  %6 = load i32, ptr @c, align 4, !tbaa !5
  %7 = mul nsw i32 %6, %5
  store i32 %7, ptr @tot, align 4, !tbaa !5
  %.not23 = icmp eq i32 %7, 31
  br i1 %.not23, label %._crit_edge17, label %.lr.ph16

._crit_edge17:                                    ; preds = %42, %.lr.ph21
  %8 = phi i32 [ 10000, %.lr.ph21 ], [ %43, %42 ]
  %9 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %.01119, i32 noundef %8)
  %10 = add nuw nsw i32 %.01119, 1
  %11 = load i32, ptr @t, align 4, !tbaa !5
  %.not.not = icmp slt i32 %.01119, %11
  br i1 %.not.not, label %.lr.ph21, label %._crit_edge22, !llvm.loop !9

.lr.ph16:                                         ; preds = %.lr.ph21, %42
  %12 = phi i32 [ %43, %42 ], [ 10000, %.lr.ph21 ]
  %13 = phi i32 [ %44, %42 ], [ %7, %.lr.ph21 ]
  %.01014 = phi i32 [ %45, %42 ], [ 0, %.lr.ph21 ]
  store i32 0, ptr @num, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph16
  %xtraiter = and i32 %13, 1
  %15 = icmp eq i32 %13, 1
  br i1 %15, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %13, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %36, %.lr.ph.preheader
  %.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %31, %36 ]
  %.013.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %37, %36 ]
  %.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %31, %36 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %16 = shl nuw i32 1, %.013.unr
  %17 = and i32 %16, %.01014
  %.not12.epil = icmp eq i32 %17, 0
  br i1 %.not12.epil, label %._crit_edge, label %18

18:                                               ; preds = %.lr.ph.epil
  %19 = add nsw i32 %.unr, 1
  store i32 %19, ptr @num, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %18, %.lr.ph.epil, %.lr.ph16
  %20 = phi i32 [ 0, %.lr.ph16 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.unr, %.lr.ph.epil ], [ %19, %18 ]
  %21 = load i32, ptr @n, align 4, !tbaa !5
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %38, label %42

.lr.ph:                                           ; preds = %36, %.lr.ph.preheader.new
  %.013 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %37, %36 ]
  %23 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %31, %36 ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %36 ]
  %24 = shl nuw i32 1, %.013
  %25 = and i32 %24, %.01014
  %.not12 = icmp eq i32 %25, 0
  %26 = add nsw i32 %23, 1
  %27 = select i1 %.not12, i32 %23, i32 %26
  %28 = shl nuw i32 2, %.013
  %29 = and i32 %28, %.01014
  %.not12.1 = icmp eq i32 %29, 0
  %30 = add nsw i32 %27, 1
  %31 = select i1 %.not12.1, i32 %27, i32 %30
  %32 = xor i1 %.not12, true
  %33 = xor i1 %.not12.1, true
  %34 = or i1 %32, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %.lr.ph
  store i32 %31, ptr @num, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %.lr.ph, %35
  %37 = add nuw nsw i32 %.013, 2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

38:                                               ; preds = %._crit_edge
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1024) @grid, i8 0, i64 1024, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #6
  %39 = call noundef i32 @_Z5checki(i32 noundef %.01014)
  store i32 %39, ptr %1, align 4, !tbaa !5
  %40 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @res, ptr noundef nonnull align 4 dereferenceable(4) %1)
  %41 = load i32, ptr %40, align 4, !tbaa !5
  store i32 %41, ptr @res, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #6
  %.pre = load i32, ptr @tot, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %._crit_edge, %38
  %43 = phi i32 [ %12, %._crit_edge ], [ %41, %38 ]
  %44 = phi i32 [ %13, %._crit_edge ], [ %.pre, %38 ]
  %45 = add nuw nsw i32 %.01014, 1
  %46 = shl nuw i32 1, %44
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %.lr.ph16, label %._crit_edge17, !llvm.loop !12
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #5

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress norecurse 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 = { nofree 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { inlinehint 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 #6 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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