; LLVM IR from holzschu/ios_system :: text_cmds/rs (rs)

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

@flags = external dso_local global i64, align 8
@irows = external dso_local global i32, align 4
@skip = external dso_local global i32, align 4
@curline = external dso_local global ptr, align 8
@curlen = external dso_local global i32, align 4
@rs_getline.putlength = external hidden global i32, align 4
@rs_getline.endblock = external hidden global ptr, align 8
@ibuf = external dso_local global [2048 x i8], align 16
@.str.3 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [15 x i8], align 1
@stdin = external global ptr, align 8
@.str.5 = external hidden unnamed_addr constant [11 x i8], align 1

declare i32 @printf(ptr 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: nounwind uwtable
define dso_local i32 @rs_getline() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #7
  %4 = load i32, ptr @irows, align 4, !tbaa !5
  %5 = icmp ne i32 %4, 0
  br i1 %5, label %10, label %6

6:                                                ; preds = %0
  store ptr @ibuf, ptr @curline, align 8, !tbaa !9
  %7 = load i64, ptr @flags, align 8, !tbaa !11
  %8 = and i64 %7, 256
  %9 = trunc i64 %8 to i32
  store i32 %9, ptr @rs_getline.putlength, align 4, !tbaa !5
  br label %27

10:                                               ; preds = %0
  %11 = load i32, ptr @skip, align 4, !tbaa !5
  %12 = icmp sle i32 %11, 0
  br i1 %12, label %13, label %26

13:                                               ; preds = %10
  %14 = load i32, ptr @curlen, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  %16 = load ptr, ptr @curline, align 8, !tbaa !9
  %17 = sext i32 %15 to i64
  %18 = getelementptr inbounds i8, ptr %16, i64 %17
  store ptr %18, ptr @curline, align 8, !tbaa !9
  %19 = load i32, ptr @rs_getline.putlength, align 4, !tbaa !5
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %25

21:                                               ; preds = %13
  %22 = load i32, ptr @curlen, align 4, !tbaa !5
  %23 = load i32, ptr @irows, align 4, !tbaa !5
  %24 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %22, i32 noundef %23)
  store ptr @ibuf, ptr @curline, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %21, %13
  br label %26

26:                                               ; preds = %25, %10
  br label %27

27:                                               ; preds = %26, %6
  %28 = load i32, ptr @rs_getline.putlength, align 4, !tbaa !5
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %44, label %30

30:                                               ; preds = %27
  %31 = load ptr, ptr @rs_getline.endblock, align 8, !tbaa !9
  %32 = load ptr, ptr @curline, align 8, !tbaa !9
  %33 = ptrtoint ptr %31 to i64
  %34 = ptrtoint ptr %32 to i64
  %35 = sub i64 %33, %34
  %36 = icmp slt i64 %35, 8192
  br i1 %36, label %37, label %44

37:                                               ; preds = %30
  %38 = call noalias ptr @malloc(i64 noundef 2048) #8
  store ptr %38, ptr @curline, align 8, !tbaa !9
  %39 = icmp ne ptr %38, null
  br i1 %39, label %41, label %40

40:                                               ; preds = %37
  call void (i32, ptr, ...) @errx(i32 noundef 1, ptr noundef @.str.4) #9
  unreachable

41:                                               ; preds = %37
  %42 = load ptr, ptr @curline, align 8, !tbaa !9
  %43 = getelementptr inbounds i8, ptr %42, i64 2048
  store ptr %43, ptr @rs_getline.endblock, align 8, !tbaa !9
  br label %44

44:                                               ; preds = %41, %30, %27
  %45 = load ptr, ptr @curline, align 8, !tbaa !9
  store ptr %45, ptr %1, align 8, !tbaa !9
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %57, %44
  %47 = load i32, ptr %3, align 4, !tbaa !5
  %48 = icmp slt i32 %47, 8192
  br i1 %48, label %49, label %64

49:                                               ; preds = %46
  %50 = call i32 @getchar()
  store i32 %50, ptr %2, align 4, !tbaa !5
  %51 = icmp eq i32 %50, -1
  br i1 %51, label %55, label %52

