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

/* Variables and functions */
struct dirent *readdir(int /*<<< orphan*/ *);
int strtol(int /*<<< orphan*/, char **, int);
int __next_pid(DIR *dir) {
  while (1) {
    struct dirent *de = readdir(dir);
    if (!de)
      return 0;
    char *eol = NULL;
    int pid = strtol(de->d_name, &eol, 10);
    if (*eol != '\0')
      continue;
    return pid;
  }
  return 0;
}


x86 assembly (clang, -O0):
<__next_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1150 <__next_pid+0x30>
movl   $0x0,-0x4(%rbp)
jmp    118a <__next_pid+0x6a>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1184 <__next_pid+0x64>
jmp    112c <__next_pid+0xc>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__next_pid>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <readdir@plt>
test   %rax,%rax
je     115f <__next_pid+0x3f>
movq   $0x0,(%rsp)
mov    (%rax),%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1130 <__next_pid+0x10>
jmp    1161 <__next_pid+0x41>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
define i32 @__next_pid(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %3
br label %7
7: ; preds = %1, %22
%8 = load i32*, i32** %3
%9 = call %struct.dirent* @readdir(i32* %8)
store %struct.dirent* %9, %struct.dirent** %4
%10 = load %struct.dirent*, %struct.dirent** %4
%11 = icmp ne %struct.dirent* %10, null
br i1 %11, label %13, label %12
12: ; preds = %7
store i32 0, i32* %2
br label %25
13: ; preds = %7
store i8* null, i8** %5
%14 = load %struct.dirent*, %struct.dirent** %4
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strtol(i32 %16, i8** %5, i32 10)
store i32 %17, i32* %6
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %13
br label %7
23: ; preds = %13
%24 = load i32, i32* %6
store i32 %24, i32* %2
br label %25
25: ; preds = %23, %12
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
define i32 @__next_pid(i32* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
br label %4
4: ; preds = %7, %1
%5 = call %struct.dirent* @readdir(i32* %0)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4
store i8* null, i8** %2
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %5, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @strtol(i32 %9, i8** %2, i32 10)
%11 = load i8*, i8** %2
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %4
14: ; preds = %4, %7
%15 = phi i32 [ %10, %7 ], [ 0, %4 ]
ret i32 %15
}