; LLVM IR from chkn/logdb :: src (logdb_log)

; 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.logdb_trailer_t = type { i32 }
%struct.logdb_log_header_t = type { [4 x i8], i16 }

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

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

declare i32 @open(ptr noundef, i32 noundef, ...) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @logdb_log_read(i32 noundef, i32 noundef) #2

declare i32 @close(i32 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @logdb_log_new(i32 noundef, ptr 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
define dso_local ptr @logdb_log_create(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca %struct.logdb_trailer_t, align 4
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  store i64 6, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = call i64 @lseek(i32 noundef %13, i64 noundef 0, i32 noundef 2) #5
  store i64 %14, ptr %8, align 8, !tbaa !11
  %15 = load i64, ptr %8, align 8, !tbaa !11
  %16 = icmp eq i64 %15, -1
  br i1 %16, label %17, label %18

17:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %104

18:                                               ; preds = %2
  %19 = load i64, ptr %8, align 8, !tbaa !11
  %20 = icmp uge i64 %19, 16
  br i1 %20, label %21, label %52

21:                                               ; preds = %18
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = call i64 @lseek(i32 noundef %22, i64 noundef -4, i32 noundef 2) #5
  %24 = icmp eq i64 %23, -1
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %104

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = call i64 @logdb_io_read(i32 noundef %27, ptr noundef %10, i64 noundef 4)
  %29 = icmp sgt i64 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %49

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %32 = getelementptr inbounds %struct.logdb_trailer_t, ptr %10, i32 0, i32 0
  %33 = load i32, ptr %32, align 4, !tbaa !13
  %34 = zext i32 %33 to i64
  %35 = mul nsw i64 %34, -1
  store i64 %35, ptr %11, align 8, !tbaa !11
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = load i64, ptr %11, align 8, !tbaa !11
  %38 = call i64 @lseek(i32 noundef %36, i64 noundef %37, i32 noundef 2) #5
  %39 = icmp eq i64 %38, -1
  br i1 %39, label %40, label %41

40:                                               ; preds = %31
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %48

41:                                               ; preds = %31
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = call ptr @logdb_log_read(i32 noundef %42, i32 noundef 2)
  store ptr %43, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.logdb_trailer_t, ptr %10, i32 0, i32 0
  %45 = load i32, ptr %44, align 4, !tbaa !13
  %46 = zext i32 %45 to i64
  %47 = sub i64 %46, 4
  store i64 %47, ptr %7, align 8, !tbaa !11
  store i32 0, ptr %9, align 4
  br label %48

48:                                               ; preds = %41, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %49

49:                                               ; preds = %48, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %50 = load i32, ptr %9, align 4
  switch i32 %50, label %104 [
    i32 0, label %51
  ]

51:                                               ; preds = %49
  br label %52

52:                                               ; preds = %51, %18
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = icmp ne ptr %53, null
  br i1 %54, label %67, label %55

55:                                               ; preds = %52
  %56 = call noalias ptr @malloc(i64 noundef 6) #6
  store ptr %56, ptr %6, align 8, !tbaa !5
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = icmp ne ptr %57, null
  br i1 %58, label %60, label %59

59:                                               ; preds = %55
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %104

60:                                               ; preds = %55
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.logdb_log_header_t, ptr %61, i32 0, i32 0
  %63 = getelementptr inbounds [4 x i8], ptr %62, i64 0, i64 0
  %64 = call ptr @strncpy(ptr noundef %63, ptr noundef @.str, i64 noundef 4) #5
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.logdb_log_header_t, ptr %65, i32 0, i32 1
  store i16 1, ptr %66, align 2, !tbaa !15
  br label %67

67:                                               ; preds = %60, %52
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = call i32 (ptr, i32, ...) @open(ptr noundef %68, i32 noundef 1218, i32 noundef 384)
  store i32 %69, ptr %12, align 4, !tbaa !9
  %70 = load i32, ptr %12, align 4, !tbaa !9
  %71 = icmp eq i32 %70, -1
  br i1 %71, label %72, label %74

72:                                               ; preds = %67
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %73) #5
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %103

74:                                               ; preds = %67
  %75 = load i64, ptr %7, align 8, !tbaa !11
  %76 = icmp ugt i64 %75, 6
  br i1 %76, label %77, label %86

77:                                               ; preds = %74
  %78 = load i32, ptr %12, align 4, !tbaa !9
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.logdb_log_header_t, ptr %79, i64 1
  %81 = load i64, ptr %7, align 8, !tbaa !11
  %82 = sub i64 %81, 6
  %83 = call i64 @logdb_io_pwrite(i32 noundef %78, ptr noundef %80, i64 noundef %82, i64 noundef 6)
  %84 = icmp ne i64 %83, 0
  br i1 %84, label %85, label %86

85:                                               ; preds = %77
  br label %97

86:                                               ; preds = %77, %74
  %87 = load i32, ptr %12, align 4, !tbaa !9
  %88 = load ptr, ptr %6, align 8, !tbaa !5
  %89 = call i64 @logdb_io_pwrite(i32 noundef %87, ptr noundef %88, i64 noundef 6, i64 noundef 0)
  %90 = icmp ne i64 %89, 0
  br i1 %90, label %91, label %92

91:                                               ; preds = %86
  br label %97

92:                                               ; preds = %86
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %93) #5
  %94 = load i32, ptr %12, align 4, !tbaa !9
  %95 = load ptr, ptr %4, align 8, !tbaa !5
  %96 = call ptr @logdb_log_new(i32 noundef %94, ptr noundef %95)
  store ptr %96, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %103

97:                                               ; preds = %91, %85
  %98 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %98) #5
  %99 = load i32, ptr %12, align 4, !tbaa !9
  %100 = call i32 @close(i32 noundef %99)
  %101 = load ptr, ptr %4, align 8, !tbaa !5
  %102 = call i32 @unlink(ptr noundef %101) #5
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %103

103:                                              ; preds = %97, %92, %72
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %104

104:                                              ; preds = %103, %59, %49, %25, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %105 = load ptr, ptr %3, align 8
  ret ptr %105
}

; Function Attrs: nounwind
declare i64 @lseek(i32 noundef, i64 noundef, i32 noundef) #3

declare i64 @logdb_io_read(i32 noundef, ptr noundef, i64 noundef) #1

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

; Function Attrs: nounwind
declare ptr @strncpy(ptr noundef, ptr noundef, i64 noundef) #3

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

declare i64 @logdb_io_pwrite(i32 noundef, ptr noundef, i64 noundef, i64 noundef) #1

; Function Attrs: nounwind
declare i32 @unlink(ptr noundef) #3

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 "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 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

!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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0}
!15 = !{!16, !17, i64 4}
!16 = !{!"", !7, i64 0, !17, i64 4}
!17 = !{!"short", !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.logdb_trailer_t = type { i32 }

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

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

; Function Attrs: nofree
declare noundef i32 @open(ptr nocapture noundef readonly, i32 noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @logdb_log_read(i32 noundef, i32 noundef) local_unnamed_addr #2

declare i32 @close(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden ptr @logdb_log_new(i32 noundef, 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) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @logdb_log_create(ptr noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.logdb_trailer_t, align 4
  %4 = tail call i64 @lseek(i32 noundef %1, i64 noundef 0, i32 noundef 2) #9
  %5 = icmp eq i64 %4, -1
  br i1 %5, label %45, label %6

6:                                                ; preds = %2
  %7 = icmp ugt i64 %4, 15
  br i1 %7, label %8, label %.thread45

8:                                                ; preds = %6
  %9 = tail call i64 @lseek(i32 noundef %1, i64 noundef -4, i32 noundef 2) #9
  %10 = icmp eq i64 %9, -1
  br i1 %10, label %45, label %11

11:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #9
  %12 = call i64 @logdb_io_read(i32 noundef %1, ptr noundef nonnull %3, i64 noundef 4) #9
  %13 = icmp sgt i64 %12, 0
  br i1 %13, label %.thread, label %14

14:                                               ; preds = %11
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = sub nsw i64 0, %16
  %18 = call i64 @lseek(i32 noundef %1, i64 noundef %17, i32 noundef 2) #9
  %.not51 = icmp eq i64 %18, -1
  br i1 %.not51, label %.thread, label %19

.thread:                                          ; preds = %11, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #9
  br label %45

19:                                               ; preds = %14
  %20 = call ptr @logdb_log_read(i32 noundef %1, i32 noundef 2)
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = zext i32 %21 to i64
  %23 = add nsw i64 %22, -4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #9
  %.not = icmp eq ptr %20, null
  br i1 %.not, label %.thread45, label %28

.thread45:                                        ; preds = %6, %19
  %.23650 = phi i64 [ %23, %19 ], [ 6, %6 ]
  %24 = call noalias dereferenceable_or_null(6) ptr @malloc(i64 noundef 6) #10
  %.not39 = icmp eq ptr %24, null
  br i1 %.not39, label %45, label %25

25:                                               ; preds = %.thread45
  %26 = call ptr @strncpy(ptr noundef nonnull dereferenceable(1) %24, ptr noundef nonnull dereferenceable(1) @.str, i64 noundef 4) #9
  %27 = getelementptr inbounds i8, ptr %24, i64 4
  store i16 1, ptr %27, align 2, !tbaa !10
  br label %28

28:                                               ; preds = %25, %19
  %.23649 = phi i64 [ %23, %19 ], [ %.23650, %25 ]
  %.331 = phi ptr [ %20, %19 ], [ %24, %25 ]
  %29 = call i32 (ptr, i32, ...) @open(ptr noundef %0, i32 noundef 1218, i32 noundef 384) #9
  %30 = icmp eq i32 %29, -1
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  call void @free(ptr noundef nonnull %.331) #9
  br label %45

32:                                               ; preds = %28
  %33 = icmp ugt i64 %.23649, 6
  br i1 %33, label %34, label %38

34:                                               ; preds = %32
  %35 = getelementptr inbounds i8, ptr %.331, i64 6
  %36 = add nsw i64 %.23649, -6
  %37 = call i64 @logdb_io_pwrite(i32 noundef %29, ptr noundef nonnull %35, i64 noundef %36, i64 noundef 6) #9
  %.not40 = icmp eq i64 %37, 0
  br i1 %.not40, label %38, label %42

38:                                               ; preds = %34, %32
  %39 = call i64 @logdb_io_pwrite(i32 noundef %29, ptr noundef nonnull %.331, i64 noundef 6, i64 noundef 0) #9
  %.not41 = icmp eq i64 %39, 0
  br i1 %.not41, label %40, label %42

40:                                               ; preds = %38
  call void @free(ptr noundef nonnull %.331) #9
  %41 = call ptr @logdb_log_new(i32 noundef %29, ptr noundef %0)
  br label %45

42:                                               ; preds = %38, %34
  call void @free(ptr noundef nonnull %.331) #9
  %43 = call i32 @close(i32 noundef %29) #9
  %44 = call i32 @unlink(ptr noundef %0) #9
  br label %45

45:                                               ; preds = %.thread, %31, %40, %42, %.thread45, %8, %2
  %.3 = phi ptr [ null, %2 ], [ null, %8 ], [ null, %.thread45 ], [ null, %31 ], [ null, %42 ], [ %41, %40 ], [ null, %.thread ]
  ret ptr %.3
}

; Function Attrs: nounwind
declare i64 @lseek(i32 noundef, i64 noundef, i32 noundef) local_unnamed_addr #4

declare i64 @logdb_io_read(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strncpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly, i64 noundef) local_unnamed_addr #6

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #7

declare i64 @logdb_io_pwrite(i32 noundef, ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @unlink(ptr nocapture noundef readonly) local_unnamed_addr #8

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree "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 = { 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 #5 = { 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 #6 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #8 = { 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 #9 = { nounwind }
attributes #10 = { nounwind allocsize(0) }

!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 = !{!"", !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !12, i64 4}
!11 = !{!"", !8, i64 0, !12, i64 4}
!12 = !{!"short", !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass