#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 */
typedef int uint64_t;
typedef int /*<<< orphan*/ tmpfs_loc;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *mkdtemp(char *);
int mount(char *, char const *, char *, int /*<<< orphan*/, char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int rmdir(char const *);
int snprintf(char *, int, char *, char const *);
int umount(char const *);
int unlink(char *);
int write(int, char const *, int);
int /*<<< orphan*/ write_debugfs(char *, char *, int, int);
void split_file_backed_thp(void) {
  int status;
  int fd;
  ssize_t num_written;
  char tmpfs_template[] = "/tmp/thp_split_XXXXXX";
  const char *tmpfs_loc = mkdtemp(tmpfs_template);
  char testfile[80];
  uint64_t pgoff_start = 0, pgoff_end = 1024;
  printf("Please enable pr_debug in split_huge_pages_in_file() if you need "
         "more info.\n");
  status = mount("tmpfs", tmpfs_loc, "tmpfs", 0, "huge=always,size=4m");
  if (status) {
    printf("Unable to create a tmpfs for testing\n");
    exit(EXIT_FAILURE);
  }
  status = snprintf(testfile, 80, "%s/thp_file", tmpfs_loc);
  if (status >= 80) {
    printf("Fail to create file-backed THP split testing file\n");
    goto cleanup;
  }
  fd = open(testfile, O_CREAT | O_WRONLY);
  if (fd == -1) {
    perror("Cannot open testing file\n");
    goto cleanup;
  }
  num_written = write(fd, tmpfs_loc, sizeof(tmpfs_loc));
  close(fd);
  if (num_written < 1) {
    printf("Fail to write data to testing file\n");
    goto cleanup;
  }
  write_debugfs("%s,0x%lx,0x%lx", testfile, pgoff_start, pgoff_end);
  status = unlink(testfile);
  if (status)
    perror("Cannot remove testing file\n");
cleanup:
  status = umount(tmpfs_loc);
  if (status) {
    printf("Unable to umount %s\n", tmpfs_loc);
    exit(EXIT_FAILURE);
  }
  status = rmdir(tmpfs_loc);
  if (status) {
    perror("cannot remove tmp dir");
    exit(EXIT_FAILURE);
  }
  printf("file-backed THP split test done, please check dmesg for more "
         "information\n");
}


x86 assembly (clang, -O0):
<split_file_backed_thp>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    0xe1e(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe1b(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe19(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xe13(%rip),%ax
mov    %ax,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
call   10c0 <mkdtemp@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x94(%rbp)
movl   $0x400,-0x98(%rbp)
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x38(%rbp),%rsi
lea    0xe24(%rip),%rdx
xor    %ecx,%ecx
lea    0xe21(%rip),%r8
mov    %rdx,%rdi
call   1070 <mount@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1279 <split_file_backed_thp+0xa9>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    -0x90(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x50,%esi
lea    0xe13(%rip),%rdx
call   1090 <snprintf@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x50,-0x4(%rbp)
jl     12b5 <split_file_backed_thp+0xe5>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1373 <split_file_backed_thp+0x1a3>
lea    -0x90(%rbp),%rdi
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
mov    0x2cfc(%rip),%rax
or     (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12f1 <split_file_backed_thp+0x121>
lea    0xdfb(%rip),%rdi
call   10e0 <perror@plt>
jmp    1373 <split_file_backed_thp+0x1a3>
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1050 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   10a0 <close@plt>
cmpl   $0x1,-0xc(%rbp)
jge    132a <split_file_backed_thp+0x15a>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1373 <split_file_backed_thp+0x1a3>
lea    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
mov    -0x98(%rbp),%ecx
lea    0xddc(%rip),%rdi
call   10b0 <write_debugfs@plt>
lea    -0x90(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     136e <split_file_backed_thp+0x19e>
lea    0xdc6(%rip),%rdi
call   10e0 <perror@plt>
jmp    1373 <split_file_backed_thp+0x1a3>
mov    -0x38(%rbp),%rdi
call   1040 <umount@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     13a9 <split_file_backed_thp+0x1d9>
mov    -0x38(%rbp),%rsi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2c36(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1060 <rmdir@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     13d9 <split_file_backed_thp+0x209>
lea    0xd9a(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c06(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_file_backed_thp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
movaps 0xe11(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x5858585858585f,%rax
mov    %rax,0xe(%rsp)
mov    %rsp,%rdi
call   10d0 <mkdtemp@plt>
mov    %rax,%rbx
lea    0xe98(%rip),%rdi
call   1040 <puts@plt>
lea    0xdf6(%rip),%rdi
lea    0xdf5(%rip),%r8
mov    %rbx,%rsi
mov    %rdi,%rdx
xor    %ecx,%ecx
call   1080 <mount@plt>
test   %eax,%eax
jne    1323 <split_file_backed_thp+0x143>
lea    0xded(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x50,%esi
mov    %rbx,%rcx
call   10a0 <snprintf@plt>
cmp    $0x50,%eax
jl     126b <split_file_backed_thp+0x8b>
lea    0xf03(%rip),%rdi
call   1040 <puts@plt>
jmp    12f6 <split_file_backed_thp+0x116>
mov    0x2d86(%rip),%rax
mov    0x2d4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x20(%rsp),%rdi
call   10e0 <open@plt>
cmp    $0xffffffff,%eax
je     12ea <split_file_backed_thp+0x10a>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1060 <write@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   10b0 <close@plt>
test   %r14d,%r14d
jle    12dc <split_file_backed_thp+0xfc>
lea    0xda3(%rip),%rdi
lea    0x20(%rsp),%rbp
mov    %rbp,%rsi
xor    %edx,%edx
mov    $0x400,%ecx
call   10c0 <write_debugfs@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
test   %eax,%eax
je     12f6 <split_file_backed_thp+0x116>
lea    0xd8b(%rip),%rdi
jmp    12f1 <split_file_backed_thp+0x111>
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
jmp    12f6 <split_file_backed_thp+0x116>
lea    0xd4b(%rip),%rdi
call   10f0 <perror@plt>
mov    %rbx,%rdi
call   1050 <umount@plt>
test   %eax,%eax
jne    1331 <split_file_backed_thp+0x151>
mov    %rbx,%rdi
call   1070 <rmdir@plt>
test   %eax,%eax
jne    1344 <split_file_backed_thp+0x164>
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6c(%rip),%rdi
call   1040 <puts@plt>
jmp    1350 <split_file_backed_thp+0x170>
lea    0xd49(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    1350 <split_file_backed_thp+0x170>
lea    0xd4b(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@__const.split_file_backed_thp.tmpfs_template = [22 x i8] c"/tmp/thp_split_XXXXXX\00"
@.str = [77 x i8] c"Please enable pr_debug in split_huge_pages_in_file() if you need more info.\0A\00"
@.str.1 = [6 x i8] c"tmpfs\00"
@.str.2 = [20 x i8] c"huge=always,size=4m\00"
@.str.3 = [38 x i8] c"Unable to create a tmpfs for testing\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [12 x i8] c"%s/thp_file\00"
@.str.5 = [51 x i8] c"Fail to create file-backed THP split testing file\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.6 = [26 x i8] c"Cannot open testing file\0A\00"
@.str.7 = [36 x i8] c"Fail to write data to testing file\0A\00"
@.str.8 = [15 x i8] c"%s,0x%lx,0x%lx\00"
@.str.9 = [28 x i8] c"Cannot remove testing file\0A\00"
@.str.10 = [21 x i8] c"Unable to umount %s\0A\00"
@.str.11 = [22 x i8] c"cannot remove tmp dir\00"
@.str.12 = [74 x i8] c"file-backed THP split test done, please check dmesg for more information\0A\00"
define void @split_file_backed_thp() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [22 x i8]
%5 = alloca i8*
%6 = alloca [80 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = bitcast [22 x i8]* %4 to i8*
%10 = getelementptr inbounds [22 x i8], [22 x i8]* %4, i64 0, i64 0
%11 = call i8* @mkdtemp(i8* %10)
store i8* %11, i8** %5
store i32 0, i32* %7
store i32 1024, i32* %8
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %5
%14 = call i32 @mount(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 %14, i32* %1
%15 = load i32, i32* %1
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %0
%22 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%23 = load i8*, i8** %5
%24 = call i32 @snprintf(i8* %22, i32 80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %23)
store i32 %24, i32* %1
%25 = load i32, i32* %1
%26 = icmp sge i32 %25, 80
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0))
br label %61
29: ; preds = %21
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%31 = load i32, i32* @O_CREAT
%32 = load i32, i32* @O_WRONLY
%33 = or i32 %31, %32
%34 = call i32 @open(i8* %30, i32 %33)
store i32 %34, i32* %2
%35 = load i32, i32* %2
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %61
39: ; preds = %29
%40 = load i32, i32* %2
%41 = load i8*, i8** %5
%42 = call i32 @write(i32 %40, i8* %41, i32 8)
store i32 %42, i32* %3
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %3
%46 = icmp slt i32 %45, 1
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
br label %61
49: ; preds = %39
%50 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%51 = load i32, i32* %7
%52 = load i32, i32* %8
%53 = call i32 @write_debugfs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %50, i32 %51, i32 %52)
%54 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%55 = call i32 @unlink(i8* %54)
store i32 %55, i32* %1
%56 = load i32, i32* %1
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %49
%59 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %60
60: ; preds = %58, %49
br label %61
61: ; preds = %60, %47, %37, %27
%62 = load i8*, i8** %5
%63 = call i32 @umount(i8* %62)
store i32 %63, i32* %1
%64 = load i32, i32* %1
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %61
%67 = load i8*, i8** %5
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), i8* %67)
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %61
%72 = load i8*, i8** %5
%73 = call i32 @rmdir(i8* %72)
store i32 %73, i32* %1
%74 = load i32, i32* %1
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %71
%77 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.11, i64 0, i64 0))
%78 = load i32, i32* @EXIT_FAILURE
%79 = call i32 @exit(i32 %78)
unreachable
80: ; preds = %71
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.12, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@__const.split_file_backed_thp.tmpfs_template = [22 x i8] c"/tmp/thp_split_XXXXXX\00"
@.str.1 = [6 x i8] c"tmpfs\00"
@.str.2 = [20 x i8] c"huge=always,size=4m\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [12 x i8] c"%s/thp_file\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.6 = [26 x i8] c"Cannot open testing file\0A\00"
@.str.8 = [15 x i8] c"%s,0x%lx,0x%lx\00"
@.str.9 = [28 x i8] c"Cannot remove testing file\0A\00"
@.str.10 = [21 x i8] c"Unable to umount %s\0A\00"
@.str.11 = [22 x i8] c"cannot remove tmp dir\00"
@str = [76 x i8] c"Please enable pr_debug in split_huge_pages_in_file() if you need more info.\00"
@str.13 = [73 x i8] c"file-backed THP split test done, please check dmesg for more information\00"
@str.14 = [35 x i8] c"Fail to write data to testing file\00"
@str.15 = [50 x i8] c"Fail to create file-backed THP split testing file\00"
@str.16 = [37 x i8] c"Unable to create a tmpfs for testing\00"
define void @split_file_backed_thp() {
%1 = alloca [22 x i8]
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [22 x i8], [22 x i8]* %1, i64 0, i64 0
%4 = call i8* @mkdtemp(i8* %3)
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([76 x i8], [76 x i8]* @str, i64 0, i64 0))
%7 = call i32 @mount(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.16, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 @snprintf(i8* %5, i32 80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%15 = icmp sgt i32 %14, 79
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.15, i64 0, i64 0))
br label %38
18: ; preds = %13
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_WRONLY
%21 = or i32 %20, %19
%22 = call i32 @open(i8* %5, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %38
26: ; preds = %18
%27 = call i32 @write(i32 %22, i8* %4, i32 8)
%28 = call i32 @close(i32 %22)
%29 = icmp slt i32 %27, 1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.14, i64 0, i64 0))
br label %38
32: ; preds = %26
%33 = call i32 @write_debugfs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %5, i32 0, i32 1024)
%34 = call i32 @unlink(i8* %5)
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %38
38: ; preds = %32, %36, %30, %24, %16
%39 = call i32 @umount(i8* %4)
%40 = icmp eq i32 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %38
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), i8* %4)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %38
%46 = call i32 @rmdir(i8* %4)
%47 = icmp eq i32 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.11, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %45
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @str.13, i64 0, i64 0))
ret void
}