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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int sched_get_host_pid(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, ...);
scalar_t__ stat(char *, struct stat *);
int alt_lookup_nspid(int pid) {
  char path[300];
  snprintf(path, sizeof(path), "/proc/%d/ns/pid", pid);
  struct stat oldns_stat, newns_stat;
  if (stat("/proc/self/ns/pid", &oldns_stat) == 0 &&
      stat(path, &newns_stat) == 0) {
    if (oldns_stat.st_ino == newns_stat.st_ino) {
      return pid;
    }
  }
  snprintf(path, sizeof(path), "/proc/%d/root/proc", pid);
  DIR *dir = opendir(path);
  if (dir != NULL) {
    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
      if (entry->d_name[0] >= '1' && entry->d_name[0] <= '9') {
        snprintf(path, sizeof(path), "/proc/%d/root/proc/%s/sched", pid,
                 entry->d_name);
        if (sched_get_host_pid(path) == pid) {
          closedir(dir);
          return atoi(entry->d_name);
        }
      }
    }
    closedir(dir);
  }
  return pid;
}


x86 assembly (clang, -O0):
<alt_lookup_nspid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x12c,%esi
lea    0xe6c(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    0xe6e(%rip),%rdi
lea    -0x148(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    11f9 <alt_lookup_nspid+0x89>
lea    -0x140(%rbp),%rdi
lea    -0x150(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    11f9 <alt_lookup_nspid+0x89>
mov    -0x148(%rbp),%rax
cmp    -0x150(%rbp),%rax
jne    11f4 <alt_lookup_nspid+0x84>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1300 <alt_lookup_nspid+0x190>
jmp    11f9 <alt_lookup_nspid+0x89>
lea    -0x140(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x12c,%esi
lea    0xe13(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x140(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
je     12fa <alt_lookup_nspid+0x18a>
jmp    123c <alt_lookup_nspid+0xcc>
mov    -0x158(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x160(%rbp)
cmp    $0x0,%rax
je     12ee <alt_lookup_nspid+0x17e>
mov    -0x160(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jl     12e9 <alt_lookup_nspid+0x179>
mov    -0x160(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     12e9 <alt_lookup_nspid+0x179>
lea    -0x140(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x160(%rbp),%rax
mov    (%rax),%r8
mov    $0x12c,%esi
lea    0xd90(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x140(%rbp),%rdi
call   1030 <sched_get_host_pid@plt>
cmp    -0x8(%rbp),%eax
jne    12e4 <alt_lookup_nspid+0x174>
mov    -0x158(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x160(%rbp),%rax
mov    (%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    1300 <alt_lookup_nspid+0x190>
jmp    12e9 <alt_lookup_nspid+0x179>
jmp    123c <alt_lookup_nspid+0xcc>
mov    -0x158(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alt_lookup_nspid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x140,%rsp
mov    %edi,%r14d
lea    0xe77(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x12c,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0xe6c(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    11d4 <alt_lookup_nspid+0x64>
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    11d4 <alt_lookup_nspid+0x64>
mov    0x8(%rsp),%rax
cmp    (%rsp),%rax
je     128e <alt_lookup_nspid+0x11e>
lea    0xe47(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x12c,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     128e <alt_lookup_nspid+0x11e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1286 <alt_lookup_nspid+0x116>
mov    %rax,%rbp
lea    0xe18(%rip),%r15
lea    0x10(%rsp),%r12
jmp    1240 <alt_lookup_nspid+0xd0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1286 <alt_lookup_nspid+0x116>
mov    0x0(%rbp),%r8
movzbl (%r8),%eax
add    $0xcf,%al
cmp    $0x8,%al
ja     1230 <alt_lookup_nspid+0xc0>
mov    %r12,%rdi
mov    $0x12c,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1030 <sched_get_host_pid@plt>
cmp    %r14d,%eax
jne    1230 <alt_lookup_nspid+0xc0>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    0x0(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
jmp    128e <alt_lookup_nspid+0x11e>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    %r14d,%eax
add    $0x140,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
%struct.dirent = type { i8* }
@.str = [16 x i8] c"/proc/%d/ns/pid\00"
@.str.1 = [18 x i8] c"/proc/self/ns/pid\00"
@.str.2 = [19 x i8] c"/proc/%d/root/proc\00"
@.str.3 = [28 x i8] c"/proc/%d/root/proc/%s/sched\00"
define i32 @alt_lookup_nspid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [300 x i8]
%5 = alloca %struct.stat
%6 = alloca %struct.stat
%7 = alloca i32*
%8 = alloca %struct.dirent*
store i32 %0, i32* %3
%9 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %9, i32 300, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i64 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %5)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %1
%15 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%16 = call i64 @stat(i8* %15, %struct.stat* %6)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i64, i64* %21
%23 = icmp eq i64 %20, %22
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i32, i32* %3
store i32 %25, i32* %2
br label %81
26: ; preds = %18
br label %27
27: ; preds = %26, %14, %1
%28 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%29 = load i32, i32* %3
%30 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %28, i32 300, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%32 = call i32* @opendir(i8* %31)
store i32* %32, i32** %7
%33 = load i32*, i32** %7
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %79
35: ; preds = %27
br label %36
36: ; preds = %75, %35
%37 = load i32*, i32** %7
%38 = call %struct.dirent* @readdir(i32* %37)
store %struct.dirent* %38, %struct.dirent** %8
%39 = icmp ne %struct.dirent* %38, null
br i1 %39, label %40, label %76
40: ; preds = %36
%41 = load %struct.dirent*, %struct.dirent** %8
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp sge i32 %46, 49
br i1 %47, label %48, label %75
48: ; preds = %40
%49 = load %struct.dirent*, %struct.dirent** %8
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = getelementptr inbounds i8, i8* %51, i64 0
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp sle i32 %54, 57
br i1 %55, label %56, label %75
56: ; preds = %48
%57 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%58 = load i32, i32* %3
%59 = load %struct.dirent*, %struct.dirent** %8
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %57, i32 300, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %58, i8* %61)
%63 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%64 = call i32 @sched_get_host_pid(i8* %63)
%65 = load i32, i32* %3
%66 = icmp eq i32 %64, %65
br i1 %66, label %67, label %74
67: ; preds = %56
%68 = load i32*, i32** %7
%69 = call i32 @closedir(i32* %68)
%70 = load %struct.dirent*, %struct.dirent** %8
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = call i32 @atoi(i8* %72)
store i32 %73, i32* %2
br label %81
74: ; preds = %56
br label %75
75: ; preds = %74, %48, %40
br label %36
76: ; preds = %36
%77 = load i32*, i32** %7
%78 = call i32 @closedir(i32* %77)
br label %79
79: ; preds = %76, %27
%80 = load i32, i32* %3
store i32 %80, i32* %2
br label %81
81: ; preds = %79, %67, %24
%82 = load i32, i32* %2
ret i32 %82
}

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.dirent = type { i8* }
@.str = [16 x i8] c"/proc/%d/ns/pid\00"
@.str.1 = [18 x i8] c"/proc/self/ns/pid\00"
@.str.2 = [19 x i8] c"/proc/%d/root/proc\00"
@.str.3 = [28 x i8] c"/proc/%d/root/proc/%s/sched\00"
define i32 @alt_lookup_nspid(i32 %0) {
%2 = alloca [300 x i8]
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 0
%6 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 300, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = bitcast %struct.stat* %3 to i8*
%8 = bitcast %struct.stat* %4 to i8*
%9 = call i64 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %1
%12 = call i64 @stat(i8* %5, %struct.stat* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i64, i64* %15
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%18 = load i64, i64* %17
%19 = icmp eq i64 %16, %18
br i1 %19, label %48, label %20
20: ; preds = %14, %11, %1
%21 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 300, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%22 = call i32* @opendir(i8* %5)
%23 = icmp eq i32* %22, null
br i1 %23, label %48, label %24
24: ; preds = %20
%25 = call %struct.dirent* @readdir(i32* %22)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %41, label %27
27: ; preds = %24, %38
%28 = phi %struct.dirent* [ %39, %38 ], [ %25, %24 ]
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i64 0, i32 0
%30 = load i8*, i8** %29
%31 = load i8, i8* %30
%32 = add i8 %31, -49
%33 = icmp ult i8 %32, 9
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 300, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %0, i8* %30)
%36 = call i32 @sched_get_host_pid(i8* %5)
%37 = icmp eq i32 %36, %0
br i1 %37, label %43, label %38
38: ; preds = %34, %27
%39 = call %struct.dirent* @readdir(i32* %22)
%40 = icmp eq %struct.dirent* %39, null
br i1 %40, label %41, label %27
41: ; preds = %38, %24
%42 = call i32 @closedir(i32* %22)
br label %48
43: ; preds = %34
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i64 0, i32 0
%45 = call i32 @closedir(i32* %22)
%46 = load i8*, i8** %44
%47 = call i32 @atoi(i8* %46)
br label %48
48: ; preds = %20, %41, %43, %14
%49 = phi i32 [ %0, %14 ], [ %47, %43 ], [ %0, %41 ], [ %0, %20 ]
ret i32 %49
}