#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 */
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int dup(int);
int /*<<< orphan*/ *fdopendir(int);
int getProcessStatus(int, char *, char *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ readdir_r(int /*<<< orphan*/ *, struct dirent *,
                             struct dirent **);
bool hasChildren(int proc_fd, int pid) {
  bool ret = false;
  int fd = dup(proc_fd);
  lseek(fd, SEEK_SET, 0);
  DIR *dir = fd >= 0 ? fdopendir(fd) : NULL;
  struct dirent de, *res;
  while (dir && !readdir_r(dir, &de, &res) && res) {
    if (res->d_name[0] <= '0' || res->d_name[0] > '9') {
      continue;
    }
    int ppid = getProcessStatus(proc_fd, res->d_name, "PPid");
    if (ppid == pid) {
      ret = true;
      break;
    }
  }
  closedir(dir);
  return ret;
}


x86 assembly (clang, -O0):
<hasChildren>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <lseek@plt>
cmpl   $0x0,-0x10(%rbp)
jl     11ae <hasChildren+0x4e>
mov    -0x10(%rbp),%edi
call   1080 <fdopendir@plt>
mov    %rax,-0x38(%rbp)
jmp    11b9 <hasChildren+0x59>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    11b9 <hasChildren+0x59>
mov    -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x39(%rbp)
je     11fd <hasChildren+0x9d>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <readdir_r@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x39(%rbp)
jne    11fd <hasChildren+0x9d>
cmpq   $0x0,-0x28(%rbp)
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    120d <hasChildren+0xad>
jmp    126e <hasChildren+0x10e>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jle    1233 <hasChildren+0xd3>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    1238 <hasChildren+0xd8>
jmp    11c1 <hasChildren+0x61>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xdb7(%rip),%rdx
call   1030 <getProcessStatus@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1269 <hasChildren+0x109>
movl   $0x1,-0xc(%rbp)
jmp    126e <hasChildren+0x10e>
jmp    11c1 <hasChildren+0x61>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hasChildren>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %edi,%ebp
call   1050 <dup@plt>
mov    %eax,%ebx
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <lseek@plt>
test   %ebx,%ebx
js     120d <hasChildren+0xad>
mov    %ebx,%edi
call   1080 <fdopendir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     120d <hasChildren+0xad>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <readdir_r@plt>
test   %eax,%eax
jne    120f <hasChildren+0xaf>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     120f <hasChildren+0xaf>
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
nop
mov    (%rax),%rsi
movzbl (%rsi),%eax
add    $0xc6,%al
cmp    $0xf6,%al
jbe    11ef <hasChildren+0x8f>
mov    %ebp,%edi
lea    0xe1b(%rip),%rdx
call   1030 <getProcessStatus@plt>
cmp    %r15d,%eax
je     1229 <hasChildren+0xc9>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <readdir_r@plt>
test   %eax,%eax
jne    120f <hasChildren+0xaf>
mov    0x8(%rsp),%rax
test   %rax,%rax
jne    11d0 <hasChildren+0x70>
jmp    120f <hasChildren+0xaf>
xor    %ebx,%ebx
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r14d
jmp    120f <hasChildren+0xaf>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@SEEK_SET = global i32 0
@.str = [5 x i8] c"PPid\00"
define i32 @hasChildren(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.dirent
%9 = alloca %struct.dirent*
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
%11 = load i32, i32* %3
%12 = call i32 @dup(i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %13, i32 %14, i32 0)
%16 = load i32, i32* %6
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32* @fdopendir(i32 %19)
br label %22
21: ; preds = %2
br label %22
22: ; preds = %21, %18
%23 = phi i32* [ %20, %18 ], [ null, %21 ]
store i32* %23, i32** %7
br label %24
24: ; preds = %63, %52, %22
%25 = load i32*, i32** %7
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i32*, i32** %7
%29 = call i32 @readdir_r(i32* %28, %struct.dirent* %8, %struct.dirent** %9)
%30 = icmp ne i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = load %struct.dirent*, %struct.dirent** %9
%33 = icmp ne %struct.dirent* %32, null
br label %34
34: ; preds = %31, %27, %24
%35 = phi i1 [ false, %27 ], [ false, %24 ], [ %33, %31 ]
br i1 %35, label %36, label %64
36: ; preds = %34
%37 = load %struct.dirent*, %struct.dirent** %9
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = getelementptr inbounds i8, i8* %39, i64 0
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp sle i32 %42, 48
br i1 %43, label %52, label %44
44: ; preds = %36
%45 = load %struct.dirent*, %struct.dirent** %9
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = getelementptr inbounds i8, i8* %47, i64 0
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp sgt i32 %50, 57
br i1 %51, label %52, label %53
52: ; preds = %44, %36
br label %24
53: ; preds = %44
%54 = load i32, i32* %3
%55 = load %struct.dirent*, %struct.dirent** %9
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 @getProcessStatus(i32 %54, i8* %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = load i32, i32* %4
%61 = icmp eq i32 %59, %60
br i1 %61, label %62, label %63
62: ; preds = %53
store i32 1, i32* %5
br label %64
63: ; preds = %53
br label %24
64: ; preds = %62, %34
%65 = load i32*, i32** %7
%66 = call i32 @closedir(i32* %65)
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@SEEK_SET = global i32 0
@.str = [5 x i8] c"PPid\00"
define i32 @hasChildren(i32 %0, i32 %1) {
%3 = alloca %struct.dirent
%4 = alloca %struct.dirent*
%5 = tail call i32 @dup(i32 %0)
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek(i32 %5, i32 %6, i32 0)
%8 = icmp sgt i32 %5, -1
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = bitcast %struct.dirent* %3 to i8*
%11 = bitcast %struct.dirent** %4 to i8*
br label %39
12: ; preds = %2
%13 = tail call i32* @fdopendir(i32 %5)
%14 = bitcast %struct.dirent* %3 to i8*
%15 = bitcast %struct.dirent** %4 to i8*
%16 = icmp eq i32* %13, null
br i1 %16, label %39, label %17
17: ; preds = %12
%18 = call i32 @readdir_r(i32* %13, %struct.dirent* %3, %struct.dirent** %4)
%19 = icmp eq i32 %18, 0
%20 = load %struct.dirent*, %struct.dirent** %4
%21 = icmp ne %struct.dirent* %20, null
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %23, label %39
23: ; preds = %17, %30
%24 = phi %struct.dirent* [ %33, %30 ], [ %20, %17 ]
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = load i8, i8* %26
%28 = add i8 %27, -58
%29 = icmp ult i8 %28, -9
br i1 %29, label %30, label %36
30: ; preds = %23, %36
%31 = call i32 @readdir_r(i32* %13, %struct.dirent* %3, %struct.dirent** %4)
%32 = icmp eq i32 %31, 0
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = icmp ne %struct.dirent* %33, null
%35 = select i1 %32, i1 %34, i1 false
br i1 %35, label %23, label %39
36: ; preds = %23
%37 = call i32 @getProcessStatus(i32 %0, i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%38 = icmp eq i32 %37, %1
br i1 %38, label %39, label %30
39: ; preds = %30, %36, %17, %9, %12
%40 = phi i32* [ null, %12 ], [ null, %9 ], [ %13, %17 ], [ %13, %36 ], [ %13, %30 ]
%41 = phi i32 [ 0, %12 ], [ 0, %9 ], [ 0, %17 ], [ 0, %30 ], [ 1, %36 ]
%42 = bitcast %struct.dirent* %3 to i8*
%43 = bitcast %struct.dirent** %4 to i8*
%44 = call i32 @closedir(i32* %40)
ret i32 %41
}