; LLVM IR from yudi-matsuzake/libgenerics :: doc/examples (vector3)

; 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.vector_t = type { ptr, i64, i64, i64 }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [11 x i8], align 1
@stdin = external global ptr, align 8
@stdout = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [32 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.vector_t, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca [8192 x i8], align 16
  %6 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 32, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %7 = call i32 @vector_create(ptr noundef %2, i64 noundef 0, i64 noundef 1)
  store i32 %7, ptr %3, align 4, !tbaa !5
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %16

9:                                                ; preds = %0
  %10 = load ptr, ptr @stderr, align 8, !tbaa !9
  %11 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %10, ptr noundef @.str) #4
  %12 = load ptr, ptr @stderr, align 8, !tbaa !9
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = call ptr @gerror_to_str(i32 noundef %13)
  %15 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef @.str.1, ptr noundef %14) #4
  store i32 1, ptr %1, align 4
  store i32 1, ptr %4, align 4
  br label %41

16:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8192, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  br label %17

17:                                               ; preds = %22, %16
  %18 = getelementptr inbounds [8192 x i8], ptr %5, i64 0, i64 0
  %19 = load ptr, ptr @stdin, align 8, !tbaa !9
  %20 = call i64 @fread(ptr noundef %18, i64 noundef 1, i64 noundef 8192, ptr noundef %19)
  store i64 %20, ptr %6, align 8, !tbaa !11
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %26

22:                                               ; preds = %17
  %23 = getelementptr inbounds [8192 x i8], ptr %5, i64 0, i64 0
  %24 = load i64, ptr %6, align 8, !tbaa !11
  %25 = call i32 @vector_append_buffer(ptr noundef %2, ptr noundef %23, i64 noundef %24)
  br label %17, !llvm.loop !13

26:                                               ; preds = %17
  %27 = call i32 @vector_append(ptr noundef %2, ptr noundef %2)
  %28 = load ptr, ptr @stdout, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.vector_t, ptr %2, i32 0, i32 1
  %30 = load i64, ptr %29, align 8, !tbaa !15
  %31 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %28, ptr noundef @.str.2, i64 noundef %30) #4
  %32 = getelementptr inbounds %struct.vector_t, ptr %2, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  %34 = getelementptr inbounds %struct.vector_t, ptr %2, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !18
  %36 = getelementptr inbounds %struct.vector_t, ptr %2, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !15
  %38 = load ptr, ptr @stdout, align 8, !tbaa !9
  %39 = call i64 @fwrite(ptr noundef %33, i64 noundef %35, i64 noundef %37, ptr noundef %38)
  %40 = call i32 @vector_destroy(ptr noundef %2)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8192, ptr %5) #4
  br label %41

41:                                               ; preds = %26, %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %2) #4
  %42 = load i32, ptr %1, align 4
  ret i32 %42
}

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

declare i32 @vector_create(ptr noundef, i64 noundef, i64 noundef) #2

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

declare ptr @gerror_to_str(i32 noundef) #2

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

declare i32 @vector_append_buffer(ptr noundef, ptr noundef, i64 noundef) #2

declare i32 @vector_append(ptr noundef, ptr noundef) #2

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

declare i32 @vector_destroy(ptr noundef) #2

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 "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 }

!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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !12, i64 8}
!16 = !{!"vector_t", !10, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!17 = !{!16, !10, i64 0}
!18 = !{!16, !12, i64 24}


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

%struct.vector_t = type { ptr, i64, i64, i64 }

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [11 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8
@stdout = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [32 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @main() local_unnamed_addr #0 {
  %1 = alloca %struct.vector_t, align 8
  %2 = alloca [8192 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %1) #4
  %3 = call i32 @vector_create(ptr noundef nonnull %1, i64 noundef 0, i64 noundef 1) #4
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %10, label %4

4:                                                ; preds = %0
  %5 = load ptr, ptr @stderr, align 8, !tbaa !5
  %6 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str) #5
  %7 = load ptr, ptr @stderr, align 8, !tbaa !5
  %8 = call ptr @gerror_to_str(i32 noundef %3) #4
  %9 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %7, ptr noundef nonnull @.str.1, ptr noundef %8) #5
  br label %29

10:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8192, ptr nonnull %2) #4
  %11 = load ptr, ptr @stdin, align 8, !tbaa !5
  %12 = call i64 @fread(ptr noundef nonnull %2, i64 noundef 1, i64 noundef 8192, ptr noundef %11)
  %.not56 = icmp eq i64 %12, 0
  br i1 %.not56, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %10, %.lr.ph
  %13 = phi i64 [ %16, %.lr.ph ], [ %12, %10 ]
  %14 = call i32 @vector_append_buffer(ptr noundef nonnull %1, ptr noundef nonnull %2, i64 noundef %13) #4
  %15 = load ptr, ptr @stdin, align 8, !tbaa !5
  %16 = call i64 @fread(ptr noundef nonnull %2, i64 noundef 1, i64 noundef 8192, ptr noundef %15)
  %.not5 = icmp eq i64 %16, 0
  br i1 %.not5, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %10
  %17 = call i32 @vector_append(ptr noundef nonnull %1, ptr noundef nonnull %1) #4
  %18 = load ptr, ptr @stdout, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %1, i64 8
  %20 = load i64, ptr %19, align 8, !tbaa !11
  %21 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef nonnull @.str.2, i64 noundef %20) #4
  %22 = load ptr, ptr %1, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %1, i64 24
  %24 = load i64, ptr %23, align 8, !tbaa !15
  %25 = load i64, ptr %19, align 8, !tbaa !11
  %26 = load ptr, ptr @stdout, align 8, !tbaa !5
  %27 = call i64 @fwrite(ptr noundef %22, i64 noundef %24, i64 noundef %25, ptr noundef %26)
  %28 = call i32 @vector_destroy(ptr noundef nonnull %1) #4
  call void @llvm.lifetime.end.p0(i64 8192, ptr nonnull %2) #4
  br label %29

29:                                               ; preds = %._crit_edge, %4
  %.0 = phi i32 [ 1, %4 ], [ 0, %._crit_edge ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %1) #4
  ret i32 %.0
}

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

declare i32 @vector_create(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #2

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

declare ptr @gerror_to_str(i32 noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

declare i32 @vector_append_buffer(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare i32 @vector_append(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

declare i32 @vector_destroy(ptr noundef) local_unnamed_addr #2

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

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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { nounwind }
attributes #5 = { cold 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !13, i64 8}
!12 = !{!"vector_t", !6, i64 0, !13, i64 8, !13, i64 16, !13, i64 24}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 0}
!15 = !{!12, !13, i64 24}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass