; LLVM IR from kazunetakahashi/atcoder :: 2018/1014_code-festival-2018-qualb/C.ll (Z5paintii)

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

%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

@N = external dso_local global i32, align 4
@_Z1SB5cxx11 = external dso_local global [1010 x %"class.std::__cxx11::basic_string"], align 16
@_ZL2dx = external hidden constant [4 x i32], align 16
@_ZL2dy = external hidden constant [4 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5paintii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [1010 x %"class.std::__cxx11::basic_string"], ptr @_Z1SB5cxx11, i64 0, i64 %10
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %11, i64 noundef %13)
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %16 = sext i8 %15 to i32
  %17 = icmp eq i32 %16, 46
  br i1 %17, label %18, label %77

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

19:                                               ; preds = %65, %18
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = icmp slt i32 %20, 4
  br i1 %21, label %23, label %22

22:                                               ; preds = %19
  store i32 2, ptr %6, align 4
  br label %68

23:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [4 x i32], ptr @_ZL2dx, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = add nsw i32 %24, %28
  store i32 %29, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [4 x i32], ptr @_ZL2dy, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = add nsw i32 %30, %34
  store i32 %35, ptr %8, align 4, !tbaa !5
  %36 = load i32, ptr %7, align 4, !tbaa !5
  %37 = icmp sle i32 0, %36
  br i1 %37, label %38, label %61

38:                                               ; preds = %23
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = load i32, ptr @N, align 4, !tbaa !5
  %41 = icmp slt i32 %39, %40
  br i1 %41, label %42, label %61

42:                                               ; preds = %38
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = icmp sle i32 0, %43
  br i1 %44, label %45, label %61

45:                                               ; preds = %42
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = load i32, ptr @N, align 4, !tbaa !5
  %48 = icmp slt i32 %46, %47
  br i1 %48, label %49, label %61

49:                                               ; preds = %45
  %50 = load i32, ptr %7, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [1010 x %"class.std::__cxx11::basic_string"], ptr @_Z1SB5cxx11, i64 0, i64 %51
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %52, i64 noundef %54)
  %56 = load i8, ptr %55, align 1, !tbaa !9
  %57 = sext i8 %56 to i32
  %58 = icmp eq i32 %57, 88
  br i1 %58, label %59, label %60

59:                                               ; preds = %49
  store i32 1, ptr %6, align 4
  br label %62

60:                                               ; preds = %49
  br label %61

61:                                               ; preds = %60, %45, %42, %38, %23
  store i32 0, ptr %6, align 4
  br label %62

62:                                               ; preds = %61, %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %63 = load i32, ptr %6, align 4
  switch i32 %63, label %68 [
    i32 0, label %64
  ]

64:                                               ; preds = %62
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %5, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %5, align 4, !tbaa !5
  br label %19, !llvm.loop !10

68:                                               ; preds = %62, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %69 = load i32, ptr %6, align 4
  switch i32 %69, label %78 [
    i32 2, label %70
    i32 1, label %77
  ]

70:                                               ; preds = %68
  %71 = load i32, ptr %3, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [1010 x %"class.std::__cxx11::basic_string"], ptr @_Z1SB5cxx11, i64 0, i64 %72
  %74 = load i32, ptr %4, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %73, i64 noundef %75)
  store i8 88, ptr %76, align 1, !tbaa !9
  br label %77

77:                                               ; preds = %70, %68, %2
  ret void

78:                                               ; preds = %68
  unreachable
}

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) #1 align 2

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

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

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 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"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"

%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

@N = external dso_local local_unnamed_addr global i32, align 4
@_Z1SB5cxx11 = external dso_local global [1010 x %"class.std::__cxx11::basic_string"], align 16
@_ZL2dx = external hidden local_unnamed_addr constant [4 x i32], align 16
@_ZL2dy = external hidden local_unnamed_addr constant [4 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5paintii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [1010 x %"class.std::__cxx11::basic_string"], ptr @_Z1SB5cxx11, i64 0, i64 %3
  %5 = sext i32 %1 to i64
  %6 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %4, i64 noundef %5)
  %7 = load i8, ptr %6, align 1, !tbaa !5
  %8 = icmp eq i8 %7, 46
  br i1 %8, label %.preheader.preheader, label %.loopexit

.preheader.preheader:                             ; preds = %2
  %9 = load i32, ptr @_ZL2dx, align 16, !tbaa !8
  %10 = add nsw i32 %9, %0
  %11 = load i32, ptr @_ZL2dy, align 16, !tbaa !8
  %12 = add nsw i32 %11, %1
  %13 = icmp sgt i32 %10, -1
  br i1 %13, label %14, label %.critedge

14:                                               ; preds = %.preheader.preheader
  %15 = load i32, ptr @N, align 4, !tbaa !8
  %16 = icmp slt i32 %10, %15
  %17 = icmp sgt i32 %12, -1
  %18 = icmp slt i32 %12, %15
  %19 = and i1 %17, %18
  %or.cond23 = select i1 %16, i1 %19, i1 false
  br i1 %or.cond23, label %20, label %.critedge

