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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ strtoul(char *, int /*<<< orphan*/ *, int);
uint64_t read_pmd_pagesize(void) {
  int fd;
  char buf[20];
  ssize_t num_read;
  fd = open("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", O_RDONLY);
  if (fd == -1) {
    perror("Open hpage_pmd_size failed");
    exit(EXIT_FAILURE);
  }
  num_read = read(fd, buf, 19);
  if (num_read < 1) {
    close(fd);
    perror("Read hpage_pmd_size failed");
    exit(EXIT_FAILURE);
  }
  buf[num_read] = '\0';
  close(fd);
  return strtoul(buf, NULL, 10);
}


x86 assembly (clang, -O0):
<read_pmd_pagesize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11a4 <read_pmd_pagesize+0x44>
lea    0xea2(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x13,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    11e4 <read_pmd_pagesize+0x84>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
lea    0xe7d(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movslq -0x24(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pmd_pagesize>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b6 <read_pmd_pagesize+0x56>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x13,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11bf <read_pmd_pagesize+0x5f>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %ebx,%edi
call   1030 <close@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtoul@plt>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe76(%rip),%rdi
jmp    11cd <read_pmd_pagesize+0x6d>
mov    %ebx,%edi
call   1030 <close@plt>
lea    0xe81(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [51 x i8] c"/sys/kernel/mm/transparent_hugepage/hpage_pmd_size\00"
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"Open hpage_pmd_size failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [27 x i8] c"Read hpage_pmd_size failed\00"
define i32 @read_pmd_pagesize() {
%1 = alloca i32
%2 = alloca [20 x i8]
%3 = alloca i32
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i32, i32* %1
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 19)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = load i32, i32* %1
%20 = call i32 @close(i32 %19)
%21 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %12
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 %26
store i8 0, i8* %27
%28 = load i32, i32* %1
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%31 = call i32 @strtoul(i8* %30, i32* null, i32 10)
ret i32 %31
}

LLVM IR (-O2):
@.str = [51 x i8] c"/sys/kernel/mm/transparent_hugepage/hpage_pmd_size\00"
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"Open hpage_pmd_size failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [27 x i8] c"Read hpage_pmd_size failed\00"
define i32 @read_pmd_pagesize() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = call i32 @read(i32 %4, i8* %2, i32 19)
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = tail call i32 @close(i32 %4)
%15 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
%19 = zext i32 %11 to i64
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 %19
store i8 0, i8* %20
%21 = tail call i32 @close(i32 %4)
%22 = call i32 @strtoul(i8* nocapture %2, i32* null, i32 10)
ret i32 %22
}