; LLVM IR from tezc/sc :: logger (sc_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.sc_log = type { ptr, [256 x i8], [256 x i8], i64, %struct.sc_log_mutex, i32, i8, i8, ptr, ptr }
%struct.sc_log_mutex = type { %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@sc_log = external dso_local global %struct.sc_log, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

declare i32 @fclose(ptr noundef) #4

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

11:                                               ; preds = %2
  %12 = load ptr, ptr @sc_log, align 8, !tbaa !11
  %13 = call i32 @fclose(ptr noundef %12)
  store ptr null, ptr @sc_log, align 8, !tbaa !11
  br label %14

14:                                               ; preds = %11, %2
  store i8 0, ptr getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 2), align 8, !tbaa !16
  store i8 0, ptr getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 1), align 8, !tbaa !16
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %20, label %17

17:                                               ; preds = %14
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %17, %14
  br label %66

21:                                               ; preds = %17
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = call i64 @strlen(ptr noundef %22) #8
  %24 = icmp uge i64 %23, 255
  br i1 %24, label %29, label %25

25:                                               ; preds = %21
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = call i64 @strlen(ptr noundef %26) #8
  %28 = icmp uge i64 %27, 255
  br i1 %28, label %29, label %30

29:                                               ; preds = %25, %21
  br label %57

30:                                               ; preds = %25
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = call i64 @strlen(ptr noundef %32) #8
  %34 = add i64 %33, 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 2), ptr align 1 %31, i64 %34, i1 false)
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = call i64 @strlen(ptr noundef %36) #8
  %38 = add i64 %37, 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 1), ptr align 1 %35, i64 %38, i1 false)
  %39 = call noalias ptr @fopen(ptr noundef getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 1), ptr noundef @.str)
  store ptr %39, ptr %8, align 8, !tbaa !5
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = icmp eq ptr %40, null
  br i1 %41, label %42, label %43

42:                                               ; preds = %30
  br label %57

43:                                               ; preds = %30
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %44, ptr noundef @.str.1) #7
  %46 = icmp slt i32 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %43
  br label %57

48:                                               ; preds = %43
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = call i64 @ftell(ptr noundef %49)
  store i64 %50, ptr %7, align 8, !tbaa !17
  %51 = load i64, ptr %7, align 8, !tbaa !17
  %52 = icmp slt i64 %51, 0
  br i1 %52, label %53, label %54

53:                                               ; preds = %48
  br label %57

54:                                               ; preds = %48
  %55 = load i64, ptr %7, align 8, !tbaa !17
  store i64 %55, ptr getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 3), align 8, !tbaa !18
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %56, ptr @sc_log, align 8, !tbaa !11
  br label %66

57:                                               ; preds = %53, %47, %42, %29
  store i32 -1, ptr %5, align 4, !tbaa !9
  %58 = call ptr @__errno_location() #9
  %59 = load i32, ptr %58, align 4, !tbaa !9
  store i32 %59, ptr %6, align 4, !tbaa !9
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %65

62:                                               ; preds = %57
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = call i32 @fclose(ptr noundef %63)
  br label %65

65:                                               ; preds = %62, %57
  br label %66

66:                                               ; preds = %65, %54, %20
  call void @sc_log_mutex_unlock(ptr noundef getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 4))
  %67 = load i32, ptr %6, align 4, !tbaa !9
  %68 = call ptr @__errno_location() #9
  store i32 %67, ptr %68, align 4, !tbaa !9
  %69 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #7
  ret i32 %69
}

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #5

declare noalias ptr @fopen(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #6

declare i64 @ftell(ptr noundef) #4

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

!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, !6, i64 0}
!12 = !{!"sc_log", !6, i64 0, !7, i64 8, !7, i64 264, !13, i64 520, !14, i64 528, !7, i64 568, !15, i64 572, !15, i64 573, !6, i64 576, !6, i64 584}
!13 = !{!"long", !7, i64 0}
!14 = !{!"sc_log_mutex", !7, i64 0}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{!7, !7, i64 0}
!17 = !{!13, !13, i64 0}
!18 = !{!12, !13, i64 520}


