program main
    implicit none
    integer x, n
    integer, allocatable, dimension(:) :: p
    integer, dimension(102) :: q
    integer i, min, d, a

    read(*,*) x, n
    allocate(p(n))
    read(*,*) p

    do i = 1, 102
        q(i) = i-1
    end do

    do i = 1, n
        q(p(i)+1) = 1000
    end do

    a = 1
    min = 200
    do i = 1, 102
        d = abs(q(i)-x)
        if(d < min) then
            min = d
            a = q(i)
        else if (d == min) then
            if (q(i) < a) then
                a = q(i)
            end if
        end if
    end do


    write(*,*) a

end program

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

@_QFEp.0 = internal unnamed_addr global ptr null
@_QFEp.8 = internal unnamed_addr global i64 0
@_QFEq = internal unnamed_addr global [102 x i32] undef
@_QQcl.42b1d0151fd48d13f37dcbd28e129060 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_1199/source.f\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 {
vector.ph:
  %0 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.42b1d0151fd48d13f37dcbd28e129060, i32 8)
  %4 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %2, i32 4)
  %5 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %1, i32 4)
  %6 = call i32 @_FortranAioEndIoStatement(ptr %3)
  %7 = load i32, ptr %1, align 4, !tbaa !3
  %8 = call i32 @llvm.smax.i32(i32 %7, i32 0)
  %9 = zext i32 %8 to i64
  %10 = shl nuw nsw i64 %9, 2
  %11 = call ptr @malloc(i64 %10)
  store ptr %11, ptr @_QFEp.0, align 8, !tbaa !7
  store i64 %9, ptr @_QFEp.8, align 8, !tbaa !7
  %12 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.42b1d0151fd48d13f37dcbd28e129060, i32 10)
  %.unpack = load ptr, ptr @_QFEp.0, align 8, !tbaa !7
  %.unpack106.unpack.unpack108 = load i64, ptr @_QFEp.8, align 8, !tbaa !7
  store ptr %.unpack, ptr %0, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %0, 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 %0, 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 %0, 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 %0, 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 %0, 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 %0, 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 %0, 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 %0, i64 0, i32 7, i64 0, i64 1
  store i64 %.unpack106.unpack.unpack108, 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 %0, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %13 = call i1 @_FortranAioInputDescriptor(ptr %12, ptr nonnull %0)
  %14 = call i32 @_FortranAioEndIoStatement(ptr %12)
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %15 = or i64 %index, 1
  %16 = or i64 %index, 2
  %17 = or i64 %index, 3
  %18 = insertelement <4 x i64> poison, i64 %index, i64 0
  %19 = insertelement <4 x i64> %18, i64 %15, i64 1
  %20 = insertelement <4 x i64> %19, i64 %16, i64 2
  %21 = insertelement <4 x i64> %20, i64 %17, i64 3
  %22 = getelementptr [102 x i32], ptr @_QFEq, i64 0, i64 %index
  %23 = trunc <4 x i64> %21 to <4 x i32>
  store <4 x i32> %23, ptr %22, align 16, !tbaa !3
  %index.next = add nuw i64 %index, 4
  %24 = icmp eq i64 %index.next, 100
  br i1 %24, label %scalar.ph, label %vector.body, !llvm.loop !9

scalar.ph:                                        ; preds = %vector.body
  store i32 100, ptr getelementptr inbounds ([102 x i32], ptr @_QFEq, i64 0, i64 100), align 16, !tbaa !3
  store i32 101, ptr getelementptr inbounds ([102 x i32], ptr @_QFEq, i64 0, i64 101), align 4, !tbaa !3
  %25 = load i32, ptr %1, align 4, !tbaa !3
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %.lr.ph, label %.preheader

.lr.ph:                                           ; preds = %scalar.ph
  %27 = zext i32 %25 to i64
  %.unpack111 = load ptr, ptr @_QFEp.0, align 8, !tbaa !7
  %xtraiter = and i64 %27, 1
  %28 = icmp eq i32 %25, 1
  br i1 %28, label %.preheader.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %27, 4294967294
  br label %37

.preheader.loopexit.unr-lcssa.loopexit:           ; preds = %37
  %29 = add nuw nsw i64 %indvars.iv126, 1
  br label %.preheader.loopexit.unr-lcssa

.preheader.loopexit.unr-lcssa:                    ; preds = %.preheader.loopexit.unr-lcssa.loopexit, %.lr.ph
  %indvars.iv126.unr = phi i64 [ 0, %.lr.ph ], [ %29, %.preheader.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.preheader.loopexit.epilog-lcssa

.preheader.loopexit.epilog-lcssa:                 ; preds = %.preheader.loopexit.unr-lcssa
  %30 = getelementptr i32, ptr %.unpack111, i64 %indvars.iv126.unr
  %31 = load i32, ptr %30, align 4, !tbaa !3
  %32 = add i32 %31, 1
  %33 = sext i32 %32 to i64
  %34 = add nsw i64 %33, -1
  %35 = getelementptr [102 x i32], ptr @_QFEq, i64 0, i64 %34
  store i32 1000, ptr %35, align 4, !tbaa !3
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit.epilog-lcssa, %.preheader.loopexit.unr-lcssa, %scalar.ph
  %36 = load i32, ptr %2, align 4, !tbaa !3
  br label %51

37:                                               ; preds = %37, %.lr.ph.new
  %indvars.iv126 = phi i64 [ 1, %.lr.ph.new ], [ %indvars.iv.next127.1, %37 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %37 ]
  %38 = add nsw i64 %indvars.iv126, -1
  %39 = getelementptr i32, ptr %.unpack111, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !3
  %41 = add i32 %40, 1
  %42 = sext i32 %41 to i64
  %43 = add nsw i64 %42, -1
  %44 = getelementptr [102 x i32], ptr @_QFEq, i64 0, i64 %43
  store i32 1000, ptr %44, align 4, !tbaa !3
  %45 = getelementptr i32, ptr %.unpack111, i64 %indvars.iv126
  %46 = load i32, ptr %45, align 4, !tbaa !3
  %47 = add i32 %46, 1
  %48 = sext i32 %47 to i64
  %49 = add nsw i64 %48, -1
  %50 = getelementptr [102 x i32], ptr @_QFEq, i64 0, i64 %49
  store i32 1000, ptr %50, align 4, !tbaa !3
  %indvars.iv.next127.1 = add nuw nsw i64 %indvars.iv126, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.preheader.loopexit.unr-lcssa.loopexit, label %37

51:                                               ; preds = %.preheader, %51
  %indvars.iv130 = phi i64 [ 1, %.preheader ], [ %indvars.iv.next131, %51 ]
  %.0124 = phi i32 [ 200, %.preheader ], [ %.1, %51 ]
  %.083123 = phi i32 [ 1, %.preheader ], [ %.184, %51 ]
  %52 = add nsw i64 %indvars.iv130, -1
  %53 = getelementptr [102 x i32], ptr @_QFEq, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !3
  %55 = sub i32 %54, %36
  %56 = call i32 @llvm.abs.i32(i32 %55, i1 false)
  %57 = icmp slt i32 %56, %.0124
  %58 = icmp eq i32 %56, %.0124
  %59 = icmp slt i32 %54, %.083123
  %or.cond = select i1 %58, i1 %59, i1 false
  %60 = select i1 %57, i1 true, i1 %or.cond
  %.184 = select i1 %60, i32 %54, i32 %.083123
  %.1 = call i32 @llvm.smin.i32(i32 %56, i32 %.0124)
  %indvars.iv.next131 = add nuw nsw i64 %indvars.iv130, 1
  %exitcond133.not = icmp eq i64 %indvars.iv.next131, 103
  br i1 %exitcond133.not, label %61, label %51

61:                                               ; preds = %51
  %62 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.42b1d0151fd48d13f37dcbd28e129060, i32 35)
  %63 = call i1 @_FortranAioOutputInteger32(ptr %62, i32 %.184)
  %64 = call i32 @_FortranAioEndIoStatement(ptr %62)
  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 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 i32 @llvm.smax.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

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

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) }

!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, !11}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
