; LLVM IR from humz2k/sugaryc :: ext/bdwgc/cord (cordxtra)

; 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.CORD_ec_struct = type { ptr, ptr, [129 x i8] }

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

declare ptr @CORD_cat(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @CORD_chars(i8 noundef signext, i64 noundef) #2

; 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 void @CORD_ec_flush_buf(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @CORD_from_file_eager(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca [1 x %struct.CORD_ec_struct], align 16
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 152, ptr %3) #3
  %7 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %8 = getelementptr inbounds %struct.CORD_ec_struct, ptr %7, i32 0, i32 0
  store ptr null, ptr %8, align 16, !tbaa !9
  %9 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %10 = getelementptr inbounds %struct.CORD_ec_struct, ptr %9, i32 0, i32 2
  %11 = getelementptr inbounds [129 x i8], ptr %10, i64 0, i64 0
  %12 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %13 = getelementptr inbounds %struct.CORD_ec_struct, ptr %12, i32 0, i32 1
  store ptr %11, ptr %13, align 8, !tbaa !11
  br label %14

14:                                               ; preds = %63, %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = call i32 @getc(ptr noundef %15)
  store i32 %16, ptr %4, align 4, !tbaa !12
  %17 = load i32, ptr %4, align 4, !tbaa !12
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %37

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 1, ptr %5, align 8, !tbaa !14
  %20 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  call void @CORD_ec_flush_buf(ptr noundef %20)
  br label %21

21:                                               ; preds = %25, %19
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = call i32 @getc(ptr noundef %22)
  store i32 %23, ptr %4, align 4, !tbaa !12
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %28

25:                                               ; preds = %21
  %26 = load i64, ptr %5, align 8, !tbaa !14
  %27 = add i64 %26, 1
  store i64 %27, ptr %5, align 8, !tbaa !14
  br label %21, !llvm.loop !16

28:                                               ; preds = %21
  %29 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %30 = getelementptr inbounds %struct.CORD_ec_struct, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 16, !tbaa !9
  %32 = load i64, ptr %5, align 8, !tbaa !14
  %33 = call ptr @CORD_chars(i8 noundef signext 0, i64 noundef %32)
  %34 = call ptr @CORD_cat(ptr noundef %31, ptr noundef %33)
  %35 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %36 = getelementptr inbounds %struct.CORD_ec_struct, ptr %35, i32 0, i32 0
  store ptr %34, ptr %36, align 16, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %37

37:                                               ; preds = %28, %14
  %38 = load i32, ptr %4, align 4, !tbaa !12
  %39 = icmp eq i32 %38, -1
  br i1 %39, label %40, label %41

40:                                               ; preds = %37
  store i32 2, ptr %6, align 4
  br label %61

41:                                               ; preds = %37
  %42 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %43 = getelementptr inbounds %struct.CORD_ec_struct, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !11
  %45 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %46 = getelementptr inbounds %struct.CORD_ec_struct, ptr %45, i32 0, i32 2
  %47 = getelementptr inbounds [129 x i8], ptr %46, i64 0, i64 0
  %48 = getelementptr inbounds i8, ptr %47, i64 128
  %49 = icmp eq ptr %44, %48
  br i1 %49, label %50, label %52

50:                                               ; preds = %41
  %51 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  call void @CORD_ec_flush_buf(ptr noundef %51)
  br label %53

52:                                               ; preds = %41
  br label %53

53:                                               ; preds = %52, %50
  %54 = phi i32 [ 0, %50 ], [ 0, %52 ]
  %55 = load i32, ptr %4, align 4, !tbaa !12
  %56 = trunc i32 %55 to i8
  %57 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %58 = getelementptr inbounds %struct.CORD_ec_struct, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !11
  %60 = getelementptr inbounds i8, ptr %59, i32 1
  store ptr %60, ptr %58, align 8, !tbaa !11
  store i8 %56, ptr %59, align 1, !tbaa !18
  store i32 0, ptr %6, align 4
  br label %61

61:                                               ; preds = %53, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %62 = load i32, ptr %6, align 4
  switch i32 %62, label %72 [
    i32 0, label %63
    i32 2, label %64
  ]

63:                                               ; preds = %61
  br label %14

64:                                               ; preds = %61
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = call i32 @fclose(ptr noundef %65)
  %67 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  call void @CORD_ec_flush_buf(ptr noundef %67)
  %68 = getelementptr inbounds [1 x %struct.CORD_ec_struct], ptr %3, i64 0, i64 0
  %69 = getelementptr inbounds %struct.CORD_ec_struct, ptr %68, i32 0, i32 0
  %70 = load ptr, ptr %69, align 16, !tbaa !9
  %71 = call ptr @CORD_balance(ptr noundef %70)
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 152, ptr %3) #3
  ret ptr %71

72:                                               ; preds = %61
  unreachable
}