20:                                               ; preds = %14
  %21 = zext nneg i32 %10 to i64
  %22 = getelementptr inbounds [1010 x %"class.std::__cxx11::basic_string"], ptr @_Z1SB5cxx11, i64 0, i64 %21
  %23 = zext nneg i32 %12 to i64
  %24 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %22, i64 noundef %23)
  %25 = load i8, ptr %24, align 1, !tbaa !5
  %26 = icmp eq i8 %25, 88
  br i1 %26, label %.loopexit, label %.critedge

.critedge:                                        ; preds = %20, %14, %.preheader.preheader
  %27 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dx, i64 4), align 4, !tbaa !8
  %28 = add nsw i32 %27, %0
  %29 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dy, i64 4), align 4, !tbaa !8
  %30 = add nsw i32 %29, %1
  %31 = icmp sgt i32 %28, -1
  br i1 %31, label %32, label %.critedge.1

32:                                               ; preds = %.critedge
  %33 = load i32, ptr @N, align 4, !tbaa !8
  %34 = icmp slt i32 %28, %33
  %35 = icmp sgt i32 %30, -1
  %36 = icmp slt i32 %30, %33
  %37 = and i1 %35, %36
  %or.cond23.1 = select i1 %34, i1 %37, i1 false
  br i1 %or.cond23.1, label %38, label %.critedge.1

38:                                               ; preds = %32
  %39 = zext nneg i32 %28 to i64
  %40 = getelementptr inbounds [1010 x %"class.std::__cxx11::basic_string"], ptr @_Z1SB5cxx11, i64 0, i64 %39
  %41 = zext nneg i32 %30 to i64
  %42 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %40, i64 noundef %41)
  %43 = load i8, ptr %42, align 1, !tbaa !5
  %44 = icmp eq i8 %43, 88
  br i1 %44, label %.loopexit, label %.critedge.1

.critedge.1:                                      ; preds = %38, %32, %.critedge
  %45 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dx, i64 8), align 8, !tbaa !8
  %46 = add nsw i32 %45, %0
  %47 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dy, i64 8), align 8, !tbaa !8
  %48 = add nsw i32 %47, %1
  %49 = icmp sgt i32 %46, -1
  br i1 %49, label %50, label %.critedge.2

50:                                               ; preds = %.critedge.1
  %51 = load i32, ptr @N, align 4, !tbaa !8
  %52 = icmp slt i32 %46, %51
  %53 = icmp sgt i32 %48, -1
  %54 = icmp slt i32 %48, %51
  %55 = and i1 %53, %54
  %or.cond23.2 = select i1 %52, i1 %55, i1 false
  br i1 %or.cond23.2, label %56, label %.critedge.2

56:                                               ; preds = %50
  %57 = zext nneg i32 %46 to i64
  %58 = getelementptr inbounds [1010 x %"class.std::__cxx11::basic_string"], ptr @_Z1SB5cxx11, i64 0, i64 %57
  %59 = zext nneg i32 %48 to i64
  %60 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %58, i64 noundef %59)
  %61 = load i8, ptr %60, align 1, !tbaa !5
  %62 = icmp eq i8 %61, 88
  br i1 %62, label %.loopexit, label %.critedge.2

.critedge.2:                                      ; preds = %56, %50, %.critedge.1
  %63 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dx, i64 12), align 4, !tbaa !8
  %64 = add nsw i32 %63, %0
  %65 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dy, i64 12), align 4, !tbaa !8
  %66 = add nsw i32 %65, %1
  %67 = icmp sgt i32 %64, -1
  br i1 %67, label %68, label %.critedge.3

68:                                               ; preds = %.critedge.2
  %69 = load i32, ptr @N, align 4, !tbaa !8
  %70 = icmp slt i32 %64, %69
  %71 = icmp sgt i32 %66, -1
  %72 = icmp slt i32 %66, %69
  %73 = and i1 %71, %72
  %or.cond23.3 = select i1 %70, i1 %73, i1 false
  br i1 %or.cond23.3, label %74, label %.critedge.3

74:                                               ; preds = %68
  %75 = zext nneg i32 %64 to i64
  %76 = getelementptr inbounds [1010 x %"class.std::__cxx11::basic_string"], ptr @_Z1SB5cxx11, i64 0, i64 %75
  %77 = zext nneg i32 %66 to i64
  %78 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %76, i64 noundef %77)
  %79 = load i8, ptr %78, align 1, !tbaa !5
  %80 = icmp eq i8 %79, 88
  br i1 %80, label %.loopexit, label %.critedge.3

.critedge.3:                                      ; preds = %74, %68, %.critedge.2
  %81 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %4, i64 noundef %5)
  store i8 88, ptr %81, align 1, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %20, %38, %56, %74, %.critedge.3, %2
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) local_unnamed_addr #0 align 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass