; LLVM IR from tangjz/acm-icpc :: usaco/Training/Chapter 2 Bigger challenges (lamps)

; 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
@c = external dso_local global i32, align 4
@i = external dso_local global i32, align 4
@j = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@on = external dso_local global [7 x i8], align 1
@off = external dso_local global [7 x i8], align 1
@light = external dso_local global [64 x i8], align 16
@succeed = external dso_local global i8, align 1
@ch = external dso_local global [10 x i8], align 1
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z8make_bitPcii(ptr noundef, i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3dfsiii(i32 noundef, i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %2 = load ptr, ptr @stdin, align 8, !tbaa !5
  %3 = call ptr @freopen(ptr noundef @.str, ptr noundef @.str.1, ptr noundef %2)
  %4 = load ptr, ptr @stdout, align 8, !tbaa !5
  %5 = call ptr @freopen(ptr noundef @.str.2, ptr noundef @.str.3, ptr noundef %4)
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef @n, ptr noundef @c, ptr noundef @k)
  br label %7

7:                                                ; preds = %10, %0
  %8 = load i32, ptr @k, align 4, !tbaa !9
  %9 = icmp ne i32 %8, -1
  br i1 %9, label %10, label %19

10:                                               ; preds = %7
  %11 = load i32, ptr @k, align 4, !tbaa !9
  %12 = sub nsw i32 %11, 1
  %13 = srem i32 %12, 6
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr @k, align 4, !tbaa !9
  %15 = load i32, ptr @k, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [7 x i8], ptr @on, i64 0, i64 %16
  store i8 1, ptr %17, align 1, !tbaa !11
  %18 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.5, ptr noundef @k)
  br label %7, !llvm.loop !13

19:                                               ; preds = %7
  %20 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.5, ptr noundef @k)
  br label %21

21:                                               ; preds = %24, %19
  %22 = load i32, ptr @k, align 4, !tbaa !9
  %23 = icmp ne i32 %22, -1
  br i1 %23, label %24, label %33

24:                                               ; preds = %21
  %25 = load i32, ptr @k, align 4, !tbaa !9
  %26 = sub nsw i32 %25, 1
  %27 = srem i32 %26, 6
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr @k, align 4, !tbaa !9
  %29 = load i32, ptr @k, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [7 x i8], ptr @off, i64 0, i64 %30
  store i8 1, ptr %31, align 1, !tbaa !11
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.5, ptr noundef @k)
  br label %21, !llvm.loop !15

33:                                               ; preds = %21
  call void @_Z3dfsiii(i32 noundef 0, i32 noundef 63, i32 noundef 0)
  %34 = load i8, ptr @succeed, align 1, !tbaa !11, !range !16, !noundef !17
  %35 = trunc i8 %34 to i1
  br i1 %35, label %38, label %36

36:                                               ; preds = %33
  %37 = call i32 @puts(ptr noundef @.str.6)
  br label %81

38:                                               ; preds = %33
  store i32 0, ptr @i, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %77, %38
  %40 = load i32, ptr @i, align 4, !tbaa !9
  %41 = icmp sle i32 %40, 63
  br i1 %41, label %42, label %80

42:                                               ; preds = %39
  %43 = load i32, ptr @i, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [64 x i8], ptr @light, i64 0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !11, !range !16, !noundef !17
  %47 = trunc i8 %46 to i1
  br i1 %47, label %48, label %76

48:                                               ; preds = %42
  %49 = load i32, ptr @i, align 4, !tbaa !9
  call void @_Z8make_bitPcii(ptr noundef @ch, i32 noundef %49, i32 noundef 6)
  %50 = load i32, ptr @n, align 4, !tbaa !9
  %51 = sdiv i32 %50, 6
  store i32 %51, ptr @j, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %57, %48
  %53 = load i32, ptr @j, align 4, !tbaa !9
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %60

55:                                               ; preds = %52
  %56 = call i32 (ptr, ...) @printf(ptr noundef @.str.7, ptr noundef @ch)
  br label %57

57:                                               ; preds = %55
  %58 = load i32, ptr @j, align 4, !tbaa !9
  %59 = add nsw i32 %58, -1
  store i32 %59, ptr @j, align 4, !tbaa !9
  br label %52, !llvm.loop !18

60:                                               ; preds = %52
  %61 = load i32, ptr @n, align 4, !tbaa !9
  %62 = srem i32 %61, 6
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %74

64:                                               ; preds = %60
  %65 = load i32, ptr @i, align 4, !tbaa !9
  %66 = load i32, ptr @n, align 4, !tbaa !9
  %67 = srem i32 %66, 6
  %68 = sub nsw i32 6, %67
  %69 = ashr i32 %65, %68
  store i32 %69, ptr @k, align 4, !tbaa !9
  %70 = load i32, ptr @k, align 4, !tbaa !9
  %71 = load i32, ptr @n, align 4, !tbaa !9
  %72 = srem i32 %71, 6
  call void @_Z8make_bitPcii(ptr noundef @ch, i32 noundef %70, i32 noundef %72)
  %73 = call i32 (ptr, ...) @printf(ptr noundef @.str.7, ptr noundef @ch)
  br label %74

74:                                               ; preds = %64, %60
  %75 = call i32 @putchar(i32 noundef 10)
  br label %76

76:                                               ; preds = %74, %42
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr @i, align 4, !tbaa !9
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr @i, align 4, !tbaa !9
  br label %39, !llvm.loop !19

80:                                               ; preds = %39
  br label %81

81:                                               ; preds = %80, %36
  %82 = load ptr, ptr @stdin, align 8, !tbaa !5
  %83 = call i32 @fclose(ptr noundef %82)
  %84 = load ptr, ptr @stdout, align 8, !tbaa !5
  %85 = call i32 @fclose(ptr noundef %84)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) #3

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

declare i32 @puts(ptr noundef) #3

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

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @putchar(i32 noundef) #4

declare i32 @fclose(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 = { 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 #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 = { inlinehint 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" }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = distinct !{!18, !14}
!19 = distinct !{!19, !14}


; 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
@c = external dso_local global i32, align 4
@i = external dso_local local_unnamed_addr global i32, align 4
@j = external dso_local local_unnamed_addr global i32, align 4
@k = external dso_local global i32, align 4
@on = external dso_local local_unnamed_addr global [7 x i8], align 1
@off = external dso_local local_unnamed_addr global [7 x i8], align 1
@light = external dso_local local_unnamed_addr global [64 x i8], align 16
@succeed = external dso_local local_unnamed_addr global i8, align 1
@ch = external dso_local global [10 x i8], align 1
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z8make_bitPcii(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3dfsiii(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = load ptr, ptr @stdin, align 8, !tbaa !5
  %2 = tail call ptr @freopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef %1)
  %3 = load ptr, ptr @stdout, align 8, !tbaa !5
  %4 = tail call ptr @freopen(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3, ptr noundef %3)
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull @n, ptr noundef nonnull @c, ptr noundef nonnull @k)
  %6 = load i32, ptr @k, align 4, !tbaa !9
  %.not8 = icmp eq i32 %6, -1
  br i1 %.not8, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %.lr.ph
  %7 = phi i32 [ %14, %.lr.ph ], [ %6, %0 ]
  %8 = add nsw i32 %7, -1
  %9 = srem i32 %8, 6
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @k, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [7 x i8], ptr @on, i64 0, i64 %11
  store i8 1, ptr %12, align 1, !tbaa !11
  %13 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.5, ptr noundef nonnull @k)
  %14 = load i32, ptr @k, align 4, !tbaa !9
  %.not = icmp eq i32 %14, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %0
  %15 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.5, ptr noundef nonnull @k)
  %16 = load i32, ptr @k, align 4, !tbaa !9
  %.not49 = icmp eq i32 %16, -1
  br i1 %.not49, label %._crit_edge12, label %.lr.ph11

.lr.ph11:                                         ; preds = %._crit_edge, %.lr.ph11
  %17 = phi i32 [ %24, %.lr.ph11 ], [ %16, %._crit_edge ]
  %18 = add nsw i32 %17, -1
  %19 = srem i32 %18, 6
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr @k, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [7 x i8], ptr @off, i64 0, i64 %21
  store i8 1, ptr %22, align 1, !tbaa !11
  %23 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.5, ptr noundef nonnull @k)
  %24 = load i32, ptr @k, align 4, !tbaa !9
  %.not4 = icmp eq i32 %24, -1
  br i1 %.not4, label %._crit_edge12, label %.lr.ph11, !llvm.loop !15

._crit_edge12:                                    ; preds = %.lr.ph11, %._crit_edge
  tail call void @_Z3dfsiii(i32 noundef 0, i32 noundef 63, i32 noundef 0)
  %25 = load i8, ptr @succeed, align 1, !tbaa !11, !range !16, !noundef !17
  %26 = trunc nuw i8 %25 to i1
  br i1 %26, label %.preheader, label %27

.preheader:                                       ; preds = %._crit_edge12
  store i32 0, ptr @i, align 4, !tbaa !9
  br label %29

27:                                               ; preds = %._crit_edge12
  %28 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.6)
  br label %.loopexit

29:                                               ; preds = %.preheader, %49
  %storemerge17 = phi i32 [ 0, %.preheader ], [ %51, %49 ]
  %30 = sext i32 %storemerge17 to i64
  %31 = getelementptr inbounds [64 x i8], ptr @light, i64 0, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !11, !range !16, !noundef !17
  %33 = trunc nuw i8 %32 to i1
  br i1 %33, label %34, label %49

34:                                               ; preds = %29
  tail call void @_Z8make_bitPcii(ptr noundef nonnull @ch, i32 noundef %storemerge17, i32 noundef 6)
  %35 = load i32, ptr @n, align 4, !tbaa !9
  %36 = sdiv i32 %35, 6
  store i32 %36, ptr @j, align 4, !tbaa !9
  %.off = add i32 %35, 5
  %.not613 = icmp ult i32 %.off, 11
  br i1 %.not613, label %._crit_edge16, label %.lr.ph15

.lr.ph15:                                         ; preds = %34, %.lr.ph15
  %37 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7, ptr noundef nonnull @ch)
  %38 = load i32, ptr @j, align 4, !tbaa !9
  %39 = add nsw i32 %38, -1
  store i32 %39, ptr @j, align 4, !tbaa !9
  %.not6 = icmp eq i32 %39, 0
  br i1 %.not6, label %._crit_edge16.loopexit, label %.lr.ph15, !llvm.loop !18

._crit_edge16.loopexit:                           ; preds = %.lr.ph15
  %.pre = load i32, ptr @n, align 4, !tbaa !9
  br label %._crit_edge16

._crit_edge16:                                    ; preds = %._crit_edge16.loopexit, %34
  %40 = phi i32 [ %.pre, %._crit_edge16.loopexit ], [ %35, %34 ]
  %41 = srem i32 %40, 6
  %.not7 = icmp eq i32 %41, 0
  br i1 %.not7, label %47, label %42

42:                                               ; preds = %._crit_edge16
  %43 = load i32, ptr @i, align 4, !tbaa !9
  %44 = sub nsw i32 6, %41
  %45 = ashr i32 %43, %44
  store i32 %45, ptr @k, align 4, !tbaa !9
  tail call void @_Z8make_bitPcii(ptr noundef nonnull @ch, i32 noundef %45, i32 noundef %41)
  %46 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7, ptr noundef nonnull @ch)
  br label %47

47:                                               ; preds = %42, %._crit_edge16
  %48 = tail call i32 @putchar(i32 noundef 10)
  %.pre18 = load i32, ptr @i, align 4, !tbaa !9
  br label %49

49:                                               ; preds = %29, %47
  %50 = phi i32 [ %storemerge17, %29 ], [ %.pre18, %47 ]
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr @i, align 4, !tbaa !9
  %52 = icmp slt i32 %50, 63
  br i1 %52, label %29, label %.loopexit, !llvm.loop !19

.loopexit:                                        ; preds = %49, %27
  %53 = load ptr, ptr @stdin, align 8, !tbaa !5
  %54 = tail call i32 @fclose(ptr noundef %53)
  %55 = load ptr, ptr @stdout, align 8, !tbaa !5
  %56 = tail call i32 @fclose(ptr noundef %55)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

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

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

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

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @putchar(i32 noundef) local_unnamed_addr #5

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #4

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 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 = { 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 = { 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 #5 = { inlinehint mustprogress nofree 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = distinct !{!18, !14}
!19 = distinct !{!19, !14}


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