; LLVM IR from kparc/ksimple :: a (rl)

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

@mx = external dso_local global i64, align 8
@l = external hidden global ptr, align 8
@t = external dso_local global ptr, align 8
@.str.8 = external hidden unnamed_addr constant [2 x i8], align 1
@__FUNCTION__.rl = external hidden unnamed_addr constant [3 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @err(i64 noundef, i64 noundef, i64 noundef) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i64 @rl(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr @l, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %11

10:                                               ; preds = %1
  br label %14

11:                                               ; preds = %1
  %12 = load i64, ptr @mx, align 8, !tbaa !9
  %13 = call noalias ptr @malloc(i64 noundef %12) #4
  br label %14

14:                                               ; preds = %11, %10
  %15 = phi ptr [ %8, %10 ], [ %13, %11 ]
  store ptr %15, ptr @l, align 8, !tbaa !5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %27, label %18

18:                                               ; preds = %14
  %19 = load ptr, ptr @l, align 8, !tbaa !5
  %20 = load ptr, ptr @l, align 8, !tbaa !5
  %21 = load i64, ptr @mx, align 8, !tbaa !9
  %22 = call i64 @read(i32 noundef 0, ptr noundef %20, i64 noundef %21)
  %23 = sub nsw i64 %22, 1
  %24 = getelementptr inbounds i8, ptr %19, i64 %23
  store i8 0, ptr %24, align 1, !tbaa !11
  store i8 0, ptr %4, align 1, !tbaa !11
  %25 = load i8, ptr %4, align 1, !tbaa !11
  %26 = sext i8 %25 to i64
  store i64 %26, ptr %2, align 8
  br label %68

27:                                               ; preds = %14
  %28 = load ptr, ptr @t, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %31

30:                                               ; preds = %27
  br label %34

31:                                               ; preds = %27
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = call noalias ptr @fopen(ptr noundef %32, ptr noundef @.str.8)
  br label %34

34:                                               ; preds = %31, %30
  %35 = phi ptr [ %28, %30 ], [ %33, %31 ]
  store ptr %35, ptr @t, align 8, !tbaa !5
  %36 = load ptr, ptr @t, align 8, !tbaa !5
  %37 = icmp ne ptr %36, null
  br i1 %37, label %42, label %38

38:                                               ; preds = %34
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = ptrtoint ptr %39 to i64
  %41 = call i64 @err(i64 noundef ptrtoint (ptr @__FUNCTION__.rl to i64), i64 noundef 129, i64 noundef %40)
  store i64 %41, ptr %2, align 8
  br label %68

42:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %43 = load ptr, ptr @t, align 8, !tbaa !5
  %44 = call i64 @getline(ptr noundef @l, ptr noundef @mx, ptr noundef %43)
  store i64 %44, ptr %6, align 8, !tbaa !9
  %45 = load i64, ptr %6, align 8, !tbaa !9
  %46 = load i64, ptr @mx, align 8, !tbaa !9
  %47 = icmp ult i64 %45, %46
  br i1 %47, label %48, label %62

48:                                               ; preds = %42
  %49 = load ptr, ptr @l, align 8, !tbaa !5
  %50 = load i64, ptr %6, align 8, !tbaa !9
  %51 = load ptr, ptr @l, align 8, !tbaa !5
  %52 = load i64, ptr %6, align 8, !tbaa !9
  %53 = sub i64 %52, 1
  %54 = getelementptr inbounds i8, ptr %51, i64 %53
  %55 = load i8, ptr %54, align 1, !tbaa !11
  %56 = sext i8 %55 to i32
  %57 = icmp eq i32 10, %56
  %58 = zext i1 %57 to i32
  %59 = sext i32 %58 to i64
  %60 = sub i64 %50, %59
  %61 = getelementptr inbounds i8, ptr %49, i64 %60
  store i8 0, ptr %61, align 1, !tbaa !11
  br label %63

62:                                               ; preds = %42
  br label %63

63:                                               ; preds = %62, %48
  %64 = phi i64 [ 0, %48 ], [ 128, %62 ]
  store i64 %64, ptr %6, align 8, !tbaa !9
  %65 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %65, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %66 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %66, ptr %5, align 8, !tbaa !9
  %67 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %67, ptr %2, align 8
  br label %68

68:                                               ; preds = %63, %38, %18
  %69 = load i64, ptr %2, align 8
  ret i64 %69
}

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #3

declare noalias ptr @fopen(ptr noundef, ptr noundef) #3

declare i64 @getline(ptr noundef, ptr noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nounwind allocsize(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 = { nounwind allocsize(0) }
attributes #5 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}


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

@mx = external dso_local global i64, align 8
@l = external hidden global ptr, align 8
@t = external dso_local local_unnamed_addr global ptr, align 8
@.str.8 = external hidden unnamed_addr constant [2 x i8], align 1
@__FUNCTION__.rl = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @err(i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local i64 @rl(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @l, align 8, !tbaa !5
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %3, label %6

3:                                                ; preds = %1
  %4 = load i64, ptr @mx, align 8, !tbaa !9
  %5 = tail call noalias ptr @malloc(i64 noundef %4) #5
  br label %6

6:                                                ; preds = %1, %3
  %7 = phi ptr [ %5, %3 ], [ %2, %1 ]
  store ptr %7, ptr @l, align 8, !tbaa !5
  %.not15 = icmp eq ptr %0, null
  br i1 %.not15, label %8, label %13

8:                                                ; preds = %6
  %9 = load i64, ptr @mx, align 8, !tbaa !9
  %10 = tail call i64 @read(i32 noundef 0, ptr noundef %7, i64 noundef %9) #6
  %11 = add nsw i64 %10, -1
  %12 = getelementptr inbounds i8, ptr %7, i64 %11
  store i8 0, ptr %12, align 1, !tbaa !11
  br label %31

13:                                               ; preds = %6
  %14 = load ptr, ptr @t, align 8, !tbaa !5
  %.not16 = icmp eq ptr %14, null
  br i1 %.not16, label %15, label %.thread

15:                                               ; preds = %13
  %16 = tail call noalias ptr @fopen(ptr noundef nonnull %0, ptr noundef nonnull @.str.8)
  store ptr %16, ptr @t, align 8, !tbaa !5
  %.not17 = icmp eq ptr %16, null
  br i1 %.not17, label %17, label %.thread

17:                                               ; preds = %15
  %18 = ptrtoint ptr %0 to i64
  %19 = tail call i64 @err(i64 noundef ptrtoint (ptr @__FUNCTION__.rl to i64), i64 noundef 129, i64 noundef %18)
  br label %31

.thread:                                          ; preds = %13, %15
  %20 = phi ptr [ %16, %15 ], [ %14, %13 ]
  %21 = tail call i64 @getline(ptr noundef nonnull @l, ptr noundef nonnull @mx, ptr noundef nonnull %20) #6
  %22 = load i64, ptr @mx, align 8, !tbaa !9
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %24, label %31

24:                                               ; preds = %.thread
  %25 = load ptr, ptr @l, align 8, !tbaa !5
  %26 = getelementptr i8, ptr %25, i64 %21
  %27 = getelementptr i8, ptr %26, i64 -1
  %28 = load i8, ptr %27, align 1, !tbaa !11
  %29 = icmp eq i8 %28, 10
  %.neg = sext i1 %29 to i64
  %30 = getelementptr i8, ptr %26, i64 %.neg
  store i8 0, ptr %30, align 1, !tbaa !11
  br label %31

31:                                               ; preds = %24, %.thread, %17, %8
  %.0 = phi i64 [ %19, %17 ], [ 0, %8 ], [ 0, %24 ], [ 128, %.thread ]
  ret i64 %.0
}

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #3

declare i64 @getline(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #4

attributes #0 = { 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nofree "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 = { 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 #4 = { "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 = { nounwind allocsize(0) }
attributes #6 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass