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

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ dirfd(int /*<<< orphan*/ *);
int fstatat(int /*<<< orphan*/, char const *, struct stat *,
            int /*<<< orphan*/);
int /*<<< orphan*/ is_str_conversion_ok(char *);
int /*<<< orphan*/ strtoul(char const *, char **, int);
int get_pid_num_from_dir(DIR *proc_dir, const char *pid_dir_name, pid_t *pid) {
  struct stat p_dir_stat;
  char *tmp_end;
  int ret;
  if (pid == NULL || pid_dir_name == NULL)
    return -1;
  *pid = strtoul(pid_dir_name, &tmp_end, 10);
  if (!is_str_conversion_ok(tmp_end))
    return -1;
  ret = fstatat(dirfd(proc_dir), pid_dir_name, &p_dir_stat, 0);
  if (ret)
    return -1;
  if (!S_ISDIR(p_dir_stat.st_mode))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<get_pid_num_from_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117a <get_pid_num_from_dir+0x2a>
cmpq   $0x0,-0x18(%rbp)
jne    1186 <get_pid_num_from_dir+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <get_pid_num_from_dir+0xc5>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <is_str_conversion_ok@plt>
cmp    $0x0,%eax
jne    11be <get_pid_num_from_dir+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <get_pid_num_from_dir+0xc5>
mov    -0x10(%rbp),%rdi
call   1040 <dirfd@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1070 <fstatat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11f1 <get_pid_num_from_dir+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <get_pid_num_from_dir+0xc5>
mov    -0x28(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    120e <get_pid_num_from_dir+0xbe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <get_pid_num_from_dir+0xc5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_num_from_dir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%r15d
test   %rsi,%rsi
je     11c2 <get_pid_num_from_dir+0x72>
mov    %rdx,%rbp
test   %rdx,%rdx
je     11c2 <get_pid_num_from_dir+0x72>
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %eax,0x0(%rbp)
mov    0x10(%rsp),%rdi
call   1050 <is_str_conversion_ok@plt>
test   %eax,%eax
je     11c2 <get_pid_num_from_dir+0x72>
mov    %r14,%rdi
call   1040 <dirfd@plt>
lea    0x8(%rsp),%rdx
mov    %eax,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1070 <fstatat@plt>
test   %eax,%eax
jne    11c2 <get_pid_num_from_dir+0x72>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x1,%eax
sbb    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @get_pid_num_from_dir(i32* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %13, %3
store i32 -1, i32* %4
br label %40
17: ; preds = %13
%18 = load i8*, i8** %6
%19 = call i32 @strtoul(i8* %18, i8** %9, i32 10)
%20 = load i32*, i32** %7
store i32 %19, i32* %20
%21 = load i8*, i8** %9
%22 = call i32 @is_str_conversion_ok(i8* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %17
store i32 -1, i32* %4
br label %40
25: ; preds = %17
%26 = load i32*, i32** %5
%27 = call i32 @dirfd(i32* %26)
%28 = load i8*, i8** %6
%29 = call i32 @fstatat(i32 %27, i8* %28, %struct.stat* %8, i32 0)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %4
br label %40
33: ; preds = %25
%34 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @S_ISDIR(i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %33
store i32 -1, i32* %4
br label %40
39: ; preds = %33
store i32 0, i32* %4
br label %40
40: ; preds = %39, %38, %32, %24, %16
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @get_pid_num_from_dir(i32* %0, i8* %1, i32* writeonly %2) {
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = bitcast i8** %5 to i8*
%8 = icmp eq i32* %2, null
%9 = icmp eq i8* %1, null
%10 = or i1 %9, %8
br i1 %10, label %26, label %11
11: ; preds = %3
%12 = call i32 @strtoul(i8* %1, i8** %5, i32 10)
store i32 %12, i32* %2
%13 = load i8*, i8** %5
%14 = tail call i32 @is_str_conversion_ok(i8* %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %11
%17 = tail call i32 @dirfd(i32* %0)
%18 = call i32 @fstatat(i32 %17, i8* %1, %struct.stat* %4, i32 0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @S_ISDIR(i32 %22)
%24 = icmp eq i32 %23, 0
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %20, %16, %11, %3
%27 = phi i32 [ -1, %3 ], [ -1, %11 ], [ -1, %16 ], [ %25, %20 ]
ret i32 %27
}