; LLVM IR from eyangch/competitive-programming :: CSES/Introductory_Problems (gray_code_fast)

; 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
@vis = external dso_local global [131072 x i8], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5printi(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 i8, align 1
  %3 = alloca i8, align 1
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #4
  %8 = call i32 @getchar_unlocked()
  %9 = trunc i32 %8 to i8
  store i8 %9, ptr %2, align 1, !tbaa !5
  %10 = call i32 @getchar_unlocked()
  %11 = trunc i32 %10 to i8
  store i8 %11, ptr %3, align 1, !tbaa !5
  %12 = load i8, ptr %3, align 1, !tbaa !5
  %13 = sext i8 %12 to i32
  %14 = icmp eq i32 %13, 10
  br i1 %14, label %15, label %19

15:                                               ; preds = %0
  %16 = load i8, ptr %2, align 1, !tbaa !5
  %17 = sext i8 %16 to i32
  %18 = sub nsw i32 %17, 48
  store i32 %18, ptr @N, align 4, !tbaa !8
  br label %28

19:                                               ; preds = %0
  %20 = load i8, ptr %2, align 1, !tbaa !5
  %21 = sext i8 %20 to i32
  %22 = sub nsw i32 %21, 48
  %23 = mul nsw i32 10, %22
  %24 = load i8, ptr %3, align 1, !tbaa !5
  %25 = sext i8 %24 to i32
  %26 = add nsw i32 %23, %25
  %27 = sub nsw i32 %26, 48
  store i32 %27, ptr @N, align 4, !tbaa !8
  br label %28

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

29:                                               ; preds = %65, %28
  %30 = load i32, ptr %5, align 4, !tbaa !8
  %31 = load i32, ptr @N, align 4, !tbaa !8
  %32 = shl i32 1, %31
  %33 = icmp slt i32 %30, %32
  br i1 %33, label %35, label %34

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

35:                                               ; preds = %29
  %36 = load i32, ptr %4, align 4, !tbaa !8
  call void @_Z5printi(i32 noundef %36)
  %37 = load i32, ptr %4, align 4, !tbaa !8
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [131072 x i8], ptr @vis, i64 0, i64 %38
  store i8 1, ptr %39, align 1, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !8
  br label %40

40:                                               ; preds = %60, %35
  %41 = load i32, ptr %7, align 4, !tbaa !8
  %42 = load i32, ptr @N, align 4, !tbaa !8
  %43 = icmp slt i32 %41, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %40
  store i32 5, ptr %6, align 4
  br label %63

45:                                               ; preds = %40
  %46 = load i32, ptr %4, align 4, !tbaa !8
  %47 = load i32, ptr %7, align 4, !tbaa !8
  %48 = shl i32 1, %47
  %49 = xor i32 %46, %48
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [131072 x i8], ptr @vis, i64 0, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !10, !range !12, !noundef !13
  %53 = trunc i8 %52 to i1
  br i1 %53, label %54, label %55

54:                                               ; preds = %45
  br label %60

55:                                               ; preds = %45
  %56 = load i32, ptr %7, align 4, !tbaa !8
  %57 = shl i32 1, %56
  %58 = load i32, ptr %4, align 4, !tbaa !8
  %59 = xor i32 %58, %57
  store i32 %59, ptr %4, align 4, !tbaa !8
  store i32 5, ptr %6, align 4
  br label %63

60:                                               ; preds = %54
  %61 = load i32, ptr %7, align 4, !tbaa !8
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %7, align 4, !tbaa !8
  br label %40, !llvm.loop !14

63:                                               ; preds = %55, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %64

64:                                               ; preds = %63
  br label %65

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

68:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #4
  %69 = load i32, ptr %1, align 4
  ret i32 %69
}

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @getchar_unlocked() #3

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


; 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 local_unnamed_addr global i32, align 4
@vis = external dso_local local_unnamed_addr global [131072 x i8], align 16

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = tail call i32 @getchar_unlocked()
  %2 = tail call i32 @getchar_unlocked()
  %sext = shl i32 %2, 24
  %3 = icmp eq i32 %sext, 167772160
  br i1 %3, label %4, label %6

4:                                                ; preds = %0
  %sext19 = shl i32 %1, 24
  %5 = ashr exact i32 %sext19, 24
  br label %12

6:                                                ; preds = %0
  %7 = ashr exact i32 %sext, 24
  %sext18 = shl i32 %1, 24
  %8 = ashr exact i32 %sext18, 24
  %9 = mul nsw i32 %8, 10
  %10 = add nsw i32 %9, -480
  %11 = add nsw i32 %10, %7
  br label %12

12:                                               ; preds = %6, %4
  %storemerge.in = phi i32 [ %11, %6 ], [ %5, %4 ]
  %storemerge = add nsw i32 %storemerge.in, -48
  store i32 %storemerge, ptr @N, align 4, !tbaa !5
  %.not = icmp eq i32 %storemerge, 31
  br i1 %.not, label %._crit_edge27, label %.lr.ph26

._crit_edge27:                                    ; preds = %._crit_edge, %12
  ret i32 0

.lr.ph26:                                         ; preds = %12, %._crit_edge
  %.01524 = phi i32 [ %25, %._crit_edge ], [ 0, %12 ]
  %.01623 = phi i32 [ %.1, %._crit_edge ], [ 0, %12 ]
  tail call void @_Z5printi(i32 noundef %.01623)
  %13 = sext i32 %.01623 to i64
  %14 = getelementptr inbounds [131072 x i8], ptr @vis, i64 0, i64 %13
  store i8 1, ptr %14, align 1, !tbaa !9
  %15 = load i32, ptr @N, align 4, !tbaa !5
  %16 = icmp sgt i32 %15, 0
  br i1 %16, label %.lr.ph, label %._crit_edge

17:                                               ; preds = %.lr.ph
  %18 = add nuw nsw i32 %.020, 1
  %exitcond.not = icmp eq i32 %18, %15
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

.lr.ph:                                           ; preds = %.lr.ph26, %17
  %.020 = phi i32 [ %18, %17 ], [ 0, %.lr.ph26 ]
  %19 = shl nuw i32 1, %.020
  %20 = xor i32 %19, %.01623
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [131072 x i8], ptr @vis, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !9, !range !13, !noundef !14
  %24 = trunc nuw i8 %23 to i1
  br i1 %24, label %17, label %._crit_edge

._crit_edge:                                      ; preds = %17, %.lr.ph, %.lr.ph26
  %.1 = phi i32 [ %.01623, %.lr.ph26 ], [ %20, %.lr.ph ], [ %.01623, %17 ]
  %25 = add nuw nsw i32 %.01524, 1
  %26 = shl nuw i32 1, %15
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %.lr.ph26, label %._crit_edge27, !llvm.loop !15
}

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @getchar_unlocked() local_unnamed_addr #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 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 #2 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !12}


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