program main
      implicit none
      integer(16) :: n, k, cnt, roop, i, cnt2, waru
      integer, allocatable :: a(:)
      integer :: str, next, str2, str3
      logical, allocatable :: used(:)

      read *, n, k
      allocate( a(n) )
      read *, a

      allocate( used(n) )
      used = .false.
      cnt = 0
      str = 1
      do
        if ( used(str) ) exit
        used(str) = .true.
        next = a(str)
        str = next
        cnt = cnt + 1
      end do

      str2 = 1
      cnt2 = 1
      do
        next = a(str2)
        str2 = next
        if ( next == str ) exit
        cnt2 = cnt2 + 1
      end do

      if ( (cnt+cnt2) >= k ) then
        str3 = 1
        do i = 1, k
          next = a(str3)
          str3 = next
        end do
        print *, next
      else

        waru = cnt-cnt2
        if ( waru == 0 ) then
          waru = cnt
        end if
  
        roop = mod( k-cnt,waru )
  
        do i = 1, roop
          next = a(str)
          str = next
        end do
        print *, next
      end if

end program main


; ModuleID = 'FIRModule'
source_filename = "FIRModule"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@_QFEa.0 = internal unnamed_addr global ptr null
@_QFEa.8 = internal unnamed_addr global i64 0
@_QFEused.0 = internal unnamed_addr global ptr null
@_QFEused.7 = internal unnamed_addr global i64 1
@_QQcl.6e7d84ee2fa0ac12445f4f3df315ed2d = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_1452/source.f\00"
@_QQcl.16373402fa3425c6e46827601abb88fa = linkonce constant [76 x i8] c"array left hand side must be allocated when the right hand side is a scalar\00"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

define void @_QQmain() local_unnamed_addr {
  %1 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %2 = alloca i128, align 8
  %3 = alloca i128, align 8
  %4 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.6e7d84ee2fa0ac12445f4f3df315ed2d, i32 8)
  %5 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %3, i32 16)
  %6 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %2, i32 16)
  %7 = call i32 @_FortranAioEndIoStatement(ptr %4)
  %8 = load i128, ptr %3, align 8, !tbaa !3
  %9 = trunc i128 %8 to i64
  %10 = call i64 @llvm.smax.i64(i64 %9, i64 0)
  %11 = shl i64 %10, 2
  %12 = call ptr @malloc(i64 %11)
  store ptr %12, ptr @_QFEa.0, align 8, !tbaa !7
  store i64 %10, ptr @_QFEa.8, align 8, !tbaa !7
  %13 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.6e7d84ee2fa0ac12445f4f3df315ed2d, i32 10)
  %.unpack = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %.unpack433.unpack.unpack435 = load i64, ptr @_QFEa.8, align 8, !tbaa !7
  store ptr %.unpack, ptr %1, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 1
  store i64 4, ptr %.fca.1.gep, align 8, !tbaa !7
  %.fca.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 2
  store i32 20180515, ptr %.fca.2.gep, align 8, !tbaa !7
  %.fca.3.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 3
  store i8 1, ptr %.fca.3.gep, align 4, !tbaa !7
  %.fca.4.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 4
  store i8 9, ptr %.fca.4.gep, align 1, !tbaa !7
  %.fca.5.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 5
  store i8 0, ptr %.fca.5.gep, align 2, !tbaa !7
  %.fca.6.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 6
  store i8 0, ptr %.fca.6.gep, align 1, !tbaa !7
  %.fca.7.0.0.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 0
  store i64 1, ptr %.fca.7.0.0.gep, align 8, !tbaa !7
  %.fca.7.0.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 1
  store i64 %.unpack433.unpack.unpack435, ptr %.fca.7.0.1.gep, align 8, !tbaa !7
  %.fca.7.0.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %14 = call i1 @_FortranAioInputDescriptor(ptr %13, ptr nonnull %1)
  %15 = call i32 @_FortranAioEndIoStatement(ptr %13)
  %16 = load i128, ptr %3, align 8, !tbaa !3
  %17 = trunc i128 %16 to i64
  %18 = call i64 @llvm.smax.i64(i64 %17, i64 0)
  %19 = shl i64 %18, 2
  %20 = call ptr @malloc(i64 %19)
  store ptr %20, ptr @_QFEused.0, align 8, !tbaa !7
  store i64 1, ptr @_QFEused.7, align 8, !tbaa !7
  %.not = icmp eq ptr %20, null
  br i1 %.not, label %21, label %.preheader530

.preheader530:                                    ; preds = %0
  %.not543 = icmp slt i64 %17, 1
  br i1 %.not543, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader530
  call void @llvm.memset.p0.i64(ptr nonnull align 4 %20, i8 0, i64 %19, i1 false), !tbaa !3
  br label %.loopexit

21:                                               ; preds = %0
  %22 = call {} @_FortranAReportFatalUserError(ptr nonnull @_QQcl.16373402fa3425c6e46827601abb88fa, ptr nonnull @_QQcl.6e7d84ee2fa0ac12445f4f3df315ed2d, i32 13)
  %.unpack516.unpack.unpack = load i64, ptr @_QFEused.7, align 8, !tbaa !7
  %23 = icmp slt i64 %17, 1
  %24 = select i1 %23, i64 1, i64 %.unpack516.unpack.unpack
  store i64 %24, ptr @_QFEused.7, align 8, !tbaa !7
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.preheader, %.preheader530, %21
  %.unpack454.unpack.unpack = phi i64 [ 1, %.lr.ph.preheader ], [ 1, %.preheader530 ], [ %24, %21 ]
  %25 = sub i64 1, %.unpack454.unpack.unpack
  %26 = getelementptr i32, ptr %20, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !3
  %.not459531 = icmp eq i32 %27, 0
  %.unpack460 = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  br i1 %.not459531, label %.lr.ph533, label %.preheader

.preheader:                                       ; preds = %.lr.ph533, %.loopexit
  %.0407.lcssa = phi i128 [ 0, %.loopexit ], [ %33, %.lr.ph533 ]
  %.0406.lcssa = phi i32 [ 1, %.loopexit ], [ %32, %.lr.ph533 ]
  br label %38

.lr.ph533:                                        ; preds = %.loopexit, %.lr.ph533
  %28 = phi ptr [ %36, %.lr.ph533 ], [ %26, %.loopexit ]
  %29 = phi i64 [ %34, %.lr.ph533 ], [ 1, %.loopexit ]
  %.0407532 = phi i128 [ %33, %.lr.ph533 ], [ 0, %.loopexit ]
  store i32 1, ptr %28, align 4, !tbaa !3
  %30 = add nsw i64 %29, -1
  %31 = getelementptr i32, ptr %.unpack460, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !3
  %33 = add i128 %.0407532, 1
  %34 = sext i32 %32 to i64
  %35 = sub i64 %34, %.unpack454.unpack.unpack
  %36 = getelementptr i32, ptr %20, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !3
  %.not459 = icmp eq i32 %37, 0
  br i1 %.not459, label %.lr.ph533, label %.preheader

38:                                               ; preds = %38, %.preheader
  %.0410 = phi i128 [ %44, %38 ], [ 1, %.preheader ]
  %.0405 = phi i32 [ %42, %38 ], [ 1, %.preheader ]
  %39 = sext i32 %.0405 to i64
  %40 = add nsw i64 %39, -1
  %41 = getelementptr i32, ptr %.unpack460, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !3
  %43 = icmp eq i32 %42, %.0406.lcssa
  %44 = add i128 %.0410, 1
  br i1 %43, label %45, label %38

45:                                               ; preds = %38
  %46 = add i128 %.0410, %.0407.lcssa
  %47 = load i128, ptr %2, align 8, !tbaa !3
  %.not484 = icmp slt i128 %46, %47
  br i1 %.not484, label %74, label %48

48:                                               ; preds = %45
  %49 = trunc i128 %47 to i64
  %50 = icmp sgt i64 %49, 0
  br i1 %50, label %.lr.ph536.preheader, label %._crit_edge

.lr.ph536.preheader:                              ; preds = %48
  %xtraiter = and i64 %49, 3
  %51 = icmp ult i64 %49, 4
  br i1 %51, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph536.preheader.new

.lr.ph536.preheader.new:                          ; preds = %.lr.ph536.preheader
  %unroll_iter = and i64 %49, -4
  br label %.lr.ph536

.lr.ph536:                                        ; preds = %.lr.ph536, %.lr.ph536.preheader.new
  %.0404535 = phi i32 [ 1, %.lr.ph536.preheader.new ], [ %67, %.lr.ph536 ]
  %niter = phi i64 [ 0, %.lr.ph536.preheader.new ], [ %niter.next.3, %.lr.ph536 ]
  %52 = sext i32 %.0404535 to i64
  %53 = add nsw i64 %52, -1
  %54 = getelementptr i32, ptr %.unpack460, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !3
  %56 = sext i32 %55 to i64
  %57 = add nsw i64 %56, -1
  %58 = getelementptr i32, ptr %.unpack460, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !3
  %60 = sext i32 %59 to i64
  %61 = add nsw i64 %60, -1
  %62 = getelementptr i32, ptr %.unpack460, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !3
  %64 = sext i32 %63 to i64
  %65 = add nsw i64 %64, -1
  %66 = getelementptr i32, ptr %.unpack460, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !3
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3.not = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3.not, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph536

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph536, %.lr.ph536.preheader
  %.lcssa558.ph = phi i32 [ undef, %.lr.ph536.preheader ], [ %67, %.lr.ph536 ]
  %.0404535.unr = phi i32 [ 1, %.lr.ph536.preheader ], [ %67, %.lr.ph536 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph536.epil

.lr.ph536.epil:                                   ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph536.epil
  %.0404535.epil = phi i32 [ %71, %.lr.ph536.epil ], [ %.0404535.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph536.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %68 = sext i32 %.0404535.epil to i64
  %69 = add nsw i64 %68, -1
  %70 = getelementptr i32, ptr %.unpack460, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !3
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph536.epil, !llvm.loop !9

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph536.epil, %48
  %.0408.lcssa = phi i32 [ %.0406.lcssa, %48 ], [ %.lcssa558.ph, %._crit_edge.loopexit.unr-lcssa ], [ %71, %.lr.ph536.epil ]
  %72 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.6e7d84ee2fa0ac12445f4f3df315ed2d, i32 39)
  %73 = call i1 @_FortranAioOutputInteger32(ptr %72, i32 %.0408.lcssa)
  br label %110

74:                                               ; preds = %45
  %75 = icmp eq i128 %.0407.lcssa, %.0410
  %76 = select i1 %75, i128 0, i128 %.0410
  %spec.select = sub i128 %.0407.lcssa, %76
  %77 = sub i128 %47, %.0407.lcssa
  %78 = srem i128 %77, %spec.select
  %79 = trunc i128 %78 to i64
  %80 = icmp sgt i64 %79, 0
  br i1 %80, label %.lr.ph540.preheader, label %._crit_edge541

.lr.ph540.preheader:                              ; preds = %74
  %xtraiter562 = and i64 %79, 3
  %lcmp.mod563.not = icmp eq i64 %xtraiter562, 0
  br i1 %lcmp.mod563.not, label %.lr.ph540.prol.loopexit, label %.lr.ph540.prol

.lr.ph540.prol:                                   ; preds = %.lr.ph540.preheader, %.lr.ph540.prol
  %81 = phi i64 [ %86, %.lr.ph540.prol ], [ %79, %.lr.ph540.preheader ]
  %.1538.prol = phi i32 [ %85, %.lr.ph540.prol ], [ %.0406.lcssa, %.lr.ph540.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph540.prol ], [ 0, %.lr.ph540.preheader ]
  %82 = sext i32 %.1538.prol to i64
  %83 = add nsw i64 %82, -1
  %84 = getelementptr i32, ptr %.unpack460, i64 %83
  %85 = load i32, ptr %84, align 4, !tbaa !3
  %86 = add nsw i64 %81, -1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter562
  br i1 %prol.iter.cmp.not, label %.lr.ph540.prol.loopexit, label %.lr.ph540.prol, !llvm.loop !11

.lr.ph540.prol.loopexit:                          ; preds = %.lr.ph540.prol, %.lr.ph540.preheader
  %.unr564 = phi i64 [ %79, %.lr.ph540.preheader ], [ %86, %.lr.ph540.prol ]
  %.1538.unr = phi i32 [ %.0406.lcssa, %.lr.ph540.preheader ], [ %85, %.lr.ph540.prol ]
  %.lcssa.unr = phi i32 [ undef, %.lr.ph540.preheader ], [ %85, %.lr.ph540.prol ]
  %87 = icmp ult i64 %79, 4
  br i1 %87, label %._crit_edge541, label %.lr.ph540

.lr.ph540:                                        ; preds = %.lr.ph540.prol.loopexit, %.lr.ph540
  %88 = phi i64 [ %105, %.lr.ph540 ], [ %.unr564, %.lr.ph540.prol.loopexit ]
  %.1538 = phi i32 [ %104, %.lr.ph540 ], [ %.1538.unr, %.lr.ph540.prol.loopexit ]
  %89 = sext i32 %.1538 to i64
  %90 = add nsw i64 %89, -1
  %91 = getelementptr i32, ptr %.unpack460, i64 %90
  %92 = load i32, ptr %91, align 4, !tbaa !3
  %93 = sext i32 %92 to i64
  %94 = add nsw i64 %93, -1
  %95 = getelementptr i32, ptr %.unpack460, i64 %94
  %96 = load i32, ptr %95, align 4, !tbaa !3
  %97 = sext i32 %96 to i64
  %98 = add nsw i64 %97, -1
  %99 = getelementptr i32, ptr %.unpack460, i64 %98
  %100 = load i32, ptr %99, align 4, !tbaa !3
  %101 = sext i32 %100 to i64
  %102 = add nsw i64 %101, -1
  %103 = getelementptr i32, ptr %.unpack460, i64 %102
  %104 = load i32, ptr %103, align 4, !tbaa !3
  %105 = add nsw i64 %88, -4
  %106 = add i64 %88, -5
  %107 = icmp ult i64 %106, -2
  br i1 %107, label %.lr.ph540, label %._crit_edge541

._crit_edge541:                                   ; preds = %.lr.ph540.prol.loopexit, %.lr.ph540, %74
  %.1409.lcssa = phi i32 [ %.0406.lcssa, %74 ], [ %.lcssa.unr, %.lr.ph540.prol.loopexit ], [ %104, %.lr.ph540 ]
  %108 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.6e7d84ee2fa0ac12445f4f3df315ed2d, i32 53)
  %109 = call i1 @_FortranAioOutputInteger32(ptr %108, i32 %.1409.lcssa)
  br label %110

110:                                              ; preds = %._crit_edge, %._crit_edge541
  %.sink = phi ptr [ %72, %._crit_edge ], [ %108, %._crit_edge541 ]
  %111 = call i32 @_FortranAioEndIoStatement(ptr %.sink)
  ret void
}

declare ptr @_FortranAioBeginExternalListInput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioInputInteger(ptr, ptr, i32) local_unnamed_addr

declare i32 @_FortranAioEndIoStatement(ptr) local_unnamed_addr

declare i1 @_FortranAioInputDescriptor(ptr, ptr) local_unnamed_addr

declare {} @_FortranAReportFatalUserError(ptr, ptr, i32) local_unnamed_addr

declare ptr @_FortranAioBeginExternalListOutput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #1

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

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!4, !4, i64 0}
!4 = !{!"any data access", !5, i64 0}
!5 = !{!"any access", !6, i64 0}
!6 = !{!"Flang Type TBAA Root"}
!7 = !{!8, !8, i64 0}
!8 = !{!"descriptor member", !5, i64 0}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !10}
