#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 */
struct dirent *readdir(int /*<<< orphan*/ *);
int strtoul(char *, int /*<<< orphan*/ *, int);
int nextpid(DIR *procdir) {
  struct dirent *ent;
  for (;;) {
    ent = readdir(procdir);
    if (!ent || !ent->d_name)
      return 0;
    if (*ent->d_name > '0' && *ent->d_name <= '9')
      break;
  }
  return strtoul(ent->d_name, NULL, 10);
}


x86 assembly (clang, -O0):
<nextpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1152 <nextpid+0x32>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    115e <nextpid+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <nextpid+0x86>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jle    1189 <nextpid+0x69>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     1189 <nextpid+0x69>
jmp    118e <nextpid+0x6e>
jmp    112c <nextpid+0xc>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtoul@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nextpid>:
push   %rbx
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1030 <readdir@plt>
test   %rax,%rax
je     115b <nextpid+0x3b>
mov    (%rax),%rdi
test   %rdi,%rdi
je     115b <nextpid+0x3b>
movzbl (%rdi),%eax
add    $0xcf,%al
cmp    $0x8,%al
ja     1130 <nextpid+0x10>
xor    %esi,%esi
mov    $0xa,%edx
pop    %rbx
jmp    1040 <strtoul@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
define i32 @nextpid(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.dirent*
store i32* %0, i32** %3
br label %5
5: ; preds = %31, %1
%6 = load i32*, i32** %3
%7 = call %struct.dirent* @readdir(i32* %6)
store %struct.dirent* %7, %struct.dirent** %4
%8 = load %struct.dirent*, %struct.dirent** %4
%9 = icmp ne %struct.dirent* %8, null
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = load %struct.dirent*, %struct.dirent** %4
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i32 0, i32 0
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %10, %5
store i32 0, i32* %2
br label %37
16: ; preds = %10
%17 = load %struct.dirent*, %struct.dirent** %4
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp sgt i32 %21, 48
br i1 %22, label %23, label %31
23: ; preds = %16
%24 = load %struct.dirent*, %struct.dirent** %4
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp sle i32 %28, 57
br i1 %29, label %30, label %31
30: ; preds = %23
br label %32
31: ; preds = %23, %16
br label %5
32: ; preds = %30
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strtoul(i8* %35, i32* null, i32 10)
store i32 %36, i32* %2
br label %37
37: ; preds = %32, %15
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
define i32 @nextpid(i32* %0) {
br label %2
2: ; preds = %9, %1
%3 = tail call %struct.dirent* @readdir(i32* %0)
%4 = icmp eq %struct.dirent* %3, null
br i1 %4, label %15, label %5
5: ; preds = %2
%6 = getelementptr inbounds %struct.dirent, %struct.dirent* %3, i64 0, i32 0
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
br i1 %8, label %15, label %9
9: ; preds = %5
%10 = load i8, i8* %7
%11 = add i8 %10, -49
%12 = icmp ult i8 %11, 9
br i1 %12, label %13, label %2
13: ; preds = %9
%14 = tail call i32 @strtoul(i8* nocapture %7, i32* null, i32 10)
br label %15
15: ; preds = %2, %5, %13
%16 = phi i32 [ %14, %13 ], [ 0, %5 ], [ 0, %2 ]
ret i32 %16
}