; LLVM IR from rociodellasala/TP3_SO :: Kernel (pipe)

; 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.s_pipe = type { i32, i32, i32, i8, i8, i8, i8, i8, [40 x i8], i32, i32 }

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

18:                                               ; preds = %4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %108

19:                                               ; preds = %4
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.s_pipe, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !13
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = icmp eq i32 %22, %23
  br i1 %24, label %25, label %62

25:                                               ; preds = %19
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.s_pipe, ptr %26, i32 0, i32 5
  %28 = load i8, ptr %27, align 2, !tbaa !14
  %29 = zext i8 %28 to i32
  %30 = icmp eq i32 %29, 1
  br i1 %30, label %31, label %62

31:                                               ; preds = %25
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.s_pipe, ptr %32, i32 0, i32 10
  %34 = load i32, ptr %33, align 4, !tbaa !15
  %35 = call i32 @wait(i32 noundef %34)
  store i32 %35, ptr %10, align 4, !tbaa !9
  %36 = load i32, ptr %10, align 4, !tbaa !9
  %37 = icmp eq i32 %36, -1
  br i1 %37, label %38, label %39

38:                                               ; preds = %31
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %108

39:                                               ; preds = %31
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.s_pipe, ptr %40, i32 0, i32 8
  %42 = getelementptr inbounds [40 x i8], ptr %41, i64 0, i64 0
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.s_pipe, ptr %43, i32 0, i32 9
  %45 = load i32, ptr %44, align 4, !tbaa !11
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %42, i64 %46
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = call ptr @strcpy(ptr noundef %47, ptr noundef %48) #4
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.s_pipe, ptr %51, i32 0, i32 9
  %53 = load i32, ptr %52, align 4, !tbaa !11
  %54 = add nsw i32 %53, %50
  store i32 %54, ptr %52, align 4, !tbaa !11
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.s_pipe, ptr %55, i32 0, i32 10
  %57 = load i32, ptr %56, align 4, !tbaa !15
  %58 = call i32 @signal(i32 noundef %57)
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.s_pipe, ptr %59, i32 0, i32 2
  %61 = load i32, ptr %60, align 4, !tbaa !16
  call void @unblockProcess(i32 noundef %61)
  br label %106

62:                                               ; preds = %25, %19
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.s_pipe, ptr %63, i32 0, i32 2
  %65 = load i32, ptr %64, align 4, !tbaa !16
  %66 = load i32, ptr %9, align 4, !tbaa !9
  %67 = icmp eq i32 %65, %66
  br i1 %67, label %68, label %105

68:                                               ; preds = %62
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.s_pipe, ptr %69, i32 0, i32 7
  %71 = load i8, ptr %70, align 4, !tbaa !17
  %72 = zext i8 %71 to i32
  %73 = icmp eq i32 %72, 1
  br i1 %73, label %74, label %105

74:                                               ; preds = %68
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.s_pipe, ptr %75, i32 0, i32 10
  %77 = load i32, ptr %76, align 4, !tbaa !15
  %78 = call i32 @wait(i32 noundef %77)
  store i32 %78, ptr %10, align 4, !tbaa !9
  %79 = load i32, ptr %10, align 4, !tbaa !9
  %80 = icmp eq i32 %79, -1
  br i1 %80, label %81, label %82

81:                                               ; preds = %74
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %108

82:                                               ; preds = %74
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.s_pipe, ptr %83, i32 0, i32 8
  %85 = getelementptr inbounds [40 x i8], ptr %84, i64 0, i64 0
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.s_pipe, ptr %86, i32 0, i32 9
  %88 = load i32, ptr %87, align 4, !tbaa !11
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds i8, ptr %85, i64 %89
  %91 = load ptr, ptr %7, align 8, !tbaa !5
  %92 = call ptr @strcpy(ptr noundef %90, ptr noundef %91) #4
  %93 = load i32, ptr %8, align 4, !tbaa !9
  %94 = load ptr, ptr %6, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.s_pipe, ptr %94, i32 0, i32 9
  %96 = load i32, ptr %95, align 4, !tbaa !11
  %97 = add nsw i32 %96, %93
  store i32 %97, ptr %95, align 4, !tbaa !11
  %98 = load ptr, ptr %6, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.s_pipe, ptr %98, i32 0, i32 10
  %100 = load i32, ptr %99, align 4, !tbaa !15
  %101 = call i32 @signal(i32 noundef %100)
  %102 = load ptr, ptr %6, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.s_pipe, ptr %102, i32 0, i32 1
  %104 = load i32, ptr %103, align 4, !tbaa !13
  call void @unblockProcess(i32 noundef %104)
  br label %105

105:                                              ; preds = %82, %68, %62
  br label %106

106:                                              ; preds = %105, %39
  %107 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %107, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %108