declare i32 @getc(ptr noundef) #1

declare i32 @fclose(ptr noundef) #1

declare ptr @CORD_balance(ptr noundef) #1

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

%struct.CORD_ec_struct = type { ptr, ptr, [129 x i8] }

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

declare ptr @CORD_cat(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @CORD_chars(i8 noundef signext, i64 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) #0

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

; Function Attrs: nounwind uwtable
define dso_local ptr @CORD_from_file_eager(ptr nocapture noundef %0) local_unnamed_addr #2 {
  %2 = alloca [1 x %struct.CORD_ec_struct], align 16
  call void @llvm.lifetime.start.p0(i64 152, ptr nonnull %2) #4
  store ptr null, ptr %2, align 16, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 16
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %3, ptr %4, align 8, !tbaa !10
  %5 = getelementptr inbounds i8, ptr %2, i64 144
  br label %6

6:                                                ; preds = %24, %1
  %7 = call i32 @getc(ptr noundef %0)
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %18

9:                                                ; preds = %6
  call void @CORD_ec_flush_buf(ptr noundef nonnull %2)
  br label %10

10:                                               ; preds = %10, %9
  %.08 = phi i64 [ 1, %9 ], [ %13, %10 ]
  %11 = call i32 @getc(ptr noundef %0)
  %12 = icmp eq i32 %11, 0
  %13 = add i64 %.08, 1
  br i1 %12, label %10, label %14, !llvm.loop !11

14:                                               ; preds = %10
  %15 = load ptr, ptr %2, align 16, !tbaa !5
  %16 = call ptr @CORD_chars(i8 noundef signext 0, i64 noundef %.08)
  %17 = call ptr @CORD_cat(ptr noundef %15, ptr noundef %16) #4
  store ptr %17, ptr %2, align 16, !tbaa !5
  br label %18

18:                                               ; preds = %14, %6
  %.09 = phi i32 [ %11, %14 ], [ %7, %6 ]
  %19 = icmp eq i32 %.09, -1
  br i1 %19, label %28, label %20

20:                                               ; preds = %18
  %21 = load ptr, ptr %4, align 8, !tbaa !10
  %22 = icmp eq ptr %21, %5
  br i1 %22, label %23, label %24

23:                                               ; preds = %20
  call void @CORD_ec_flush_buf(ptr noundef nonnull %2)
  %.pre = load ptr, ptr %4, align 8, !tbaa !10
  br label %24

24:                                               ; preds = %23, %20
  %25 = phi ptr [ %.pre, %23 ], [ %21, %20 ]
  %26 = trunc i32 %.09 to i8
  %27 = getelementptr inbounds i8, ptr %25, i64 1
  store ptr %27, ptr %4, align 8, !tbaa !10
  store i8 %26, ptr %25, align 1, !tbaa !13
  br label %6

28:                                               ; preds = %18
  %29 = call i32 @fclose(ptr noundef %0)
  call void @CORD_ec_flush_buf(ptr noundef nonnull %2)
  %30 = load ptr, ptr %2, align 16, !tbaa !5
  %31 = call ptr @CORD_balance(ptr noundef %30) #4
  call void @llvm.lifetime.end.p0(i64 152, ptr nonnull %2) #4
  ret ptr %31
}

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

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

declare ptr @CORD_balance(ptr noundef) local_unnamed_addr #1

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

!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, !7, i64 0}
!6 = !{!"CORD_ec_struct", !7, i64 0, !7, i64 8, !8, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!8, !8, i64 0}


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