#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*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ free(char *);
char *make_proc_path(int /*<<< orphan*/);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
char *strtok(char *, char *);
pid_t get_parent_pid(pid_t pid) {
  char *path = make_proc_path(pid);
  int fd = open(path, O_RDONLY);
  char *buf = (char *)malloc(50 * sizeof(char));
  char *token;
  int nfield = 0;
  pid_t parent = 0;
  read(fd, buf, 50);
  token = strtok(buf, " ");
  while ((token = strtok(NULL, " "))) {
    nfield++;
    if (nfield == 3) {
      parent = atoi(token);
      break;
    }
  }
  free(path);
  free(buf);
  return parent;
}


x86 assembly (clang, -O0):
<get_parent_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <make_proc_path@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0x32,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x32,%edx
call   1040 <read@plt>
mov    -0x20(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe18(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1224 <get_parent_pid+0xb4>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x3,-0x2c(%rbp)
jne    121f <get_parent_pid+0xaf>
mov    -0x28(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x30(%rbp)
jmp    1224 <get_parent_pid+0xb4>
jmp    11dd <get_parent_pid+0x6d>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_parent_pid>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
call   1050 <make_proc_path@plt>
mov    %rax,%r14
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
mov    $0x32,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %ebx,%edi
mov    %rax,%rsi
mov    $0x32,%edx
call   1040 <read@plt>
lea    0xe4a(%rip),%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <strtok@plt>
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     1206 <get_parent_pid+0x96>
lea    0xe27(%rip),%rsi
xor    %ebx,%ebx
xor    %edi,%edi
call   1080 <strtok@plt>
test   %rax,%rax
je     1206 <get_parent_pid+0x96>
lea    0xe12(%rip),%rsi
xor    %ebx,%ebx
xor    %edi,%edi
call   1080 <strtok@plt>
test   %rax,%rax
je     1206 <get_parent_pid+0x96>
mov    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [2 x i8] c" \00"
define i32 @get_parent_pid(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
%9 = load i32, i32* %2
%10 = call i8* @make_proc_path(i32 %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = call i64 @malloc(i32 50)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %5
store i32 0, i32* %7
store i32 0, i32* %8
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = call i32 @read(i32 %16, i8* %17, i32 50)
%19 = load i8*, i8** %5
%20 = call i8* @strtok(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %20, i8** %6
br label %21
21: ; preds = %32, %1
%22 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %22, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, 3
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %8
br label %33
32: ; preds = %24
br label %21
33: ; preds = %29, %21
%34 = load i8*, i8** %3
%35 = call i32 @free(i8* %34)
%36 = load i8*, i8** %5
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %8
ret i32 %38
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [2 x i8] c" \00"
define i32 @get_parent_pid(i32 %0) {
%2 = tail call i8* @make_proc_path(i32 %0)
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %2, i32 %3)
%5 = tail call i64 @malloc(i32 50)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @read(i32 %4, i8* %6, i32 50)
%8 = tail call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %19, label %11
11: ; preds = %1
%12 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @atoi(i8* %15)
br label %19
19: ; preds = %1, %11, %14, %17
%20 = phi i32 [ %18, %17 ], [ 0, %14 ], [ 0, %11 ], [ 0, %1 ]
%21 = tail call i32 @free(i8* %2)
%22 = tail call i32 @free(i8* %6)
ret i32 %20
}