#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strtok(char *, char *);
void foo(pid_t pid) {
  printf("%d\n", pid);
  if (pid == 1)
    return;
  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, " ");
      foo(atoi(ppid));
    }
    fclose(f);
  }
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe68(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x1,-0x4(%rbp)
jne    11ac <foo+0x2c>
jmp    1285 <foo+0x105>
lea    -0x410(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe47(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     1285 <foo+0x105>
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    1279 <foo+0xf9>
lea    -0x410(%rbp),%rdi
lea    0xdea(%rip),%rsi
call   1080 <strtok@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdda(%rip),%rsi
call   1080 <strtok@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdca(%rip),%rsi
call   1080 <strtok@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdba(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1060 <foo@plt>
mov    -0x418(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
lea    0xe6d(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
cmp    $0x1,%ebx
je     122f <foo+0xaf>
lea    0xe5a(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebx,%edx
call   10a0 <sprintf@plt>
lea    0xe54(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     122f <foo+0xaf>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1227 <foo+0xa7>
lea    0xe25(%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>
mov    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1060 <foo@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

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

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