#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*/ statfile;
typedef int pid_t;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strncmp(char *, char *, int);
int is_kernel_thread(pid_t pid) {
  char statfile[20];
  char buffer[64];
  int ret;
  snprintf(statfile, sizeof(statfile), "/proc/%d/statm", pid);
  FILE *fd = fopen(statfile, "r");
  if (fd == NULL) {
    perror("open process statm file failed");
    return -1;
  }
  if (fgets(buffer, sizeof(buffer), fd) == NULL) {
    perror("read process statm file failed");
    fclose(fd);
    return -1;
  }
  ret = strncmp(buffer, "0 0 0", 3) == 0;
  fclose(fd);
  return ret;
}


x86 assembly (clang, -O0):
<is_kernel_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x14,%esi
lea    0xe82(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x20(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    11ba <is_kernel_thread+0x5a>
lea    0xe68(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <is_kernel_thread+0xca>
lea    -0x60(%rbp),%rdi
mov    -0x70(%rbp),%rdx
mov    $0x40,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11f7 <is_kernel_thread+0x97>
lea    0xe53(%rip),%rdi
call   1080 <perror@plt>
mov    -0x70(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <is_kernel_thread+0xca>
lea    -0x60(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x64(%rbp)
mov    -0x70(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_kernel_thread>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %edi,%ecx
lea    0xe91(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1050 <snprintf@plt>
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11dd <is_kernel_thread+0x7d>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f0 <is_kernel_thread+0x90>
lea    0xe9b(%rip),%rsi
lea    0x20(%rsp),%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11d4 <is_kernel_thread+0x74>
lea    0xe39(%rip),%rdi
call   1080 <perror@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%ebp
jmp    11d4 <is_kernel_thread+0x74>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%d/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"open process statm file failed\00"
@.str.3 = [31 x i8] c"read process statm file failed\00"
@.str.4 = [6 x i8] c"0 0 0\00"
define i32 @is_kernel_thread(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca [64 x i8]
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %3
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %34
17: ; preds = %1
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %7
%20 = call i32* @fgets(i8* %18, i32 64, i32* %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32*, i32** %7
%25 = call i32 @fclose(i32* %24)
store i32 -1, i32* %2
br label %34
26: ; preds = %17
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%28 = call i64 @strncmp(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 3)
%29 = icmp eq i64 %28, 0
%30 = zext i1 %29 to i32
store i32 %30, i32* %6
%31 = load i32*, i32** %7
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* %6
store i32 %33, i32* %2
br label %34
34: ; preds = %26, %22, %15
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%d/statm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"open process statm file failed\00"
@.str.3 = [31 x i8] c"read process statm file failed\00"
@.str.4 = [6 x i8] c"0 0 0\00"
define i32 @is_kernel_thread(i32 %0) {
%2 = alloca [20 x i8]
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %22
11: ; preds = %1
%12 = call i32* @fgets(i8* %5, i32 64, i32* %7)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @fclose(i32* %7)
br label %22
17: ; preds = %11
%18 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 3)
%19 = icmp eq i64 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @fclose(i32* %7)
br label %22
22: ; preds = %17, %14, %9
%23 = phi i32 [ -1, %9 ], [ -1, %14 ], [ %20, %17 ]
ret i32 %23
}