; LLVM IR from gem5/gem5 :: util/term (term)

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

@.str.15 = external hidden unnamed_addr constant [12 x i8], align 1
@saved_ios = external dso_local global %struct.termios, align 4
@.str.16 = external hidden unnamed_addr constant [11 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: noreturn
declare void @errx(i32 noundef, ptr noundef, ...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.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) #2

; Function Attrs: nounwind uwtable
define dso_local void @raw_term() #3 {
  %1 = alloca %struct.termios, align 4
  call void @llvm.lifetime.start.p0(i64 60, ptr %1) #5
  %2 = call i32 @tcgetattr(i32 noundef 0, ptr noundef %1) #5
  %3 = icmp slt i32 %2, 0
  br i1 %3, label %4, label %5

4:                                                ; preds = %0
  call void (i32, ptr, ...) @errx(i32 noundef 1, ptr noundef @.str.15) #6
  unreachable

5:                                                ; preds = %0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 @saved_ios, ptr align 4 %1, i64 60, i1 false)
  %6 = getelementptr inbounds %struct.termios, ptr %1, i32 0, i32 0
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = and i32 %7, -5537
  store i32 %8, ptr %6, align 4, !tbaa !5
  %9 = getelementptr inbounds %struct.termios, ptr %1, i32 0, i32 1
  %10 = load i32, ptr %9, align 4, !tbaa !10
  %11 = or i32 %10, 1
  store i32 %11, ptr %9, align 4, !tbaa !10
  %12 = getelementptr inbounds %struct.termios, ptr %1, i32 0, i32 1
  %13 = load i32, ptr %12, align 4, !tbaa !10
  %14 = or i32 %13, 4
  store i32 %14, ptr %12, align 4, !tbaa !10
  %15 = getelementptr inbounds %struct.termios, ptr %1, i32 0, i32 3
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = and i32 %16, -12
  store i32 %17, ptr %15, align 4, !tbaa !11
  %18 = getelementptr inbounds %struct.termios, ptr %1, i32 0, i32 5
  %19 = getelementptr inbounds [32 x i8], ptr %18, i64 0, i64 6
  store i8 1, ptr %19, align 1, !tbaa !12
  %20 = getelementptr inbounds %struct.termios, ptr %1, i32 0, i32 5
  %21 = getelementptr inbounds [32 x i8], ptr %20, i64 0, i64 5
  store i8 0, ptr %21, align 1, !tbaa !12
  %22 = call i32 @tcsetattr(i32 noundef 0, i32 noundef 0, ptr noundef %1) #5
  %23 = icmp slt i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %5
  call void (i32, ptr, ...) @errx(i32 noundef 1, ptr noundef @.str.16) #6
  unreachable

25:                                               ; preds = %5
  %26 = call i32 @atexit(ptr noundef @restore_term) #5
  call void @llvm.lifetime.end.p0(i64 60, ptr %1) #5
  ret void
}

; Function Attrs: nounwind
declare i32 @tcgetattr(i32 noundef, ptr noundef) #4

; Function Attrs: nounwind
declare i32 @tcsetattr(i32 noundef, i32 noundef, ptr noundef) #4

; Function Attrs: nounwind
declare i32 @atexit(ptr noundef) #4

; Function Attrs: nounwind uwtable
declare dso_local void @restore_term() #3

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

!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 = !{!"termios", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !8, i64 16, !8, i64 17, !7, i64 52, !7, i64 56}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!6, !7, i64 12}
!12 = !{!8, !8, 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"

%struct.termios = type { i32, i32, i32, i32, i8, [32 x i8], i32, i32 }

@.str.15 = external hidden unnamed_addr constant [12 x i8], align 1
@saved_ios = external dso_local local_unnamed_addr global %struct.termios, align 4
@.str.16 = external hidden unnamed_addr constant [11 x i8], align 1

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

; Function Attrs: noreturn
declare void @errx(i32 noundef, ptr noundef, ...) local_unnamed_addr #1

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

; 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) #2

; Function Attrs: nounwind uwtable
define dso_local void @raw_term() local_unnamed_addr #3 {
  %1 = alloca %struct.termios, align 4
  call void @llvm.lifetime.start.p0(i64 60, ptr nonnull %1) #6
  %2 = call i32 @tcgetattr(i32 noundef 0, ptr noundef nonnull %1) #6
  %3 = icmp slt i32 %2, 0
  br i1 %3, label %4, label %5

4:                                                ; preds = %0
  call void (i32, ptr, ...) @errx(i32 noundef 1, ptr noundef nonnull @.str.15) #7
  unreachable

5:                                                ; preds = %0
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(60) @saved_ios, ptr noundef nonnull align 4 dereferenceable(60) %1, i64 60, i1 false)
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = and i32 %6, -5537
  store i32 %7, ptr %1, align 4, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %1, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !10
  %10 = or i32 %9, 5
  store i32 %10, ptr %8, align 4, !tbaa !10
  %11 = getelementptr inbounds i8, ptr %1, i64 12
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = and i32 %12, -12
  store i32 %13, ptr %11, align 4, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %1, i64 23
  store i8 1, ptr %14, align 1, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %1, i64 22
  store i8 0, ptr %15, align 2, !tbaa !12
  %16 = call i32 @tcsetattr(i32 noundef 0, i32 noundef 0, ptr noundef nonnull %1) #6
  %17 = icmp slt i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %5
  call void (i32, ptr, ...) @errx(i32 noundef 1, ptr noundef nonnull @.str.16) #7
  unreachable

19:                                               ; preds = %5
  %20 = call i32 @atexit(ptr noundef nonnull @restore_term) #6
  call void @llvm.lifetime.end.p0(i64 60, ptr nonnull %1) #6
  ret void
}

; Function Attrs: nounwind
declare i32 @tcgetattr(i32 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind
declare i32 @tcsetattr(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare i32 @atexit(ptr noundef) local_unnamed_addr #5

; Function Attrs: nounwind uwtable
declare dso_local void @restore_term() #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noreturn "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 nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 "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 = { 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 #6 = { nounwind }
attributes #7 = { noreturn 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 0}
!6 = !{!"termios", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !8, i64 16, !8, i64 17, !7, i64 52, !7, i64 56}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!6, !7, i64 12}
!12 = !{!8, !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass