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

; 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_connection_t = type { i16, %union.pthread_rwlock_t, i32, i32, ptr, i32 }
%union.pthread_rwlock_t = type { %struct.__pthread_rwlock_arch_t }
%struct.__pthread_rwlock_arch_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i8, [7 x i8], i64, i32 }

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

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

declare i32 @close(i32 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
declare i32 @flock(i32 noundef, i32 noundef) #1

; Function Attrs: nounwind
declare i32 @pthread_rwlock_destroy(ptr noundef) #1

declare i32 @logdb_log_close(ptr noundef) #2

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

15:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %75

16:                                               ; preds = %1
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  call void @logdb_txn_rollback_all(ptr noundef %17)
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.logdb_connection_t, ptr %18, i32 0, i32 5
  %20 = load i32, ptr %19, align 8, !tbaa !11
  %21 = call i32 @pthread_key_delete(i32 noundef %20) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.logdb_connection_t, ptr %22, i32 0, i32 3
  %24 = load i32, ptr %23, align 4, !tbaa !14
  %25 = call i32 @flock(i32 noundef %24, i32 noundef 6) #4
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %27, label %38

27:                                               ; preds = %16
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.logdb_connection_t, ptr %28, i32 0, i32 4
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.logdb_connection_t, ptr %31, i32 0, i32 3
  %33 = load i32, ptr %32, align 4, !tbaa !14
  %34 = call i32 @logdb_log_close_merge(ptr noundef %30, i32 noundef %33)
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %27
  store i32 1, ptr %8, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %36, %27
  br label %38

38:                                               ; preds = %37, %16
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %56, label %41

41:                                               ; preds = %38
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.logdb_connection_t, ptr %42, i32 0, i32 4
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = call i32 @logdb_log_close(ptr noundef %44)
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %55

47:                                               ; preds = %41
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.logdb_connection_t, ptr %48, i32 0, i32 3
  %50 = load i32, ptr %49, align 4, !tbaa !14
  %51 = call i32 @flock(i32 noundef %50, i32 noundef 8) #4
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.logdb_connection_t, ptr %52, i32 0, i32 1
  %54 = call i32 @pthread_rwlock_unlock(ptr noundef %53) #4
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %74

55:                                               ; preds = %41
  br label %56

56:                                               ; preds = %55, %38
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.logdb_connection_t, ptr %57, i32 0, i32 3
  %59 = load i32, ptr %58, align 4, !tbaa !14
  %60 = call i32 @flock(i32 noundef %59, i32 noundef 8) #4
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.logdb_connection_t, ptr %61, i32 0, i32 3
  %63 = load i32, ptr %62, align 4, !tbaa !14
  %64 = call i32 @close(i32 noundef %63)
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.logdb_connection_t, ptr %65, i32 0, i32 1
  %67 = call i32 @pthread_rwlock_unlock(ptr noundef %66) #4
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.logdb_connection_t, ptr %68, i32 0, i32 1
  %70 = call i32 @pthread_rwlock_destroy(ptr noundef %69) #4
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.logdb_connection_t, ptr %71, i32 0, i32 0
  store i16 0, ptr %72, align 8, !tbaa !16
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %73) #4
  store i32 0, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %74

74:                                               ; preds = %56, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %75

75:                                               ; preds = %74, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %76 = load i32, ptr %2, align 4
  ret i32 %76
}

; Function Attrs: nounwind
declare i32 @pthread_rwlock_wrlock(ptr noundef) #1

declare void @logdb_txn_rollback_all(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @pthread_key_delete(i32 noundef) #1

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

; Function Attrs: nounwind
declare i32 @pthread_rwlock_unlock(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { "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 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 = { 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, !10, i64 80}
!12 = !{!"", !13, i64 0, !7, i64 8, !10, i64 64, !10, i64 68, !6, i64 72, !10, i64 80}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !10, i64 68}
!15 = !{!12, !6, i64 72}
!16 = !{!12, !13, 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"

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

declare i32 @close(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @flock(i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @pthread_rwlock_destroy(ptr noundef) local_unnamed_addr #2

declare i32 @logdb_log_close(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @logdb_close(ptr noundef %0) local_unnamed_addr #3 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = tail call i32 @pthread_rwlock_wrlock(ptr noundef nonnull %2) #4
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %31

4:                                                ; preds = %1
  tail call void @logdb_txn_rollback_all(ptr noundef %0) #4
  %5 = getelementptr inbounds i8, ptr %0, i64 80
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = tail call i32 @pthread_key_delete(i32 noundef %6) #4
  %8 = getelementptr inbounds i8, ptr %0, i64 68
  %9 = load i32, ptr %8, align 4, !tbaa !12
  %10 = tail call i32 @flock(i32 noundef %9, i32 noundef 6) #4
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %.critedge

12:                                               ; preds = %4
  %13 = getelementptr inbounds i8, ptr %0, i64 72
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = load i32, ptr %8, align 4, !tbaa !12
  %16 = tail call i32 @logdb_log_close_merge(ptr noundef %14, i32 noundef %15) #4
  %.not21 = icmp eq i32 %16, 0
  br i1 %.not21, label %24, label %.critedge

.critedge:                                        ; preds = %4, %12
  %17 = getelementptr inbounds i8, ptr %0, i64 72
  %18 = load ptr, ptr %17, align 8, !tbaa !13
  %19 = tail call i32 @logdb_log_close(ptr noundef %18) #4
  %.not20 = icmp eq i32 %19, 0
  br i1 %.not20, label %24, label %20

20:                                               ; preds = %.critedge
  %21 = load i32, ptr %8, align 4, !tbaa !12
  %22 = tail call i32 @flock(i32 noundef %21, i32 noundef 8) #4
  %23 = tail call i32 @pthread_rwlock_unlock(ptr noundef nonnull %2) #4
  br label %31

24:                                               ; preds = %.critedge, %12
  %25 = load i32, ptr %8, align 4, !tbaa !12
  %26 = tail call i32 @flock(i32 noundef %25, i32 noundef 8) #4
  %27 = load i32, ptr %8, align 4, !tbaa !12
  %28 = tail call i32 @close(i32 noundef %27) #4
  %29 = tail call i32 @pthread_rwlock_unlock(ptr noundef nonnull %2) #4
  %30 = tail call i32 @pthread_rwlock_destroy(ptr noundef nonnull %2) #4
  tail call void @free(ptr noundef nonnull %0) #4
  br label %31

31:                                               ; preds = %20, %24, %1
  %.1 = phi i32 [ -1, %1 ], [ 0, %24 ], [ -1, %20 ]
  ret i32 %.1
}

; Function Attrs: nounwind
declare i32 @pthread_rwlock_wrlock(ptr noundef) local_unnamed_addr #2

declare void @logdb_txn_rollback_all(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @pthread_key_delete(i32 noundef) local_unnamed_addr #2

declare i32 @logdb_log_close_merge(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @pthread_rwlock_unlock(ptr noundef) local_unnamed_addr #2

attributes #0 = { 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 #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 "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 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 = { 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, !10, i64 80}
!6 = !{!"", !7, i64 0, !8, i64 8, !10, i64 64, !10, i64 68, !11, i64 72, !10, i64 80}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !10, i64 68}
!13 = !{!6, !11, i64 72}


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