program kadai
implicit none
integer  ::  n
integer,allocatable  ::  a(:,:)
integer  ::  i,j,time

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

call qsort(a)
time=0
j=0
do i=1,n
time=time+a(1,i)
if (time>a(2,i)) then
    write(*,*) "No"
    stop
end if
end do
write(*,*) "Yes"

contains

!
! swap two arguments
!
subroutine swap(a, b)
implicit none
integer, intent(inout) :: a, b
integer :: c
c = a
a = b
b = c
end subroutine swap

!
! quick sort
!再帰的に定義
subroutine qsort(x)
implicit none
integer, intent(inout)  ::  x(:,:)
integer  ::  first,last
first=1
last=size(x(2,:))
!!前半と後半に区切りたいので、最初と最後の引数を入れたかsubroutineを呼ぶ
call q2sort(x,first,last)
return
end subroutine qsort



!!!クイックソートを実際に行うサブルーチン
recursive subroutine q2sort(x,first,last)
implicit none
integer, intent(inout)  ::  x(:,:)
integer, intent(inout) ::  first,last
integer  ::  fir, las   !!再帰時の頭と尾
integer  ::  i,j
integer  ::  n


!現在真ん中にある要素をpivotとして設定する。
n=x(2,(first+last)/2)

i=first
j=last


!!無限ループの中で、pivot以下を前に、pivot以上を後に動かす
do
    do while (x(2,i)<n)
        i=i+1
    end do
    do while(x(2,j)>n)
    j=j-1
    end do


    if (i>j .or. i==j) then
        exit
    end if

    call swap(x(2,i),x(2,j))
    call swap(x(1,i),x(1,j))
i=i+1
j=j-1

end do


!!pivot以下の組について再帰的に入れ替える
if (first<i-1) then
las=i-1
call q2sort(x,first,las)
end if

!!pivot以上の組について再帰的に入れ替える
if (last>j+1) then
fir=j+1
call q2sort(x,fir,last)
end if

end subroutine q2sort


end program kadai

; 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.11 = internal unnamed_addr global i64 0
@_QQcl.9b6fe68492255cce1e867504cb0c447f = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_3962/source.f\00"
@_QQcl.4E6F = linkonce constant [2 x i8] c"No"
@_QQcl.596573 = linkonce constant [3 x i8] c"Yes"
@_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 i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, align 8
  %4 = alloca { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, align 8
  %5 = alloca i32, align 4
  %6 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.9b6fe68492255cce1e867504cb0c447f, i32 7)
  %7 = call i1 @_FortranAioInputInteger(ptr %6, ptr nonnull %5, i32 4)
  %8 = call i32 @_FortranAioEndIoStatement(ptr %6)
  %9 = load i32, ptr %5, align 4, !tbaa !3
  %10 = call i32 @llvm.smax.i32(i32 %9, i32 0)
  %11 = zext i32 %10 to i64
  %12 = shl nuw nsw i64 %11, 3
  %13 = call ptr @malloc(i64 %12)
  store ptr %13, ptr @_QFEa.0, align 8, !tbaa !7
  store i64 %11, ptr @_QFEa.11, align 8, !tbaa !7
  %14 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.9b6fe68492255cce1e867504cb0c447f, i32 9)
  %.unpack = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %.unpack218.unpack219.unpack224 = load i64, ptr @_QFEa.11, align 8, !tbaa !7
  store ptr %.unpack, ptr %4, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %4, 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, [2 x [3 x i64]] }, ptr %4, 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, [2 x [3 x i64]] }, ptr %4, i64 0, i32 3
  store i8 2, ptr %.fca.3.gep, align 4, !tbaa !7
  %.fca.4.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %4, 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, [2 x [3 x i64]] }, ptr %4, 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, [2 x [3 x i64]] }, ptr %4, 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, [2 x [3 x i64]] }, ptr %4, 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, [2 x [3 x i64]] }, ptr %4, i64 0, i32 7, i64 0, i64 1
  store i64 2, ptr %.fca.7.0.1.gep, align 8, !tbaa !7
  %.fca.7.0.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %4, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %.fca.7.1.0.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %4, i64 0, i32 7, i64 1, i64 0
  store i64 1, ptr %.fca.7.1.0.gep, align 8, !tbaa !7
  %.fca.7.1.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %4, i64 0, i32 7, i64 1, i64 1
  store i64 %.unpack218.unpack219.unpack224, ptr %.fca.7.1.1.gep, align 8, !tbaa !7
  %.fca.7.1.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %4, i64 0, i32 7, i64 1, i64 2
  store i64 8, ptr %.fca.7.1.2.gep, align 8, !tbaa !7
  %15 = call i1 @_FortranAioInputDescriptor(ptr %14, ptr nonnull %4)
  %16 = call i32 @_FortranAioEndIoStatement(ptr %14)
  %.unpack227 = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %.unpack234.unpack235.unpack240 = load i64, ptr @_QFEa.11, align 8, !tbaa !7
  store ptr %.unpack227, ptr %3, align 8, !tbaa !7
  %.fca.1.gep98 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 1
  store i64 4, ptr %.fca.1.gep98, align 8, !tbaa !7
  %.fca.2.gep100 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 2
  store i32 20180515, ptr %.fca.2.gep100, align 8, !tbaa !7
  %.fca.3.gep102 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 3
  store i8 2, ptr %.fca.3.gep102, align 4, !tbaa !7
  %.fca.4.gep104 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 4
  store i8 9, ptr %.fca.4.gep104, align 1, !tbaa !7
  %.fca.5.gep106 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 5
  store i8 0, ptr %.fca.5.gep106, align 2, !tbaa !7
  %.fca.6.gep108 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 6
  store i8 0, ptr %.fca.6.gep108, align 1, !tbaa !7
  %.fca.7.0.0.gep110 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 7, i64 0, i64 0
  store i64 1, ptr %.fca.7.0.0.gep110, align 8, !tbaa !7
  %.fca.7.0.1.gep112 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 7, i64 0, i64 1
  store i64 2, ptr %.fca.7.0.1.gep112, align 8, !tbaa !7
  %.fca.7.0.2.gep114 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep114, align 8, !tbaa !7
  %.fca.7.1.0.gep116 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 7, i64 1, i64 0
  store i64 1, ptr %.fca.7.1.0.gep116, align 8, !tbaa !7
  %.fca.7.1.1.gep118 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 7, i64 1, i64 1
  store i64 %.unpack234.unpack235.unpack240, ptr %.fca.7.1.1.gep118, align 8, !tbaa !7
  %.fca.7.1.2.gep120 = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %3, i64 0, i32 7, i64 1, i64 2
  store i64 8, ptr %.fca.7.1.2.gep120, align 8, !tbaa !7
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2)
  store i32 1, ptr %1, align 4, !tbaa !3
  %17 = call i64 @llvm.smax.i64(i64 %.unpack234.unpack235.unpack240, i64 0)
  %18 = trunc i64 %17 to i32
  store i32 %18, ptr %2, align 4, !tbaa !3
  call void @_QFPq2sort(ptr nonnull %3, ptr nonnull %1, ptr nonnull %2)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2)
  %19 = load i32, ptr %5, align 4, !tbaa !3
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %36
  %.0261 = phi i32 [ %26, %36 ], [ 0, %0 ]
  %.0200259 = phi i32 [ %37, %36 ], [ 1, %0 ]
  %21 = sext i32 %.0200259 to i64
  %22 = shl nsw i64 %21, 1
  %23 = add nsw i64 %22, -2
  %24 = getelementptr i32, ptr %.unpack227, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !3
  %26 = add i32 %25, %.0261
  %27 = add nsw i64 %22, -1
  %28 = getelementptr i32, ptr %.unpack227, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !3
  %30 = icmp sgt i32 %26, %29
  br i1 %30, label %31, label %36

31:                                               ; preds = %.lr.ph
  %32 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.9b6fe68492255cce1e867504cb0c447f, i32 17)
  %33 = call i1 @_FortranAioOutputAscii(ptr %32, ptr nonnull @_QQcl.4E6F, i64 2)
  %34 = call i32 @_FortranAioEndIoStatement(ptr %32)
  %35 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable

36:                                               ; preds = %.lr.ph
  %37 = add nuw i32 %.0200259, 1
  %exitcond.not = icmp eq i32 %.0200259, %19
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %36, %0
  %38 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.9b6fe68492255cce1e867504cb0c447f, i32 21)
  %39 = call i1 @_FortranAioOutputAscii(ptr %38, ptr nonnull @_QQcl.596573, i64 3)
  %40 = call i32 @_FortranAioEndIoStatement(ptr %38)
  ret void
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
define void @_QFPswap(ptr nocapture %0, ptr nocapture %1) local_unnamed_addr #1 {
  %3 = load i32, ptr %0, align 4, !tbaa !3
  %4 = load i32, ptr %1, align 4, !tbaa !3
  store i32 %4, ptr %0, align 4, !tbaa !3
  store i32 %3, ptr %1, align 4, !tbaa !3
  ret void
}

; Function Attrs: nofree nosync nounwind memory(readwrite, inaccessiblemem: none)
define void @_QFPqsort(ptr %0) local_unnamed_addr #2 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 1, ptr %2, align 4, !tbaa !3
  %4 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %0, i64 0, i32 7, i64 1, i64 1
  %5 = load i64, ptr %4, align 8, !tbaa !7
  %6 = tail call i64 @llvm.smax.i64(i64 %5, i64 0)
  %7 = trunc i64 %6 to i32
  store i32 %7, ptr %3, align 4, !tbaa !3
  call void @_QFPq2sort(ptr %0, ptr nonnull %2, ptr nonnull %3)
  ret void
}

; Function Attrs: nofree nosync nounwind memory(readwrite, inaccessiblemem: none)
define void @_QFPq2sort(ptr %0, ptr nocapture readonly %1, ptr nocapture readonly %2) local_unnamed_addr #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = load i32, ptr %1, align 4, !tbaa !3
  %7 = load i32, ptr %2, align 4, !tbaa !3
  %8 = add i32 %7, %6
  %9 = sdiv i32 %8, 2
  %narrow = add nsw i32 %9, -1
  %10 = sext i32 %narrow to i64
  %11 = load ptr, ptr %0, align 8, !tbaa !7
  %12 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %0, i64 0, i32 7, i64 0, i64 2
  %13 = load i64, ptr %12, align 8, !tbaa !7
  %14 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [2 x [3 x i64]] }, ptr %0, i64 0, i32 7, i64 1, i64 2
  %15 = load i64, ptr %14, align 8, !tbaa !7
  %16 = mul i64 %15, %10
  %17 = add i64 %16, %13
  %18 = getelementptr i8, ptr %11, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !3
  br label %.outer

.outer:                                           ; preds = %51, %3
  %.018.ph = phi i32 [ %38, %51 ], [ %7, %3 ]
  %.0.ph = phi i32 [ %28, %51 ], [ %6, %3 ]
  br label %20

20:                                               ; preds = %20, %.outer
  %.0 = phi i32 [ %28, %20 ], [ %.0.ph, %.outer ]
  %21 = sext i32 %.0 to i64
  %22 = add nsw i64 %21, -1
  %23 = mul i64 %22, %15
  %24 = add i64 %23, %13
  %25 = getelementptr i8, ptr %11, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !3
  %27 = icmp slt i32 %26, %19
  %28 = add i32 %.0, 1
  br i1 %27, label %20, label %.preheader

.preheader:                                       ; preds = %20
  %29 = getelementptr i8, ptr %11, i64 %24
  br label %30

30:                                               ; preds = %30, %.preheader
  %.1 = phi i32 [ %38, %30 ], [ %.018.ph, %.preheader ]
  %31 = sext i32 %.1 to i64
  %32 = add nsw i64 %31, -1
  %33 = mul i64 %32, %15
  %34 = add i64 %33, %13
  %35 = getelementptr i8, ptr %11, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !3
  %37 = icmp sgt i32 %36, %19
  %38 = add i32 %.1, -1
  br i1 %37, label %30, label %39

39:                                               ; preds = %30
  %.not = icmp slt i32 %.0, %.1
  br i1 %.not, label %51, label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %1, align 4, !tbaa !3
  %42 = add i32 %.0, -1
  %43 = icmp slt i32 %41, %42
  br i1 %43, label %44, label %45

44:                                               ; preds = %40
  store i32 %42, ptr %5, align 4, !tbaa !3
  call void @_QFPq2sort(ptr nonnull %0, ptr nonnull %1, ptr nonnull %5)
  br label %45

45:                                               ; preds = %44, %40
  %46 = load i32, ptr %2, align 4, !tbaa !3
  %47 = add i32 %.1, 1
  %48 = icmp sgt i32 %46, %47
  br i1 %48, label %49, label %50

49:                                               ; preds = %45
  store i32 %47, ptr %4, align 4, !tbaa !3
  call void @_QFPq2sort(ptr nonnull %0, ptr nonnull %4, ptr nonnull %2)
  br label %50

50:                                               ; preds = %49, %45
  ret void

51:                                               ; preds = %39
  %52 = getelementptr i8, ptr %11, i64 %34
  store i32 %36, ptr %29, align 4, !tbaa !3
  store i32 %26, ptr %52, align 4, !tbaa !3
  %53 = getelementptr i8, ptr %11, i64 %23
  %54 = getelementptr i8, ptr %11, i64 %33
  %55 = load i32, ptr %53, align 4, !tbaa !3
  %56 = load i32, ptr %54, align 4, !tbaa !3
  store i32 %56, ptr %53, align 4, !tbaa !3
  store i32 %55, ptr %54, align 4, !tbaa !3
  br label %.outer
}

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 @_FortranAioOutputAscii(ptr, ptr, i64) local_unnamed_addr

declare {} @_FortranAStopStatement(i32, i1, i1) local_unnamed_addr

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #4

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

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