108:                                              ; preds = %106, %81, %38, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %109 = load i32, ptr %5, align 4
  ret i32 %109
}

declare i32 @wait(i32 noundef) #2

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

declare i32 @signal(i32 noundef) #2

declare void @unblockProcess(i32 noundef) #2

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}
!11 = !{!12, !10, i64 60}
!12 = !{!"s_pipe", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 12, !7, i64 13, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 17, !10, i64 60, !10, i64 64}
!13 = !{!12, !10, i64 4}
!14 = !{!12, !7, i64 14}
!15 = !{!12, !10, i64 64}
!16 = !{!12, !10, i64 8}
!17 = !{!12, !7, i64 16}


; 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: nounwind uwtable
define dso_local noundef i32 @write(ptr noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 60
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = sub nsw i32 40, %6
  %8 = icmp slt i32 %7, %2
  br i1 %8, label %57, label %9

9:                                                ; preds = %4
  %10 = getelementptr inbounds i8, ptr %0, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !10
  %12 = icmp eq i32 %11, %3
  br i1 %12, label %13, label %33

13:                                               ; preds = %9
  %14 = getelementptr inbounds i8, ptr %0, i64 14
  %15 = load i8, ptr %14, align 2, !tbaa !11
  %16 = icmp eq i8 %15, 1
  br i1 %16, label %17, label %33

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %0, i64 64
  %19 = load i32, ptr %18, align 4, !tbaa !12
  %20 = tail call i32 @wait(i32 noundef %19) #3
  %21 = icmp eq i32 %20, -1
  br i1 %21, label %57, label %22

22:                                               ; preds = %17
  %23 = getelementptr inbounds i8, ptr %0, i64 17
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %23, i64 %25
  %27 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %26, ptr noundef nonnull dereferenceable(1) %1) #3
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = add nsw i32 %28, %2
  store i32 %29, ptr %5, align 4, !tbaa !5
  %30 = load i32, ptr %18, align 4, !tbaa !12
  %31 = tail call i32 @signal(i32 noundef %30) #3
  %32 = getelementptr inbounds i8, ptr %0, i64 8
  br label %.sink.split

33:                                               ; preds = %13, %9
  %34 = getelementptr inbounds i8, ptr %0, i64 8
  %35 = load i32, ptr %34, align 4, !tbaa !13
  %36 = icmp eq i32 %35, %3
  br i1 %36, label %37, label %57

37:                                               ; preds = %33
  %38 = getelementptr inbounds i8, ptr %0, i64 16
  %39 = load i8, ptr %38, align 4, !tbaa !14
  %40 = icmp eq i8 %39, 1
  br i1 %40, label %41, label %57

41:                                               ; preds = %37
  %42 = getelementptr inbounds i8, ptr %0, i64 64
  %43 = load i32, ptr %42, align 4, !tbaa !12
  %44 = tail call i32 @wait(i32 noundef %43) #3
  %45 = icmp eq i32 %44, -1
  br i1 %45, label %57, label %46

46:                                               ; preds = %41
  %47 = getelementptr inbounds i8, ptr %0, i64 17
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %47, i64 %49
  %51 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %50, ptr noundef nonnull dereferenceable(1) %1) #3
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = add nsw i32 %52, %2
  store i32 %53, ptr %5, align 4, !tbaa !5
  %54 = load i32, ptr %42, align 4, !tbaa !12
  %55 = tail call i32 @signal(i32 noundef %54) #3
  br label %.sink.split

.sink.split:                                      ; preds = %46, %22
  %.sink28 = phi ptr [ %32, %22 ], [ %10, %46 ]
  %56 = load i32, ptr %.sink28, align 4, !tbaa !15
  tail call void @unblockProcess(i32 noundef %56) #3
  br label %57

57:                                               ; preds = %.sink.split, %37, %33, %41, %17, %4
  %.0 = phi i32 [ -1, %4 ], [ -1, %17 ], [ -1, %41 ], [ %2, %33 ], [ %2, %37 ], [ %2, %.sink.split ]
  ret i32 %.0
}

declare i32 @wait(i32 noundef) local_unnamed_addr #1

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

declare i32 @signal(i32 noundef) local_unnamed_addr #1

declare void @unblockProcess(i32 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: 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 #3 = { 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, !7, i64 60}
!6 = !{!"s_pipe", !7, i64 0, !7, i64 4, !7, i64 8, !8, i64 12, !8, i64 13, !8, i64 14, !8, i64 15, !8, i64 16, !8, i64 17, !7, i64 60, !7, i64 64}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!6, !8, i64 14}
!12 = !{!6, !7, i64 64}
!13 = !{!6, !7, i64 8}
!14 = !{!6, !8, i64 16}
!15 = !{!7, !7, i64 0}


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