52:                                               ; preds = %49
  %53 = load i32, ptr %2, align 4, !tbaa !5
  %54 = icmp eq i32 %53, 10
  br i1 %54, label %55, label %56

55:                                               ; preds = %52, %49
  br label %64

56:                                               ; preds = %52
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %2, align 4, !tbaa !5
  %59 = trunc i32 %58 to i8
  %60 = load ptr, ptr %1, align 8, !tbaa !9
  %61 = getelementptr inbounds i8, ptr %60, i32 1
  store ptr %61, ptr %1, align 8, !tbaa !9
  store i8 %59, ptr %60, align 1, !tbaa !13
  %62 = load i32, ptr %3, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %3, align 4, !tbaa !5
  br label %46, !llvm.loop !14

64:                                               ; preds = %55, %46
  %65 = load ptr, ptr @stdin, align 8, !tbaa !9
  %66 = call i32 @ferror(ptr noundef %65) #7
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %68, label %69

68:                                               ; preds = %64
  call void (i32, ptr, ...) @errx(i32 noundef 74, ptr noundef @.str.5) #9
  unreachable

69:                                               ; preds = %64
  %70 = load ptr, ptr %1, align 8, !tbaa !9
  store i8 0, ptr %70, align 1, !tbaa !13
  %71 = load i32, ptr %3, align 4, !tbaa !5
  %72 = sub nsw i32 %71, 1
  store i32 %72, ptr @curlen, align 4, !tbaa !5
  %73 = load i32, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #7
  ret i32 %73
}

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

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

; Function Attrs: noreturn
declare void @errx(i32 noundef, ptr noundef, ...) #4

; Function Attrs: inlinehint nounwind uwtable
declare i32 @getchar() #5

; Function Attrs: nounwind
declare i32 @ferror(ptr noundef) #6

attributes #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 = { 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 #3 = { 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 #4 = { noreturn "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 = { inlinehint 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 #6 = { 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 #7 = { nounwind }
attributes #8 = { nounwind allocsize(0) }
attributes #9 = { noreturn }

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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

@flags = external dso_local local_unnamed_addr global i64, align 8
@irows = external dso_local local_unnamed_addr global i32, align 4
@skip = external dso_local local_unnamed_addr global i32, align 4
@curline = external dso_local local_unnamed_addr global ptr, align 8
@curlen = external dso_local local_unnamed_addr global i32, align 4
@rs_getline.putlength = external hidden local_unnamed_addr global i32, align 4
@rs_getline.endblock = external hidden local_unnamed_addr global ptr, align 8
@ibuf = external dso_local global [2048 x i8], align 16
@.str.3 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [15 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8
@.str.5 = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @rs_getline() local_unnamed_addr #1 {
  %1 = load i32, ptr @irows, align 4, !tbaa !5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %2, label %6

2:                                                ; preds = %0
  store ptr @ibuf, ptr @curline, align 8, !tbaa !9
  %3 = load i64, ptr @flags, align 8, !tbaa !11
  %4 = trunc i64 %3 to i32
  %5 = and i32 %4, 256
  store i32 %5, ptr @rs_getline.putlength, align 4, !tbaa !5
  br label %17

6:                                                ; preds = %0
  %7 = load i32, ptr @skip, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 1
  %.pre19.pre.pre = load ptr, ptr @curline, align 8, !tbaa !9
  br i1 %8, label %9, label %thread-pre-split

9:                                                ; preds = %6
  %10 = load i32, ptr @curlen, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr i8, ptr %.pre19.pre.pre, i64 %11
  %13 = getelementptr i8, ptr %12, i64 1
  store ptr %13, ptr @curline, align 8, !tbaa !9
  %14 = load i32, ptr @rs_getline.putlength, align 4, !tbaa !5
  %.not12 = icmp eq i32 %14, 0
  br i1 %.not12, label %.thread, label %15

15:                                               ; preds = %9
  %16 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %10, i32 noundef %1)
  store ptr @ibuf, ptr @curline, align 8, !tbaa !9
  br label %thread-pre-split

thread-pre-split:                                 ; preds = %15, %6
  %.pre19.pre = phi ptr [ @ibuf, %15 ], [ %.pre19.pre.pre, %6 ]
  %.pr = load i32, ptr @rs_getline.putlength, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %thread-pre-split, %2
  %.pre19 = phi ptr [ %.pre19.pre, %thread-pre-split ], [ @ibuf, %2 ]
  %18 = phi i32 [ %.pr, %thread-pre-split ], [ %5, %2 ]
  %.not13 = icmp eq i32 %18, 0
  br i1 %.not13, label %.thread, label %.preheader

.thread:                                          ; preds = %17, %9
  %19 = phi ptr [ %13, %9 ], [ %.pre19, %17 ]
  %20 = load ptr, ptr @rs_getline.endblock, align 8, !tbaa !9
  %21 = ptrtoint ptr %20 to i64
  %22 = ptrtoint ptr %19 to i64
  %23 = sub i64 %21, %22
  %24 = icmp slt i64 %23, 8192
  br i1 %24, label %25, label %.preheader

25:                                               ; preds = %.thread
  %26 = tail call noalias dereferenceable_or_null(2048) ptr @malloc(i64 noundef 2048) #6
  store ptr %26, ptr @curline, align 8, !tbaa !9
  %.not14 = icmp eq ptr %26, null
  br i1 %.not14, label %27, label %28

27:                                               ; preds = %25
  tail call void (i32, ptr, ...) @errx(i32 noundef 1, ptr noundef nonnull @.str.4) #7
  unreachable

28:                                               ; preds = %25
  %29 = getelementptr inbounds i8, ptr %26, i64 2048
  store ptr %29, ptr @rs_getline.endblock, align 8, !tbaa !9
  br label %.preheader

.preheader:                                       ; preds = %28, %.thread, %17
  %.0917.ph = phi ptr [ %.pre19, %17 ], [ %19, %.thread ], [ %26, %28 ]
  br label %30

30:                                               ; preds = %.preheader, %32
  %.018 = phi i32 [ %35, %32 ], [ 1, %.preheader ]
  %.0917 = phi ptr [ %34, %32 ], [ %.0917.ph, %.preheader ]
  %31 = tail call i32 @getchar()
  switch i32 %31, label %32 [
    i32 -1, label %36
    i32 10, label %36
  ]

32:                                               ; preds = %30
  %33 = trunc i32 %31 to i8
  %34 = getelementptr inbounds i8, ptr %.0917, i64 1
  store i8 %33, ptr %.0917, align 1, !tbaa !13
  %35 = add nuw nsw i32 %.018, 1
  %exitcond.not = icmp eq i32 %35, 8192
  br i1 %exitcond.not, label %36, label %30, !llvm.loop !14

36:                                               ; preds = %30, %30, %32
  %.09.lcssa = phi ptr [ %.0917, %30 ], [ %.0917, %30 ], [ %34, %32 ]
  %.0.lcssa = phi i32 [ %.018, %30 ], [ %.018, %30 ], [ 8192, %32 ]
  %37 = load ptr, ptr @stdin, align 8, !tbaa !9
  %38 = tail call i32 @ferror(ptr noundef %37) #8
  %.not15 = icmp eq i32 %38, 0
  br i1 %.not15, label %40, label %39

39:                                               ; preds = %36
  tail call void (i32, ptr, ...) @errx(i32 noundef 74, ptr noundef nonnull @.str.5) #7
  unreachable

40:                                               ; preds = %36
  store i8 0, ptr %.09.lcssa, align 1, !tbaa !13
  %41 = add nsw i32 %.0.lcssa, -1
  store i32 %41, ptr @curlen, align 4, !tbaa !5
  ret i32 %31
}

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

; Function Attrs: noreturn
declare void @errx(i32 noundef, ptr noundef, ...) local_unnamed_addr #3

; Function Attrs: inlinehint nofree nounwind uwtable
declare noundef i32 @getchar() local_unnamed_addr #4

; Function Attrs: nofree nounwind memory(read)
declare noundef i32 @ferror(ptr nocapture noundef) local_unnamed_addr #5

attributes #0 = { 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 #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 = { 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 #3 = { noreturn "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 = { inlinehint 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" }
attributes #5 = { nofree nounwind memory(read) "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 #6 = { nounwind allocsize(0) }
attributes #7 = { noreturn nounwind }
attributes #8 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass