; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/Looksery 2015/Problem C (C)

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

@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@c1 = external dso_local global i32, align 4
@c2 = external dso_local global i32, align 4
@a = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [8 x i8], align 1

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

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

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

13:                                               ; preds = %8
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @a)
  %15 = load i32, ptr @a, align 4, !tbaa !5
  %16 = srem i32 %15, 2
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %13
  %19 = load i32, ptr @c2, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr @c2, align 4, !tbaa !5
  br label %24

21:                                               ; preds = %13
  %22 = load i32, ptr @c1, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr @c1, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %21, %18
  br label %25

25:                                               ; preds = %24
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %2, align 4, !tbaa !5
  br label %8, !llvm.loop !9

28:                                               ; preds = %12
  %29 = load i32, ptr @k, align 4, !tbaa !5
  %30 = load i32, ptr @n, align 4, !tbaa !5
  %31 = icmp eq i32 %29, %30
  br i1 %31, label %32, label %41

32:                                               ; preds = %28
  %33 = load i32, ptr @c2, align 4, !tbaa !5
  %34 = srem i32 %33, 2
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %38

36:                                               ; preds = %32
  %37 = call i32 @puts(ptr noundef @.str.2)
  br label %40

38:                                               ; preds = %32
  %39 = call i32 @puts(ptr noundef @.str.3)
  br label %40

40:                                               ; preds = %38, %36
  br label %103

41:                                               ; preds = %28
  %42 = load i32, ptr @n, align 4, !tbaa !5
  %43 = load i32, ptr @k, align 4, !tbaa !5
  %44 = sub nsw i32 %42, %43
  %45 = srem i32 %44, 2
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %68

47:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %48 = load i32, ptr @n, align 4, !tbaa !5
  %49 = load i32, ptr @k, align 4, !tbaa !5
  %50 = sub nsw i32 %48, %49
  %51 = sdiv i32 %50, 2
  store i32 %51, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %52 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %52, ptr %4, align 4, !tbaa !5
  %53 = load i32, ptr @k, align 4, !tbaa !5
  %54 = srem i32 %53, 2
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %58

56:                                               ; preds = %47
  %57 = call i32 @puts(ptr noundef @.str.2)
  br label %67

58:                                               ; preds = %47
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = load i32, ptr @c1, align 4, !tbaa !5
  %61 = icmp sge i32 %59, %60
  br i1 %61, label %62, label %64

62:                                               ; preds = %58
  %63 = call i32 @puts(ptr noundef @.str.3)
  br label %66

64:                                               ; preds = %58
  %65 = call i32 @puts(ptr noundef @.str.2)
  br label %66

66:                                               ; preds = %64, %62
  br label %67

67:                                               ; preds = %66, %56
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %102

68:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %69 = load i32, ptr @n, align 4, !tbaa !5
  %70 = load i32, ptr @k, align 4, !tbaa !5
  %71 = sub nsw i32 %69, %70
  %72 = sdiv i32 %71, 2
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %74 = load i32, ptr %5, align 4, !tbaa !5
  %75 = sub nsw i32 %74, 1
  store i32 %75, ptr %6, align 4, !tbaa !5
  %76 = load i32, ptr @k, align 4, !tbaa !5
  %77 = srem i32 %76, 2
  %78 = icmp eq i32 %77, 0
  br i1 %78, label %79, label %92

79:                                               ; preds = %68
  %80 = load i32, ptr %6, align 4, !tbaa !5
  %81 = load i32, ptr @c2, align 4, !tbaa !5
  %82 = icmp sge i32 %80, %81
  br i1 %82, label %87, label %83

83:                                               ; preds = %79
  %84 = load i32, ptr %6, align 4, !tbaa !5
  %85 = load i32, ptr @c1, align 4, !tbaa !5
  %86 = icmp sge i32 %84, %85
  br i1 %86, label %87, label %89

87:                                               ; preds = %83, %79
  %88 = call i32 @puts(ptr noundef @.str.2)
  br label %91

89:                                               ; preds = %83
  %90 = call i32 @puts(ptr noundef @.str.3)
  br label %91

