; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: FilesHandler/Sources (TxtFileLoader)

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

%struct.TxtFileLoader = type { ptr, ptr }

@stderr = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @txtLoaderOpenFile(ptr noundef, ptr noundef) #3

declare ptr @stringInitialization(i32 noundef) #4

declare i32 @fgetc(ptr noundef) #4

declare void @stringAppendChar(ptr noundef, i8 noundef signext) #4

declare i32 @fclose(ptr noundef) #4

declare ptr @stringToCharArray(ptr noundef) #4

declare void @destroyString(ptr noundef) #4

; Function Attrs: nounwind uwtable
define dso_local ptr @txtLoaderReadLineC(ptr noundef %0, i32 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %16

13:                                               ; preds = %2
  %14 = load ptr, ptr @stderr, align 8, !tbaa !5
  %15 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %14, ptr noundef @.str.6, ptr noundef @.str.4, ptr noundef @.str.2) #5
  call void @exit(i32 noundef -83386157) #6
  unreachable

16:                                               ; preds = %2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  call void @txtLoaderOpenFile(ptr noundef %17, ptr noundef @.str.9)
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %39, %16
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = icmp ne i32 %19, %20
  br i1 %21, label %22, label %30

22:                                               ; preds = %18
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.TxtFileLoader, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = call i32 @fgetc(ptr noundef %25)
  %27 = trunc i32 %26 to i8
  store i8 %27, ptr %6, align 1, !tbaa !13
  %28 = sext i8 %27 to i32
  %29 = icmp ne i32 %28, -1
  br label %30

30:                                               ; preds = %22, %18
  %31 = phi i1 [ false, %18 ], [ %29, %22 ]
  br i1 %31, label %32, label %40

32:                                               ; preds = %30
  %33 = load i8, ptr %6, align 1, !tbaa !13
  %34 = sext i8 %33 to i32
  %35 = icmp eq i32 %34, 10
  br i1 %35, label %36, label %39

36:                                               ; preds = %32
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %7, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %36, %32
  br label %18, !llvm.loop !14

40:                                               ; preds = %30
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = icmp ne i32 %41, %42
  br i1 %43, label %44, label %53

44:                                               ; preds = %40
  %45 = load i8, ptr %6, align 1, !tbaa !13
  %46 = sext i8 %45 to i32
  %47 = icmp eq i32 %46, -1
  br i1 %47, label %48, label %53

48:                                               ; preds = %44
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.TxtFileLoader, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !11
  %52 = call i32 @fclose(ptr noundef %51)
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %81

53:                                               ; preds = %44, %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %54 = call ptr @stringInitialization(i32 noundef 5)
  store ptr %54, ptr %9, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %69, %53
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.TxtFileLoader, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !11
  %59 = call i32 @fgetc(ptr noundef %58)
  %60 = trunc i32 %59 to i8
  store i8 %60, ptr %6, align 1, !tbaa !13
  %61 = sext i8 %60 to i32
  %62 = icmp ne i32 %61, -1
  br i1 %62, label %63, label %67

63:                                               ; preds = %55
  %64 = load i8, ptr %6, align 1, !tbaa !13
  %65 = sext i8 %64 to i32
  %66 = icmp ne i32 %65, 10
  br label %67

67:                                               ; preds = %63, %55
  %68 = phi i1 [ false, %55 ], [ %66, %63 ]
  br i1 %68, label %69, label %72

69:                                               ; preds = %67
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = load i8, ptr %6, align 1, !tbaa !13
  call void @stringAppendChar(ptr noundef %70, i8 noundef signext %71)
  br label %55, !llvm.loop !16

72:                                               ; preds = %67
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %73 = load ptr, ptr %9, align 8, !tbaa !5
  %74 = call ptr @stringToCharArray(ptr noundef %73)
  store ptr %74, ptr %10, align 8, !tbaa !5
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  call void @destroyString(ptr noundef %75)
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.TxtFileLoader, ptr %76, i32 0, i32 1
  %78 = load ptr, ptr %77, align 8, !tbaa !11
  %79 = call i32 @fclose(ptr noundef %78)
  %80 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %80, ptr %3, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  br label %81