; 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.sc_log = type { ptr, [256 x i8], [256 x i8], i64, %struct.sc_log_mutex, i32, i8, i8, ptr, ptr }
%struct.sc_log_mutex = type { %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@sc_log = external dso_local global %struct.sc_log, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

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

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

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @sc_log_set_file(ptr noundef readonly %0, ptr noundef readonly %1) local_unnamed_addr #1 {
  tail call void @sc_log_mutex_lock(ptr noundef nonnull getelementptr inbounds (i8, ptr @sc_log, i64 528))
  %3 = load ptr, ptr @sc_log, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %6, label %4

4:                                                ; preds = %2
  %5 = tail call i32 @fclose(ptr noundef nonnull %3)
  store ptr null, ptr @sc_log, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %4, %2
  store i8 0, ptr getelementptr inbounds (i8, ptr @sc_log, i64 264), align 8, !tbaa !13
  store i8 0, ptr getelementptr inbounds (i8, ptr @sc_log, i64 8), align 8, !tbaa !13
  %7 = icmp eq ptr %0, null
  %8 = icmp eq ptr %1, null
  %or.cond = or i1 %7, %8
  br i1 %or.cond, label %34, label %9

9:                                                ; preds = %6
  %10 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #5
  %11 = icmp ugt i64 %10, 254
  br i1 %11, label %.thread, label %12

12:                                               ; preds = %9
  %13 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #5
  %14 = icmp ugt i64 %13, 254
  br i1 %14, label %.thread, label %15

15:                                               ; preds = %12
  %16 = add nuw nsw i64 %10, 1
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(1) getelementptr inbounds (i8, ptr @sc_log, i64 264), ptr noundef nonnull align 1 dereferenceable(1) %0, i64 %16, i1 false)
  %17 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #5
  %18 = add i64 %17, 1
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 getelementptr inbounds (i8, ptr @sc_log, i64 8), ptr nonnull align 1 %1, i64 %18, i1 false)
  %19 = tail call noalias ptr @fopen(ptr noundef nonnull getelementptr inbounds (i8, ptr @sc_log, i64 8), ptr noundef nonnull @.str)
  %20 = icmp eq ptr %19, null
  br i1 %20, label %.thread, label %21

21:                                               ; preds = %15
  %22 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef nonnull %19, ptr noundef nonnull @.str.1) #6
  %23 = icmp slt i32 %22, 0
  br i1 %23, label %30, label %24

24:                                               ; preds = %21
  %25 = tail call i64 @ftell(ptr noundef nonnull %19)
  %26 = icmp slt i64 %25, 0
  br i1 %26, label %30, label %27

27:                                               ; preds = %24
  store i64 %25, ptr getelementptr inbounds (i8, ptr @sc_log, i64 520), align 8, !tbaa !14
  store ptr %19, ptr @sc_log, align 8, !tbaa !5
  br label %34

.thread:                                          ; preds = %9, %12, %15
  %28 = tail call ptr @__errno_location() #7
  %29 = load i32, ptr %28, align 4, !tbaa !15
  br label %34

30:                                               ; preds = %21, %24
  %31 = tail call ptr @__errno_location() #7
  %32 = load i32, ptr %31, align 4, !tbaa !15
  %33 = tail call i32 @fclose(ptr noundef nonnull %19)
  br label %34

34:                                               ; preds = %.thread, %30, %6, %27
  %.019 = phi i32 [ 0, %6 ], [ %32, %30 ], [ 0, %27 ], [ %29, %.thread ]
  %.018 = phi i32 [ 0, %6 ], [ -1, %30 ], [ 0, %27 ], [ -1, %.thread ]
  tail call void @sc_log_mutex_unlock(ptr noundef nonnull getelementptr inbounds (i8, ptr @sc_log, i64 528))
  %35 = tail call ptr @__errno_location() #7
  store i32 %.019, ptr %35, align 4, !tbaa !15
  ret i32 %.018
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #3

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

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

attributes #0 = { mustprogress nocallback nofree 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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { 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 #5 = { nounwind willreturn memory(read) }
attributes #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(none) }

!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 = !{!"sc_log", !7, i64 0, !8, i64 8, !8, i64 264, !10, i64 520, !11, i64 528, !8, i64 568, !12, i64 572, !12, i64 573, !7, i64 576, !7, i64 584}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"sc_log_mutex", !8, i64 0}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!8, !8, i64 0}
!14 = !{!6, !10, i64 520}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !8, i64 0}


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