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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strtok(char *, char *);
int main() {
  pid_t pid = getpid();
  char buff[1024];
  sprintf(buff, "/proc/%d/stat", pid);
  FILE *f = fopen(buff, "r");
  if (f) {
    size_t sz = fread(buff, sizeof(char), sizeof(buff), f);
    if (sz > 0) {
      strtok(buff, " ");
      strtok(NULL, " ");
      strtok(NULL, " ");
      char *ppid = strtok(NULL, " ");
      printf("%s\n", ppid);
    }
    fclose(f);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe63(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     1265 <main+0xf5>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jbe    1259 <main+0xe9>
lea    -0x410(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   1080 <strtok@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdf6(%rip),%rsi
call   1080 <strtok@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xde6(%rip),%rsi
call   1080 <strtok@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdd6(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rsi
lea    0xdbe(%rip),%rdi
call   1060 <printf@plt>
mov    -0x418(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x408,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe78(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     120d <main+0x9d>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1205 <main+0x95>
lea    0xe43(%rip),%r14
mov    %rsp,%rdi
mov    %r14,%rsi
call   1080 <strtok@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   1080 <strtok@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   1080 <strtok@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   1080 <strtok@plt>
lea    0xe15(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
store i32 0, i32* %1
%7 = call i32 (...) @getpid()
store i32 %7, i32* %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* %2
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, 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** %4
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %32
15: ; preds = %0
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i64 @fread(i8* %16, i32 1, i32 1024, i32* %17)
store i64 %18, i64* %5
%19 = load i64, i64* %5
%20 = icmp ugt i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %15
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%23 = call i8* @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %21, %15
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
br label %32
32: ; preds = %29, %0
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = tail call i32 (...) @getpid()
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %2)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %18, label %7
7: ; preds = %0
%8 = call i64 @fread(i8* %3, i32 1, i32 1024, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %10, %7
%17 = call i32 @fclose(i32* %5)
br label %18
18: ; preds = %16, %0
ret i32 0
}