81:                                               ; preds = %72, %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #5
  %82 = load ptr, ptr %3, align 8
  ret ptr %82
}

attributes #0 = { 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 = { noreturn 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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 }
attributes #6 = { noreturn 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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"TxtFileLoader", !6, i64 0, !6, i64 8}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [2 x i8], align 1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @txtLoaderOpenFile(ptr noundef, ptr noundef) local_unnamed_addr #2

declare ptr @stringInitialization(i32 noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @fgetc(ptr nocapture noundef) local_unnamed_addr #0

declare void @stringAppendChar(ptr noundef, i8 noundef signext) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #0

declare ptr @stringToCharArray(ptr noundef) local_unnamed_addr #3

declare void @destroyString(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local ptr @txtLoaderReadLineC(ptr noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = load ptr, ptr @stderr, align 8, !tbaa !5
  %6 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str.6, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.2) #4
  tail call void @exit(i32 noundef -83386157) #5
  unreachable

7:                                                ; preds = %2
  tail call void @txtLoaderOpenFile(ptr noundef nonnull %0, ptr noundef nonnull @.str.9)
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %.critedge.thread, label %.lr.ph

.lr.ph:                                           ; preds = %7
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  br label %9

9:                                                ; preds = %.lr.ph, %14
  %.02235 = phi i32 [ 0, %.lr.ph ], [ %.123, %14 ]
  %10 = load ptr, ptr %8, align 8, !tbaa !9
  %11 = tail call i32 @fgetc(ptr noundef %10)
  %trunc = trunc i32 %11 to i8
  switch i8 %trunc, label %14 [
    i8 -1, label %.critedge
    i8 10, label %12
  ]

12:                                               ; preds = %9
  %13 = add nsw i32 %.02235, 1
  br label %14

14:                                               ; preds = %9, %12
  %.123 = phi i32 [ %13, %12 ], [ %.02235, %9 ]
  %.not39 = icmp eq i32 %.123, %1
  br i1 %.not39, label %.critedge.thread, label %9, !llvm.loop !11

.critedge:                                        ; preds = %9
  %sext26.mask = and i32 %11, 255
  %15 = icmp eq i32 %sext26.mask, 255
  br i1 %15, label %24, label %.critedge.thread

.critedge.thread:                                 ; preds = %14, %7, %.critedge
  %16 = tail call ptr @stringInitialization(i32 noundef 5) #6
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  br label %18

18:                                               ; preds = %21, %.critedge.thread
  %19 = load ptr, ptr %17, align 8, !tbaa !9
  %20 = tail call i32 @fgetc(ptr noundef %19)
  %trunc31 = trunc i32 %20 to i8
  switch i8 %trunc31, label %21 [
    i8 -1, label %22
    i8 10, label %22
  ]

21:                                               ; preds = %18
  tail call void @stringAppendChar(ptr noundef %16, i8 noundef signext %trunc31) #6
  br label %18, !llvm.loop !13

22:                                               ; preds = %18, %18
  %23 = tail call ptr @stringToCharArray(ptr noundef %16) #6
  tail call void @destroyString(ptr noundef %16) #6
  br label %24

24:                                               ; preds = %.critedge, %22
  %.sink41 = phi ptr [ %17, %22 ], [ %8, %.critedge ]
  %.0 = phi ptr [ %23, %22 ], [ null, %.critedge ]
  %25 = load ptr, ptr %.sink41, align 8, !tbaa !9
  %26 = tail call i32 @fclose(ptr noundef %25)
  ret ptr %.0
}

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 = { noreturn 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 #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 = { "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 = { cold nounwind }
attributes #5 = { noreturn nounwind }
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, !6, i64 8}
!10 = !{!"TxtFileLoader", !6, i64 0, !6, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass