; LLVM IR from cvra/robot-software :: lib/cmp_mem_access (example)

; 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.cmp_mem_access_t = type { ptr, i64, i64 }
%struct.cmp_ctx_s = type { i8, ptr, ptr, ptr }

@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [17 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.cmp_mem_access_t, align 8
  %3 = alloca %struct.cmp_ctx_s, align 8
  %4 = alloca [128 x i8], align 16
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca [10 x i8], align 1
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 24, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 32, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 128, ptr %4) #3
  %11 = getelementptr inbounds [128 x i8], ptr %4, i64 0, i64 0
  call void @cmp_mem_access_init(ptr noundef %3, ptr noundef %2, ptr noundef %11, i64 noundef 128)
  %12 = call zeroext i1 @cmp_write_map(ptr noundef %3, i32 noundef 1)
  %13 = call zeroext i1 @cmp_write_str(ptr noundef %3, ptr noundef @.str, i32 noundef 4)
  %14 = call zeroext i1 @cmp_write_sint(ptr noundef %3, i64 noundef 42)
  %15 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %16 = call i64 @cmp_mem_access_get_pos(ptr noundef %2)
  %17 = trunc i64 %16 to i32
  store i32 %17, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %29, %0
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %22, label %32

22:                                               ; preds = %18
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [128 x i8], ptr %4, i64 0, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = zext i8 %26 to i32
  %28 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %27)
  br label %29

29:                                               ; preds = %22
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %6, align 4, !tbaa !5
  br label %18, !llvm.loop !10

32:                                               ; preds = %18
  %33 = getelementptr inbounds [128 x i8], ptr %4, i64 0, i64 0
  call void @cmp_mem_access_ro_init(ptr noundef %3, ptr noundef %2, ptr noundef %33, i64 noundef 128)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %34 = call zeroext i1 @cmp_read_map(ptr noundef %3, ptr noundef %7)
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %35)
  call void @llvm.lifetime.start.p0(i64 10, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 10, ptr %9, align 4, !tbaa !5
  %37 = getelementptr inbounds [10 x i8], ptr %8, i64 0, i64 0
  %38 = call zeroext i1 @cmp_read_str(ptr noundef %3, ptr noundef %37, ptr noundef %9)
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %39 = call zeroext i1 @cmp_read_int(ptr noundef %3, ptr noundef %10)
  %40 = getelementptr inbounds [10 x i8], ptr %8, i64 0, i64 0
  %41 = load i32, ptr %10, align 4, !tbaa !5
  %42 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, ptr noundef %40, i32 noundef %41)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 10, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr %2) #3
  ret i32 0
}

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

declare void @cmp_mem_access_init(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #2

declare zeroext i1 @cmp_write_map(ptr noundef, i32 noundef) #2

declare zeroext i1 @cmp_write_str(ptr noundef, ptr noundef, i32 noundef) #2

declare zeroext i1 @cmp_write_sint(ptr noundef, i64 noundef) #2

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

declare i64 @cmp_mem_access_get_pos(ptr noundef) #2

declare void @cmp_mem_access_ro_init(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #2

declare zeroext i1 @cmp_read_map(ptr noundef, ptr noundef) #2

declare zeroext i1 @cmp_read_str(ptr noundef, ptr noundef, ptr noundef) #2

declare zeroext i1 @cmp_read_int(ptr noundef, ptr noundef) #2

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

!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"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; 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.cmp_mem_access_t = type { ptr, i64, i64 }
%struct.cmp_ctx_s = type { i8, ptr, ptr, ptr }

@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [17 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca %struct.cmp_mem_access_t, align 8
  %2 = alloca %struct.cmp_ctx_s, align 8
  %3 = alloca [128 x i8], align 16
  %4 = alloca i32, align 4
  %5 = alloca [10 x i8], align 1
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %3) #4
  call void @cmp_mem_access_init(ptr noundef nonnull %2, ptr noundef nonnull %1, ptr noundef nonnull %3, i64 noundef 128) #4
  %8 = call zeroext i1 @cmp_write_map(ptr noundef nonnull %2, i32 noundef 1) #4
  %9 = call zeroext i1 @cmp_write_str(ptr noundef nonnull %2, ptr noundef nonnull @.str, i32 noundef 4) #4
  %10 = call zeroext i1 @cmp_write_sint(ptr noundef nonnull %2, i64 noundef 42) #4
  %11 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %12 = call i64 @cmp_mem_access_get_pos(ptr noundef nonnull %1) #4
  %13 = trunc i64 %12 to i32
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %0
  %wide.trip.count = and i64 %12, 2147483647
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %15 = getelementptr inbounds [128 x i8], ptr %3, i64 0, i64 %indvars.iv
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = zext i8 %16 to i32
  %18 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %17)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %0
  call void @cmp_mem_access_ro_init(ptr noundef nonnull %2, ptr noundef nonnull %1, ptr noundef nonnull %3, i64 noundef 128) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #4
  %19 = call zeroext i1 @cmp_read_map(ptr noundef nonnull %2, ptr noundef nonnull %4) #4
  %20 = load i32, ptr %4, align 4, !tbaa !10
  %21 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %20)
  call void @llvm.lifetime.start.p0(i64 10, ptr nonnull %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #4
  store i32 10, ptr %6, align 4, !tbaa !10
  %22 = call zeroext i1 @cmp_read_str(ptr noundef nonnull %2, ptr noundef nonnull %5, ptr noundef nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #4
  %23 = call zeroext i1 @cmp_read_int(ptr noundef nonnull %2, ptr noundef nonnull %7) #4
  %24 = load i32, ptr %7, align 4, !tbaa !10
  %25 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, ptr noundef nonnull %5, i32 noundef %24)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 10, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %1) #4
  ret i32 0
}

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

declare void @cmp_mem_access_init(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare zeroext i1 @cmp_write_map(ptr noundef, i32 noundef) local_unnamed_addr #2

declare zeroext i1 @cmp_write_str(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare zeroext i1 @cmp_write_sint(ptr noundef, i64 noundef) local_unnamed_addr #2

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

declare i64 @cmp_mem_access_get_pos(ptr noundef) local_unnamed_addr #2

declare void @cmp_mem_access_ro_init(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare zeroext i1 @cmp_read_map(ptr noundef, ptr noundef) local_unnamed_addr #2

declare zeroext i1 @cmp_read_str(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare zeroext i1 @cmp_read_int(ptr 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) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass