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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ strncmp(char *, char *, int);
pid_t get_parent_pid(const pid_t pid, bool *is_thread) {
  FILE *fp;
  char path[PATH_MAX];
  char buffer[4096];
  pid_t tgid = 0, ppid = 0;
  unsigned int got = 0;
  *is_thread = false;
  (void)snprintf(path, sizeof(path), "/proc/%u/status", pid);
  if ((fp = fopen(path, "r")) == NULL)
    return 0;
  while (((got & ((0x01) | (0x02))) != ((0x01) | (0x02))) &&
         (fgets(buffer, sizeof(buffer), fp) != NULL)) {
    if (!strncmp(buffer, "Tgid:", 5)) {
      if (sscanf(buffer + 5, "%10u", &tgid) == 1) {
        got |= (0x01);
      } else {
        tgid = 0;
      }
    }
    if (!strncmp(buffer, "PPid:", 5)) {
      if (sscanf(buffer + 5, "%10u", &ppid) == 1) {
        got |= (0x02);
      } else {
        ppid = 0;
      }
    }
  }
  (void)fclose(fp);
  if ((got & ((0x01) | (0x02))) == ((0x01) | (0x02))) {
    if (tgid != pid) {
      ppid = tgid;
      *is_thread = true;
    }
  } else {
    ppid = 0;
  }
  return ppid;
}


x86 assembly (clang, -O0):
<get_parent_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x1048(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1034(%rbp)
movl   $0x0,-0x1038(%rbp)
movl   $0x0,-0x103c(%rbp)
mov    -0x10(%rbp),%rcx
movl   $0x0,(%rcx)
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xe2b(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x1048(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1211 <get_parent_pid+0xb1>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x1040(%rbp)
jmp    13a2 <get_parent_pid+0x242>
jmp    1216 <get_parent_pid+0xb6>
mov    -0x103c(%rbp),%ecx
and    $0x3,%ecx
xor    %eax,%eax
cmp    $0x3,%ecx
mov    %al,-0x1049(%rbp)
je     1252 <get_parent_pid+0xf2>
lea    -0x1030(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1049(%rbp)
mov    -0x1049(%rbp),%al
test   $0x1,%al
jne    1265 <get_parent_pid+0x105>
jmp    1340 <get_parent_pid+0x1e0>
lea    -0x1030(%rbp),%rdi
lea    0xd9f(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12d0 <get_parent_pid+0x170>
lea    -0x1030(%rbp),%rdi
add    $0x5,%rdi
lea    0xd80(%rip),%rsi
lea    -0x1034(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    12c1 <get_parent_pid+0x161>
mov    -0x103c(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x103c(%rbp)
jmp    12cb <get_parent_pid+0x16b>
movl   $0x0,-0x1034(%rbp)
jmp    12d0 <get_parent_pid+0x170>
lea    -0x1030(%rbp),%rdi
lea    0xd3f(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    133b <get_parent_pid+0x1db>
lea    -0x1030(%rbp),%rdi
add    $0x5,%rdi
lea    0xd15(%rip),%rsi
lea    -0x1038(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    132c <get_parent_pid+0x1cc>
mov    -0x103c(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x103c(%rbp)
jmp    1336 <get_parent_pid+0x1d6>
movl   $0x0,-0x1038(%rbp)
jmp    133b <get_parent_pid+0x1db>
jmp    1216 <get_parent_pid+0xb6>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x103c(%rbp),%eax
and    $0x3,%eax
cmp    $0x3,%eax
jne    1385 <get_parent_pid+0x225>
mov    -0x1034(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1380 <get_parent_pid+0x220>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x1038(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    138f <get_parent_pid+0x22f>
movl   $0x0,-0x1038(%rbp)
mov    -0x1038(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x1040(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_parent_pid>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,(%r14)
lea    0xe4d(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%ecx
call   1050 <snprintf@plt>
lea    0xe4b(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12de <get_parent_pid+0x17e>
mov    %rax,%r13
mov    %r15d,-0x34(%rbp)
mov    %r14,-0x40(%rbp)
xor    %r15d,%r15d
lea    -0x1040(%rbp),%rbx
lea    0xe20(%rip),%r14
lea    0xe24(%rip),%r12
jmp    121d <get_parent_pid+0xbd>
movl   $0x0,-0x2c(%rbp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%eax
not    %eax
test   $0x3,%al
je     12c1 <get_parent_pid+0x161>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12d6 <get_parent_pid+0x176>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1280 <get_parent_pid+0x120>
lea    -0x103b(%rbp),%rdi
lea    0xdc0(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    126c <get_parent_pid+0x10c>
or     $0x1,%r15d
jmp    1280 <get_parent_pid+0x120>
movl   $0x0,-0x30(%rbp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1210 <get_parent_pid+0xb0>
lea    -0x103b(%rbp),%rdi
lea    0xd72(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11fb <get_parent_pid+0x9b>
or     $0x2,%r15d
jmp    1210 <get_parent_pid+0xb0>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%eax
cmp    -0x34(%rbp),%eax
jne    12ef <get_parent_pid+0x18f>
mov    -0x2c(%rbp),%eax
jmp    12e0 <get_parent_pid+0x180>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x40(%rbp),%rcx
movl   $0x1,(%rcx)
jmp    12e0 <get_parent_pid+0x180>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/proc/%u/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"Tgid:\00"
@.str.3 = [5 x i8] c"%10u\00"
@.str.4 = [6 x i8] c"PPid:\00"
define i32 @get_parent_pid(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %7
%17 = alloca i8, i64 %15
store i64 %15, i64* %8
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%18 = load i32*, i32** %5
store i32 0, i32* %18
%19 = trunc i64 %15 to i32
%20 = load i32, i32* %4
%21 = call i32 @snprintf(i8* %17, i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %13
br label %83
25: ; preds = %2
br label %26
26: ; preds = %65, %25
%27 = load i32, i32* %12
%28 = and i32 %27, 3
%29 = icmp ne i32 %28, 3
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%32 = load i32*, i32** %6
%33 = call i32* @fgets(i8* %31, i32 4096, i32* %32)
%34 = icmp ne i32* %33, null
br label %35
35: ; preds = %30, %26
%36 = phi i1 [ false, %26 ], [ %34, %30 ]
br i1 %36, label %37, label %66
37: ; preds = %35
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%39 = call i32 @strncmp(i8* %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%40 = icmp ne i32 %39, 0
br i1 %40, label %51, label %41
41: ; preds = %37
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%43 = getelementptr inbounds i8, i8* %42, i64 5
%44 = call i32 @sscanf(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* %10)
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32, i32* %12
%48 = or i32 %47, 1
store i32 %48, i32* %12
br label %50
49: ; preds = %41
store i32 0, i32* %10
br label %50
50: ; preds = %49, %46
br label %51
51: ; preds = %50, %37
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%53 = call i32 @strncmp(i8* %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%54 = icmp ne i32 %53, 0
br i1 %54, label %65, label %55
55: ; preds = %51
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%57 = getelementptr inbounds i8, i8* %56, i64 5
%58 = call i32 @sscanf(i8* %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* %11)
%59 = icmp eq i32 %58, 1
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = load i32, i32* %12
%62 = or i32 %61, 2
store i32 %62, i32* %12
br label %64
63: ; preds = %55
store i32 0, i32* %11
br label %64
64: ; preds = %63, %60
br label %65
65: ; preds = %64, %51
br label %26
66: ; preds = %35
%67 = load i32*, i32** %6
%68 = call i32 @fclose(i32* %67)
%69 = load i32, i32* %12
%70 = and i32 %69, 3
%71 = icmp eq i32 %70, 3
br i1 %71, label %72, label %80
72: ; preds = %66
%73 = load i32, i32* %10
%74 = load i32, i32* %4
%75 = icmp ne i32 %73, %74
br i1 %75, label %76, label %79
76: ; preds = %72
%77 = load i32, i32* %10
store i32 %77, i32* %11
%78 = load i32*, i32** %5
store i32 1, i32* %78
br label %79
79: ; preds = %76, %72
br label %81
80: ; preds = %66
store i32 0, i32* %11
br label %81
81: ; preds = %80, %79
%82 = load i32, i32* %11
store i32 %82, i32* %3
store i32 1, i32* %13
br label %83
83: ; preds = %81, %24
%84 = load i8*, i8** %7
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/proc/%u/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"Tgid:\00"
@.str.3 = [5 x i8] c"%10u\00"
@.str.4 = [6 x i8] c"PPid:\00"
define i32 @get_parent_pid(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%10 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%11 = bitcast i32* %5 to i8*
store i32 0, i32* %5
store i32 0, i32* %1
%12 = call i32 @snprintf(i8* %8, i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%13 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %53, label %15
15: ; preds = %2
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 5
br label %17
17: ; preds = %15, %40
%18 = phi i32 [ 0, %15 ], [ %41, %40 ]
%19 = call i32* @fgets(i8* %9, i32 4096, i32* %13)
%20 = icmp eq i32* %19, null
br i1 %20, label %51, label %21
21: ; preds = %17
%22 = call i32 @strncmp(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = or i32 %18, 1
br label %30
29: ; preds = %24
store i32 0, i32* %4
br label %30
30: ; preds = %27, %29, %21
%31 = phi i32 [ %18, %21 ], [ %28, %27 ], [ %18, %29 ]
%32 = call i32 @strncmp(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = or i32 %31, 2
br label %40
39: ; preds = %34
store i32 0, i32* %5
br label %40
40: ; preds = %37, %39, %30
%41 = phi i32 [ %31, %30 ], [ %38, %37 ], [ %31, %39 ]
%42 = and i32 %41, 3
%43 = icmp eq i32 %42, 3
br i1 %43, label %44, label %17
44: ; preds = %40
%45 = call i32 @fclose(i32* %13)
%46 = load i32, i32* %4
%47 = icmp eq i32 %46, %0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* %5
br label %53
50: ; preds = %44
store i32 1, i32* %1
br label %53
51: ; preds = %17
%52 = call i32 @fclose(i32* %13)
br label %53
53: ; preds = %51, %50, %48, %2
%54 = phi i32 [ 0, %2 ], [ %49, %48 ], [ %46, %50 ], [ 0, %51 ]
ret i32 %54
}