; LLVM IR from christo1030/ReaderWriter :: OS4 (reader)

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

%union.sem_t = type { i64, [24 x i8] }

@readCount = external dso_local global i32, align 4
@writeCount = external dso_local global i32, align 4
@requestWrite = external dso_local global i32, align 4
@sem = external dso_local global %union.sem_t, align 8
@.str.3 = external hidden unnamed_addr constant [19 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

; Function Attrs: nounwind uwtable
define dso_local ptr @reader(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %6, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  br label %7

7:                                                ; preds = %32, %1
  br label %8

8:                                                ; preds = %7
  store i32 1, ptr %4, align 4, !tbaa !9
  %9 = call i32 @sem_wait(ptr noundef @sem)
  %10 = load i32, ptr @writeCount, align 4, !tbaa !9
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %18

12:                                               ; preds = %8
  %13 = load i32, ptr @requestWrite, align 4, !tbaa !9
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %18

15:                                               ; preds = %12
  %16 = load i32, ptr @readCount, align 4, !tbaa !9
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr @readCount, align 4, !tbaa !9
  br label %19

18:                                               ; preds = %12, %8
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %18, %15
  %20 = call i32 @sem_post(ptr noundef @sem) #4
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %32

23:                                               ; preds = %19
  call void @databaseAccess()
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %24)
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = call i32 @sleep(i32 noundef %26)
  %28 = call i32 @sem_wait(ptr noundef @sem)
  %29 = load i32, ptr @readCount, align 4, !tbaa !9
  %30 = add nsw i32 %29, -1
  store i32 %30, ptr @readCount, align 4, !tbaa !9
  %31 = call i32 @sem_post(ptr noundef @sem) #4
  br label %32

32:                                               ; preds = %23, %19
  %33 = call i32 @sched_yield() #4
  br label %7
}

declare i32 @sem_wait(ptr noundef) #2

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

declare i32 @printf(ptr noundef, ...) #2

declare i32 @sleep(i32 noundef) #2

; Function Attrs: nounwind
declare i32 @sched_yield() #3

; Function Attrs: nounwind uwtable
declare dso_local void @databaseAccess() #1

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


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

%union.sem_t = type { i64, [24 x i8] }

@readCount = external dso_local local_unnamed_addr global i32, align 4
@writeCount = external dso_local local_unnamed_addr global i32, align 4
@requestWrite = external dso_local local_unnamed_addr global i32, align 4
@sem = external dso_local global %union.sem_t, align 8
@.str.3 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: noreturn nounwind uwtable
define dso_local noalias noundef nonnull ptr @reader(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load i32, ptr %0, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %.critedge, %1
  %4 = tail call i32 @sem_wait(ptr noundef nonnull @sem) #5
  %5 = load i32, ptr @writeCount, align 4, !tbaa !5
  %6 = icmp ne i32 %5, 0
  %7 = load i32, ptr @requestWrite, align 4
  %8 = icmp ne i32 %7, 0
  %or.cond.not = select i1 %6, i1 true, i1 %8
  br i1 %or.cond.not, label %.critedge, label %9

9:                                                ; preds = %3
  %10 = load i32, ptr @readCount, align 4, !tbaa !5
  %11 = add nsw i32 %10, 1
  store i32 %11, ptr @readCount, align 4, !tbaa !5
  %12 = tail call i32 @sem_post(ptr noundef nonnull @sem) #5
  tail call void @databaseAccess()
  %13 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %2)
  %14 = tail call i32 @sleep(i32 noundef %2) #5
  %15 = tail call i32 @sem_wait(ptr noundef nonnull @sem) #5
  %16 = load i32, ptr @readCount, align 4, !tbaa !5
  %17 = add nsw i32 %16, -1
  store i32 %17, ptr @readCount, align 4, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %3, %9
  %18 = tail call i32 @sem_post(ptr noundef nonnull @sem) #5
  %19 = tail call i32 @sched_yield() #5
  br label %3
}

declare i32 @sem_wait(ptr noundef) local_unnamed_addr #1

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

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

declare i32 @sleep(i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare dso_local void @databaseAccess() local_unnamed_addr #4

attributes #0 = { noreturn 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 = { 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 = { 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 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 #5 = { 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"}


; Effective O3 passes: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass