#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int read_sysfs(char *);
int /*<<< orphan*/ sleep(int);
void mte_ksm_scan(void) {
  int cur_count = read_sysfs("/sys/kernel/mm/ksm/"
                             "full_scans");
  int scan_count = cur_count + 1;
  int max_loop_count = 4;
  while ((cur_count < scan_count) && max_loop_count) {
    sleep(1);
    cur_count = read_sysfs("/sys/kernel/mm/ksm/"
                           "full_scans");
    max_loop_count--;
  }
}


x86 assembly (clang, -O0):
<mte_ksm_scan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xed1(%rip),%rdi
call   1030 <read_sysfs@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
movl   $0x4,-0xc(%rbp)
mov    -0x4(%rbp),%ecx
xor    %eax,%eax
cmp    -0x8(%rbp),%ecx
mov    %al,-0xd(%rbp)
jge    1162 <mte_ksm_scan+0x42>
cmpl   $0x0,-0xc(%rbp)
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1172 <mte_ksm_scan+0x52>
jmp    1199 <mte_ksm_scan+0x79>
mov    $0x1,%edi
call   1040 <sleep@plt>
lea    0xe7d(%rip),%rdi
call   1030 <read_sysfs@plt>
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1147 <mte_ksm_scan+0x27>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mte_ksm_scan>:
push   %rbp
push   %r14
push   %rbx
lea    0xed5(%rip),%r14
mov    %r14,%rdi
call   1030 <read_sysfs@plt>
mov    %eax,%ebp
mov    $0xfffffffd,%ebx
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    %r14,%rdi
call   1030 <read_sysfs@plt>
cmp    %ebp,%eax
jg     115f <mte_ksm_scan+0x3f>
lea    0x1(%rbx),%eax
test   %ebx,%ebx
mov    %eax,%ebx
jne    1140 <mte_ksm_scan+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"/sys/kernel/mm/ksm/full_scans\00"
define void @mte_ksm_scan() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @read_sysfs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = add nsw i32 %5, 1
store i32 %6, i32* %2
store i32 4, i32* %3
br label %7
7: ; preds = %16, %0
%8 = load i32, i32* %1
%9 = load i32, i32* %2
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 0
br label %14
14: ; preds = %11, %7
%15 = phi i1 [ false, %7 ], [ %13, %11 ]
br i1 %15, label %16, label %21
16: ; preds = %14
%17 = call i32 @sleep(i32 1)
%18 = call i32 @read_sysfs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 %18, i32* %1
%19 = load i32, i32* %3
%20 = add nsw i32 %19, -1
store i32 %20, i32* %3
br label %7
21: ; preds = %14
ret void
}

LLVM IR (-O2):
@.str = [30 x i8] c"/sys/kernel/mm/ksm/full_scans\00"
define void @mte_ksm_scan() {
%1 = tail call i32 @read_sysfs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %2
2: ; preds = %0, %2
%3 = phi i32 [ 4, %0 ], [ %6, %2 ]
%4 = tail call i32 @sleep(i32 1)
%5 = tail call i32 @read_sysfs(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%6 = add nsw i32 %3, -1
%7 = icmp sle i32 %5, %1
%8 = icmp ne i32 %6, 0
%9 = select i1 %7, i1 %8, i1 false
br i1 %9, label %2, label %10
10: ; preds = %2
ret void
}