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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ _print(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ strtol(int /*<<< orphan*/, char **, int);
int dirent_task_pid(const struct dirent *entry) {
  char *endptr;
  if (entry->d_type == DT_DIR) {
    errno = 0;
    pid_t tid = (pid_t)strtol(entry->d_name, &endptr, 10);
    if (errno != 0) {
      _print("Error converting dirent name \"%s\"to a long value: %s"
             "\n",
             entry->d_name, strerror(errno));
      ;
      return 0;
    }
    if (*endptr == '\0') {
      return tid;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<dirent_task_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e96(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <dirent_task_pid+0xb0>
mov    0x2e7e(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e58(%rip),%rax
cmpq   $0x0,(%rax)
je     11c0 <dirent_task_pid+0x90>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe51(%rip),%rdi
call   1040 <_print@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e7 <dirent_task_pid+0xb7>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11db <dirent_task_pid+0xab>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <dirent_task_pid+0xb7>
jmp    11e0 <dirent_task_pid+0xb0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirent_task_pid>:
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%rax
mov    0x2ea2(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <dirent_task_pid+0x5f>
mov    %rdi,%rbx
mov    0x2e8b(%rip),%r14
movq   $0x0,(%r14)
mov    0x8(%rdi),%edi
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    (%r14),%rdi
test   %rdi,%rdi
je     1186 <dirent_task_pid+0x56>
mov    0x8(%rbx),%ebx
call   1050 <strerror@plt>
lea    0xe85(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <_print@plt>
jmp    118f <dirent_task_pid+0x5f>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1191 <dirent_task_pid+0x61>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@DT_DIR = global i64 0
@errno = global i64 0
@.str = [54 x i8] c"Error converting dirent name \22%s\22to a long value: %s\0A\00"
define i32 @dirent_task_pid(%struct.dirent* %0) {
%2 = alloca i32
%3 = alloca %struct.dirent*
%4 = alloca i8*
%5 = alloca i32
store %struct.dirent* %0, %struct.dirent** %3
%6 = load %struct.dirent*, %struct.dirent** %3
%7 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @DT_DIR
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %33
11: ; preds = %1
store i64 0, i64* @errno
%12 = load %struct.dirent*, %struct.dirent** %3
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @strtol(i32 %14, i8** %4, i32 10)
store i32 %15, i32* %5
%16 = load i64, i64* @errno
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = load %struct.dirent*, %struct.dirent** %3
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load i64, i64* @errno
%23 = call i32 @strerror(i64 %22)
%24 = call i32 @_print(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %23)
store i32 0, i32* %2
br label %34
25: ; preds = %11
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32, i32* %5
store i32 %31, i32* %2
br label %34
32: ; preds = %25
br label %33
33: ; preds = %32, %1
store i32 0, i32* %2
br label %34
34: ; preds = %33, %30, %18
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@DT_DIR = global i64 0
@errno = global i64 0
@.str = [54 x i8] c"Error converting dirent name \22%s\22to a long value: %s\0A\00"
define i32 @dirent_task_pid(%struct.dirent* nocapture %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = getelementptr inbounds %struct.dirent, %struct.dirent* %0, i64 0, i32 0
%5 = load i64, i64* %4
%6 = load i64, i64* @DT_DIR
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %22
8: ; preds = %1
store i64 0, i64* @errno
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %0, i64 0, i32 1
%10 = load i32, i32* %9
%11 = call i32 @strtol(i32 %10, i8** %2, i32 10)
%12 = load i64, i64* @errno
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %8
%15 = load i32, i32* %9
%16 = call i32 @strerror(i64 %12)
%17 = call i32 @_print(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
br label %23
18: ; preds = %8
%19 = load i8*, i8** %2
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %18, %1
br label %23
23: ; preds = %14, %18, %22
%24 = phi i32 [ 0, %22 ], [ %11, %18 ], [ 0, %14 ]
ret i32 %24
}