#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 /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ run_command(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int is_kernel_thread(int pid) {
  char command[81];
  char pname[81];
  memset(command, 0, 81);
  memset(pname, 0, 81);
  sprintf(command, "ps -p %d -o command=", pid);
  run_command(command, pname);
  printf("process PID : %d, command : %s\n", pid, pname);
  if (pname[0] == '[') {
    printf("process %d is a kernel thread\n", pid);
    return 1;
  } else if (pname[0] != '\0') {
    printf("process %d is a user process\n", pid);
    return 0;
  } else {
    printf("no such process %d\n", pid);
  }
}


x86 assembly (clang, -O0):
<is_kernel_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x51,%edx
call   1040 <memset@plt>
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x51,%edx
call   1040 <memset@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x60(%rbp),%rdi
lea    -0xc0(%rbp),%rsi
call   1050 <run_command@plt>
mov    -0x8(%rbp),%esi
lea    -0xc0(%rbp),%rdx
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0xc0(%rbp),%eax
cmp    $0x5b,%eax
jne    11d9 <is_kernel_thread+0x99>
mov    -0x8(%rbp),%esi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121c <is_kernel_thread+0xdc>
movsbl -0xc0(%rbp),%eax
cmp    $0x0,%eax
je     1206 <is_kernel_thread+0xc6>
mov    -0x8(%rbp),%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    121c <is_kernel_thread+0xdc>
mov    -0x8(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121c <is_kernel_thread+0xdc>
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_kernel_thread>:
push   %rbp
push   %r14
push   %rbx
sub    $0xc0,%rsp
mov    %edi,%ebx
lea    0x60(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x51,%edx
call   1040 <memset@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x51,%edx
call   1040 <memset@plt>
lea    0xe86(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,%edx
call   1060 <sprintf@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1050 <run_command@plt>
lea    0xe7f(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rsp),%al
test   %al,%al
je     11bb <is_kernel_thread+0x7b>
cmp    $0x5b,%al
jne    11c4 <is_kernel_thread+0x84>
mov    $0x1,%ebp
lea    0xe7c(%rip),%rdi
jmp    11cd <is_kernel_thread+0x8d>
lea    0xeb0(%rip),%rdi
jmp    11cd <is_kernel_thread+0x8d>
xor    %ebp,%ebp
lea    0xe87(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0xc0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"ps -p %d -o command=\00"
@.str.1 = [32 x i8] c"process PID : %d, command : %s\0A\00"
@.str.2 = [31 x i8] c"process %d is a kernel thread\0A\00"
@.str.3 = [30 x i8] c"process %d is a user process\0A\00"
@.str.4 = [20 x i8] c"no such process %d\0A\00"
define i32 @is_kernel_thread(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [81 x i8]
%5 = alloca [81 x i8]
store i32 %0, i32* %3
%6 = getelementptr inbounds [81 x i8], [81 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 81)
%8 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 81)
%10 = getelementptr inbounds [81 x i8], [81 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [81 x i8], [81 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%15 = call i32 @run_command(i8* %13, i8* %14)
%16 = load i32, i32* %3
%17 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %16, i8* %17)
%19 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 91
br i1 %22, label %23, label %26
23: ; preds = %1
%24 = load i32, i32* %3
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %24)
store i32 1, i32* %2
br label %38
26: ; preds = %1
%27 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* %3
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %32)
store i32 0, i32* %2
br label %38
34: ; preds = %26
%35 = load i32, i32* %3
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34
br label %38
38: ; preds = %23, %31, %37
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@.str = [21 x i8] c"ps -p %d -o command=\00"
@.str.1 = [32 x i8] c"process PID : %d, command : %s\0A\00"
@.str.2 = [31 x i8] c"process %d is a kernel thread\0A\00"
@.str.3 = [30 x i8] c"process %d is a user process\0A\00"
@.str.4 = [20 x i8] c"no such process %d\0A\00"
define i32 @is_kernel_thread(i32 %0) {
%2 = alloca [81 x i8]
%3 = alloca [81 x i8]
%4 = getelementptr inbounds [81 x i8], [81 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 0
%6 = call i32 @memset(i8* %4, i32 0, i32 81)
%7 = call i32 @memset(i8* %5, i32 0, i32 81)
%8 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = call i32 @run_command(i8* %4, i8* %5)
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %5)
%11 = load i8, i8* %5
switch i8 %11, label %12 [
i8 91, label %14
i8 0, label %13
]
12: ; preds = %1
br label %14
13: ; preds = %1
br label %14
14: ; preds = %1, %13, %12
%15 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), %13 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), %12 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), %1 ]
%16 = phi i32 [ undef, %13 ], [ 0, %12 ], [ 1, %1 ]
%17 = call i32 (i8*, i32, ...) @printf(i8* %15, i32 %0)
ret i32 %16
}