#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 *);
int /*<<< orphan*/ printf(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) {
  int namespace_differs = 0;
  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;
    }
    namespace_differs = 1;
  }
  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);
  }
  if (namespace_differs) {
    printf("WARNING: couldn't find container pid of the target process\n");
  }
  return pid;
}


x86 assembly (clang, -O0):
<alt_lookup_nspid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x12c,%esi
lea    0xe55(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    0xe57(%rip),%rdi
lea    -0x148(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1212 <alt_lookup_nspid+0x92>
lea    -0x140(%rbp),%rdi
lea    -0x150(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1212 <alt_lookup_nspid+0x92>
mov    -0x148(%rbp),%rax
cmp    -0x150(%rbp),%rax
jne    120b <alt_lookup_nspid+0x8b>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132f <alt_lookup_nspid+0x1af>
movl   $0x1,-0xc(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x12c,%esi
lea    0xdfa(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    -0x140(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
je     1313 <alt_lookup_nspid+0x193>
jmp    1255 <alt_lookup_nspid+0xd5>
mov    -0x158(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x160(%rbp)
cmp    $0x0,%rax
je     1307 <alt_lookup_nspid+0x187>
mov    -0x160(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jl     1302 <alt_lookup_nspid+0x182>
mov    -0x160(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     1302 <alt_lookup_nspid+0x182>
lea    -0x140(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x160(%rbp),%rax
mov    (%rax),%r8
mov    $0x12c,%esi
lea    0xd77(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    -0x140(%rbp),%rdi
call   1030 <sched_get_host_pid@plt>
cmp    -0x8(%rbp),%eax
jne    12fd <alt_lookup_nspid+0x17d>
mov    -0x158(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x160(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    132f <alt_lookup_nspid+0x1af>
jmp    1302 <alt_lookup_nspid+0x182>
jmp    1255 <alt_lookup_nspid+0xd5>
mov    -0x158(%rbp),%rdi
call   1070 <closedir@plt>
cmpl   $0x0,-0xc(%rbp)
je     1329 <alt_lookup_nspid+0x1a9>
lea    0xd2d(%rip),%rdi
call   1050 <printf@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   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
mov    %edi,%r14d
lea    0xe65(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x12c,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
lea    0xe5a(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
mov    $0x1,%r13b
test   %rax,%rax
jne    11ec <alt_lookup_nspid+0x6c>
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    11ec <alt_lookup_nspid+0x6c>
mov    0x8(%rsp),%rax
cmp    (%rsp),%rax
je     12af <alt_lookup_nspid+0x12f>
xor    %r13d,%r13d
lea    0xe2f(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x12c,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     129e <alt_lookup_nspid+0x11e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1296 <alt_lookup_nspid+0x116>
mov    %rax,%rbp
lea    0xe00(%rip),%r15
lea    0x10(%rsp),%r12
jmp    1250 <alt_lookup_nspid+0xd0>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1296 <alt_lookup_nspid+0x116>
mov    0x0(%rbp),%r8
movzbl (%r8),%eax
add    $0xcf,%al
cmp    $0x8,%al
ja     1240 <alt_lookup_nspid+0xc0>
mov    %r12,%rdi
mov    $0x12c,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %r12,%rdi
call   1030 <sched_get_host_pid@plt>
cmp    %r14d,%eax
jne    1240 <alt_lookup_nspid+0xc0>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    0x0(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
jmp    12af <alt_lookup_nspid+0x12f>
mov    %rbx,%rdi
call   1070 <closedir@plt>
test   %r13b,%r13b
jne    12af <alt_lookup_nspid+0x12f>
lea    0xda7(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r12
pop    %r13
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"
@.str.4 = [60 x i8] c"WARNING: couldn't find container pid of the target process\0A\00"
define i32 @alt_lookup_nspid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [300 x i8]
%6 = alloca %struct.stat
%7 = alloca %struct.stat
%8 = alloca i32*
%9 = alloca %struct.dirent*
store i32 %0, i32* %3
store i32 0, i32* %4
%10 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %10, i32 300, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i64 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %1
%16 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%17 = call i64 @stat(i8* %16, %struct.stat* %7)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%23 = load i64, i64* %22
%24 = icmp eq i64 %21, %23
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = load i32, i32* %3
store i32 %26, i32* %2
br label %87
27: ; preds = %19
store i32 1, i32* %4
br label %28
28: ; preds = %27, %15, %1
%29 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%30 = load i32, i32* %3
%31 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %29, i32 300, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%33 = call i32* @opendir(i8* %32)
store i32* %33, i32** %8
%34 = load i32*, i32** %8
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %80
36: ; preds = %28
br label %37
37: ; preds = %76, %36
%38 = load i32*, i32** %8
%39 = call %struct.dirent* @readdir(i32* %38)
store %struct.dirent* %39, %struct.dirent** %9
%40 = icmp ne %struct.dirent* %39, null
br i1 %40, label %41, label %77
41: ; preds = %37
%42 = load %struct.dirent*, %struct.dirent** %9
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = getelementptr inbounds i8, i8* %44, i64 0
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp sge i32 %47, 49
br i1 %48, label %49, label %76
49: ; preds = %41
%50 = load %struct.dirent*, %struct.dirent** %9
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = getelementptr inbounds i8, i8* %52, i64 0
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp sle i32 %55, 57
br i1 %56, label %57, label %76
57: ; preds = %49
%58 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%59 = load i32, i32* %3
%60 = load %struct.dirent*, %struct.dirent** %9
%61 = getelementptr inbounds %struct.dirent, %struct.dirent* %60, i32 0, i32 0
%62 = load i8*, i8** %61
%63 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %58, i32 300, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %59, i8* %62)
%64 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%65 = call i32 @sched_get_host_pid(i8* %64)
%66 = load i32, i32* %3
%67 = icmp eq i32 %65, %66
br i1 %67, label %68, label %75
68: ; preds = %57
%69 = load i32*, i32** %8
%70 = call i32 @closedir(i32* %69)
%71 = load %struct.dirent*, %struct.dirent** %9
%72 = getelementptr inbounds %struct.dirent, %struct.dirent* %71, i32 0, i32 0
%73 = load i8*, i8** %72
%74 = call i32 @atoi(i8* %73)
store i32 %74, i32* %2
br label %87
75: ; preds = %57
br label %76
76: ; preds = %75, %49, %41
br label %37
77: ; preds = %37
%78 = load i32*, i32** %8
%79 = call i32 @closedir(i32* %78)
br label %80
80: ; preds = %77, %28
%81 = load i32, i32* %4
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0))
br label %85
85: ; preds = %83, %80
%86 = load i32, i32* %3
store i32 %86, i32* %2
br label %87
87: ; preds = %85, %68, %25
%88 = load i32, i32* %2
ret i32 %88
}

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"
@str = [59 x i8] c"WARNING: couldn't find container pid of the target process\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 %52, label %20
20: ; preds = %14, %11, %1
%21 = phi i1 [ true, %11 ], [ true, %1 ], [ false, %14 ]
%22 = 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)
%23 = call i32* @opendir(i8* %5)
%24 = icmp eq i32* %23, null
br i1 %24, label %49, label %25
25: ; preds = %20
%26 = call %struct.dirent* @readdir(i32* %23)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %47, label %28
28: ; preds = %25, %44
%29 = phi %struct.dirent* [ %45, %44 ], [ %26, %25 ]
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i64 0, i32 0
%31 = load i8*, i8** %30
%32 = load i8, i8* %31
%33 = add i8 %32, -49
%34 = icmp ult i8 %33, 9
br i1 %34, label %35, label %44
35: ; preds = %28
%36 = 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* %31)
%37 = call i32 @sched_get_host_pid(i8* %5)
%38 = icmp eq i32 %37, %0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i64 0, i32 0
%41 = call i32 @closedir(i32* %23)
%42 = load i8*, i8** %40
%43 = call i32 @atoi(i8* %42)
br label %52
44: ; preds = %35, %28
%45 = call %struct.dirent* @readdir(i32* %23)
%46 = icmp eq %struct.dirent* %45, null
br i1 %46, label %47, label %28
47: ; preds = %44, %25
%48 = call i32 @closedir(i32* %23)
br i1 %21, label %52, label %50
49: ; preds = %20
br i1 %21, label %52, label %50
50: ; preds = %47, %49
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %39, %47, %50, %49, %14
%53 = phi i32 [ %0, %14 ], [ %0, %47 ], [ %0, %50 ], [ %0, %49 ], [ %43, %39 ]
ret i32 %53
}