91:                                               ; preds = %89, %87
  br label %101

92:                                               ; preds = %68
  %93 = load i32, ptr %6, align 4, !tbaa !5
  %94 = load i32, ptr @c2, align 4, !tbaa !5
  %95 = icmp sge i32 %93, %94
  br i1 %95, label %96, label %98

96:                                               ; preds = %92
  %97 = call i32 @puts(ptr noundef @.str.2)
  br label %100

98:                                               ; preds = %92
  %99 = call i32 @puts(ptr noundef @.str.3)
  br label %100

100:                                              ; preds = %98, %96
  br label %101

101:                                              ; preds = %100, %91
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %102

102:                                              ; preds = %101, %67
  br label %103

103:                                              ; preds = %102, %40
  ret i32 0
}

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

; 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

declare i32 @puts(ptr noundef) #1

attributes #0 = { 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 #1 = { "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 = distinct !{!9, !10}
!10 = !{!"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"

@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@c1 = external dso_local local_unnamed_addr global i32, align 4
@c2 = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [8 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @k)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %0
  %.lcssa = phi i32 [ %2, %0 ], [ %12, %.lr.ph ]
  %4 = load i32, ptr @k, align 4, !tbaa !5
  %5 = icmp eq i32 %4, %.lcssa
  br i1 %5, label %14, label %18

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.015 = phi i32 [ %11, %.lr.ph ], [ 0, %0 ]
  %6 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @a)
  %7 = load i32, ptr @a, align 4, !tbaa !5
  %8 = and i32 %7, 1
  %.not14 = icmp eq i32 %8, 0
  %c1.c2 = select i1 %.not14, ptr @c1, ptr @c2
  %9 = load i32, ptr %c1.c2, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr %c1.c2, align 4, !tbaa !5
  %11 = add nuw nsw i32 %.015, 1
  %12 = load i32, ptr @n, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %.lr.ph, label %._crit_edge, !llvm.loop !9

14:                                               ; preds = %._crit_edge
  %15 = load i32, ptr @c2, align 4, !tbaa !5
  %16 = and i32 %15, 1
  %17 = icmp eq i32 %16, 0
  %.str.2..str.3 = select i1 %17, ptr @.str.2, ptr @.str.3
  br label %36

18:                                               ; preds = %._crit_edge
  %19 = sub nsw i32 %.lcssa, %4
  %20 = and i32 %19, 1
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %28

22:                                               ; preds = %18
  %23 = and i32 %4, 1
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %36, label %25

25:                                               ; preds = %22
  %26 = ashr exact i32 %19, 1
  %27 = load i32, ptr @c1, align 4, !tbaa !5
  %.not13 = icmp slt i32 %26, %27
  %.str.2..str.320 = select i1 %.not13, ptr @.str.2, ptr @.str.3
  br label %36

28:                                               ; preds = %18
  %29 = sdiv i32 %19, 2
  %30 = and i32 %4, 1
  %31 = icmp eq i32 %30, 0
  %32 = load i32, ptr @c2, align 4, !tbaa !5
  %.not11 = icmp slt i32 %29, %32
  br i1 %31, label %33, label %35

33:                                               ; preds = %28
  %34 = load i32, ptr @c1, align 4
  %.not12 = icmp slt i32 %29, %34
  %or.cond = select i1 %.not11, i1 %.not12, i1 false
  %.str.3..str.2 = select i1 %or.cond, ptr @.str.3, ptr @.str.2
  br label %36

35:                                               ; preds = %28
  %.str.3..str.221 = select i1 %.not11, ptr @.str.3, ptr @.str.2
  br label %36

36:                                               ; preds = %35, %33, %25, %22, %14
  %.str.3.sink = phi ptr [ %.str.2..str.3, %14 ], [ @.str.2, %22 ], [ %.str.2..str.320, %25 ], [ %.str.3..str.2, %33 ], [ %.str.3..str.221, %35 ]
  %37 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) %.str.3.sink)
  ret i32 0
}

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

; Function Attrs: nofree nounwind
declare noundef i32 @puts(ptr nocapture noundef readonly) local_unnamed_addr #1

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

!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"}


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