; LLVM IR from freakout42/macrocalc :: arx/src (rdb_read)

; 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.DBF = type { [1024 x i8], [1024 x i8], ptr, ptr, i32, i32, i8, i8, i8, i8, i8, i8, i8, i32, i32, i8, ptr, ptr, %struct.DBFPROLOG }
%struct.DBFPROLOG = type { i8, i8, i8, i8, i32, i16, i16, i16, i8, i8, [12 x i8], i8, [3 x i8] }
%struct.FIELD_RECORD = type { [11 x i8], i8, i32, i8, i8, [14 x i8] }

@.str = external hidden unnamed_addr constant [27 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @rdb_read(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.DBF, ptr %9, i32 0, i32 2
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.DBF, ptr %12, i32 0, i32 17
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = call ptr @str_gets(ptr noundef %11, ptr noundef %14, i64 noundef 4000)
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %84

18:                                               ; preds = %1
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.DBF, ptr %19, i32 0, i32 17
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  store ptr %21, ptr %7, align 8, !tbaa !5
  store i8 1, ptr %4, align 1, !tbaa !15
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.DBF, ptr %22, i32 0, i32 16
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  store ptr %24, ptr %5, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %74, %18
  %26 = load i8, ptr %4, align 1, !tbaa !15
  %27 = zext i8 %26 to i32
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.DBF, ptr %28, i32 0, i32 15
  %30 = load i8, ptr %29, align 8, !tbaa !17
  %31 = zext i8 %30 to i32
  %32 = icmp sle i32 %27, %31
  br i1 %32, label %33, label %79

33:                                               ; preds = %25
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = icmp eq ptr %34, null
  br i1 %35, label %36, label %40

36:                                               ; preds = %33
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.DBF, ptr %37, i32 0, i32 4
  %39 = load i32, ptr %38, align 8, !tbaa !18
  call void (ptr, ...) @lib_mesg(ptr noundef @.str, i32 noundef %39)
  br label %79

40:                                               ; preds = %33
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %41, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.DBF, ptr %43, i32 0, i32 6
  %45 = load i8, ptr %44, align 8, !tbaa !19
  %46 = sext i8 %45 to i32
  %47 = call ptr @strchr(ptr noundef %42, i32 noundef %46) #5
  store ptr %47, ptr %7, align 8, !tbaa !5
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %52

49:                                               ; preds = %40
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds i8, ptr %50, i32 1
  store ptr %51, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %50, align 1, !tbaa !15
  br label %52

52:                                               ; preds = %49, %40
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.DBF, ptr %53, i32 0, i32 10
  %55 = load i8, ptr %54, align 4, !tbaa !20
  %56 = sext i8 %55 to i32
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %61

58:                                               ; preds = %52
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = call ptr @str_trim(ptr noundef %59)
  br label %63

61:                                               ; preds = %52
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  br label %63

63:                                               ; preds = %61, %58
  %64 = phi ptr [ %60, %58 ], [ %62, %61 ]
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.DBF, ptr %65, i32 0, i32 17
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  %68 = ptrtoint ptr %64 to i64
  %69 = ptrtoint ptr %67 to i64
  %70 = sub i64 %68, %69
  %71 = trunc i64 %70 to i32
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.FIELD_RECORD, ptr %72, i32 0, i32 2
  store i32 %71, ptr %73, align 4, !tbaa !21
  br label %74

74:                                               ; preds = %63
  %75 = load i8, ptr %4, align 1, !tbaa !15
  %76 = add i8 %75, 1
  store i8 %76, ptr %4, align 1, !tbaa !15
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.FIELD_RECORD, ptr %77, i32 1
  store ptr %78, ptr %5, align 8, !tbaa !5
  br label %25, !llvm.loop !23

79:                                               ; preds = %36, %25
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.DBF, ptr %80, i32 0, i32 4
  %82 = load i32, ptr %81, align 8, !tbaa !18
  %83 = add i32 %82, 1
  store i32 %83, ptr %81, align 8, !tbaa !18
  store i32 1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %84

84:                                               ; preds = %79, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #4
  %85 = load i32, ptr %2, align 4
  ret i32 %85
}

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

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

declare void @lib_mesg(ptr noundef, ...) #2

; Function Attrs: nounwind willreturn memory(read)
declare ptr @strchr(ptr noundef, i32 noundef) #3

declare ptr @str_trim(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 willreturn 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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }

!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 2048}
!10 = !{!"DBF", !7, i64 0, !7, i64 1024, !6, i64 2048, !6, i64 2056, !11, i64 2064, !11, i64 2068, !7, i64 2072, !7, i64 2073, !7, i64 2074, !7, i64 2075, !7, i64 2076, !7, i64 2077, !7, i64 2078, !11, i64 2080, !11, i64 2084, !7, i64 2088, !6, i64 2096, !6, i64 2104, !12, i64 2112}
!11 = !{!"int", !7, i64 0}
!12 = !{!"DBFPROLOG", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !11, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 28, !7, i64 29}
!13 = !{!"short", !7, i64 0}
!14 = !{!10, !6, i64 2104}
!15 = !{!7, !7, i64 0}
!16 = !{!10, !6, i64 2096}
!17 = !{!10, !7, i64 2088}
!18 = !{!10, !11, i64 2064}
!19 = !{!10, !7, i64 2072}
!20 = !{!10, !7, i64 2076}
!21 = !{!22, !11, i64 12}
!22 = !{!"FIELD_RECORD", !7, i64 0, !7, i64 11, !11, i64 12, !7, i64 16, !7, i64 17, !7, i64 18}
!23 = distinct !{!23, !24}
!24 = !{!"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"

@.str = external hidden unnamed_addr constant [27 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @rdb_read(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 2048
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 2104
  %5 = load ptr, ptr %4, align 8, !tbaa !13
  %6 = tail call ptr @str_gets(ptr noundef %3, ptr noundef %5, i64 noundef 4000) #3
  %7 = icmp eq ptr %6, null
  br i1 %7, label %45, label %8

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %0, i64 2088
  %10 = load i8, ptr %9, align 8, !tbaa !14
  %.not27 = icmp eq i8 %10, 0
  br i1 %.not27, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %8
  %11 = getelementptr inbounds i8, ptr %0, i64 2096
  %12 = load ptr, ptr %11, align 8, !tbaa !15
  %13 = load ptr, ptr %4, align 8, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %0, i64 2072
  %15 = getelementptr inbounds i8, ptr %0, i64 2076
  br label %16

16:                                               ; preds = %.lr.ph, %31
  %.030 = phi ptr [ %13, %.lr.ph ], [ %.1, %31 ]
  %.02129 = phi ptr [ %12, %.lr.ph ], [ %40, %31 ]
  %.02228 = phi i8 [ 1, %.lr.ph ], [ %39, %31 ]
  %17 = icmp eq ptr %.030, null
  br i1 %17, label %18, label %21

18:                                               ; preds = %16
  %19 = getelementptr inbounds i8, ptr %0, i64 2064
  %20 = load i32, ptr %19, align 8, !tbaa !16
  tail call void (ptr, ...) @lib_mesg(ptr noundef nonnull @.str, i32 noundef %20) #3
  br label %.loopexit

21:                                               ; preds = %16
  %22 = load i8, ptr %14, align 8, !tbaa !17
  %23 = sext i8 %22 to i32
  %24 = tail call ptr @strchr(ptr noundef nonnull dereferenceable(1) %.030, i32 noundef %23) #4
  %.not25 = icmp eq ptr %24, null
  br i1 %.not25, label %27, label %25

25:                                               ; preds = %21
  %26 = getelementptr inbounds i8, ptr %24, i64 1
  store i8 0, ptr %24, align 1, !tbaa !18
  br label %27

27:                                               ; preds = %25, %21
  %.1 = phi ptr [ %26, %25 ], [ null, %21 ]
  %28 = load i8, ptr %15, align 4, !tbaa !19
  %.not26 = icmp eq i8 %28, 0
  br i1 %.not26, label %31, label %29

29:                                               ; preds = %27
  %30 = tail call ptr @str_trim(ptr noundef nonnull %.030) #3
  br label %31

31:                                               ; preds = %27, %29
  %32 = phi ptr [ %30, %29 ], [ %.030, %27 ]
  %33 = load ptr, ptr %4, align 8, !tbaa !13
  %34 = ptrtoint ptr %32 to i64
  %35 = ptrtoint ptr %33 to i64
  %36 = sub i64 %34, %35
  %37 = trunc i64 %36 to i32
  %38 = getelementptr inbounds i8, ptr %.02129, i64 12
  store i32 %37, ptr %38, align 4, !tbaa !20
  %39 = add i8 %.02228, 1
  %40 = getelementptr inbounds i8, ptr %.02129, i64 32
  %41 = load i8, ptr %9, align 8, !tbaa !14
  %.not = icmp ugt i8 %39, %41
  br i1 %.not, label %.loopexit, label %16, !llvm.loop !22

.loopexit:                                        ; preds = %31, %8, %18
  %42 = getelementptr inbounds i8, ptr %0, i64 2064
  %43 = load i32, ptr %42, align 8, !tbaa !16
  %44 = add i32 %43, 1
  store i32 %44, ptr %42, align 8, !tbaa !16
  br label %45

45:                                               ; preds = %1, %.loopexit
  %.020 = phi i32 [ 1, %.loopexit ], [ -1, %1 ]
  ret i32 %.020
}

declare ptr @str_gets(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @lib_mesg(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare ptr @strchr(ptr noundef, i32 noundef) local_unnamed_addr #2

declare ptr @str_trim(ptr noundef) local_unnamed_addr #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 = { "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 memory(argmem: 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 #3 = { nounwind }
attributes #4 = { nounwind willreturn memory(read) }

!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, !9, i64 2048}
!6 = !{!"DBF", !7, i64 0, !7, i64 1024, !9, i64 2048, !9, i64 2056, !10, i64 2064, !10, i64 2068, !7, i64 2072, !7, i64 2073, !7, i64 2074, !7, i64 2075, !7, i64 2076, !7, i64 2077, !7, i64 2078, !10, i64 2080, !10, i64 2084, !7, i64 2088, !9, i64 2096, !9, i64 2104, !11, i64 2112}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!"DBFPROLOG", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !10, i64 4, !12, i64 8, !12, i64 10, !12, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 28, !7, i64 29}
!12 = !{!"short", !7, i64 0}
!13 = !{!6, !9, i64 2104}
!14 = !{!6, !7, i64 2088}
!15 = !{!6, !9, i64 2096}
!16 = !{!6, !10, i64 2064}
!17 = !{!6, !7, i64 2072}
!18 = !{!7, !7, i64 0}
!19 = !{!6, !7, i64 2076}
!20 = !{!21, !10, i64 12}
!21 = !{!"FIELD_RECORD", !7, i64 0, !7, i64 11, !10, i64 12, !7, i64 16, !7, i64 17, !7, i64 18}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


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