#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 */
typedef int /*<<< orphan*/ status;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strrchr(char *, char);
int /*<<< orphan*/ *strstr(char *, char *);
scalar_t__ strtol(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int nspid_for_pid(int pid) {
  char status[64];
  snprintf(status, sizeof(status), "/proc/%d/status", pid);
  FILE *status_file = fopen(status, "r");
  if (status_file != NULL) {
    char *line = NULL;
    size_t size;
    while (getline(&line, &size, status_file) != -1) {
      if (strstr(line, "NStgid:") != NULL) {
        pid = (int)strtol(strrchr(line, '\t'), NULL, 10);
      }
    }
    free(line);
    fclose(status_file);
  }
  return pid;
}


x86 assembly (clang, -O0):
<nspid_for_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x40,%esi
lea    0xe62(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1236 <nspid_for_pid+0xb6>
movq   $0x0,-0x60(%rbp)
mov    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rdi
lea    -0x68(%rbp),%rsi
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     1224 <nspid_for_pid+0xa4>
mov    -0x60(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     121f <nspid_for_pid+0x9f>
mov    -0x60(%rbp),%rdi
mov    $0x9,%esi
call   1060 <strrchr@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x4(%rbp)
jmp    11ca <nspid_for_pid+0x4a>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nspid_for_pid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %edi,%r14d
lea    0xe68(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
lea    0xe5c(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1258 <nspid_for_pid+0xd8>
mov    %rax,%rbx
movq   $0x0,(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     1247 <nspid_for_pid+0xc7>
lea    0xe26(%rip),%r15
mov    %rsp,%r12
lea    0x8(%rsp),%r13
jmp    1213 <nspid_for_pid+0x93>
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     1247 <nspid_for_pid+0xc7>
mov    (%rsp),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     1200 <nspid_for_pid+0x80>
mov    %rbp,%rdi
mov    $0x9,%esi
call   1060 <strrchr@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,%r14
jmp    1200 <nspid_for_pid+0x80>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"NStgid:\00"
define i32 @nspid_for_pid(i32 %0) {
%2 = alloca i32
%3 = alloca [64 x i8]
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
store i32 %0, i32* %2
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %34
14: ; preds = %1
store i8* null, i8** %5
br label %15
15: ; preds = %28, %14
%16 = load i32*, i32** %4
%17 = call i32 @getline(i8** %5, i64* %6, i32* %16)
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %29
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = call i32* @strstr(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i8*, i8** %5
%25 = call i32 @strrchr(i8* %24, i8 signext 9)
%26 = call i64 @strtol(i32 %25, i32* null, i32 10)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %2
br label %28
28: ; preds = %23, %19
br label %15
29: ; preds = %15
%30 = load i8*, i8** %5
%31 = call i32 @free(i8* %30)
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %29, %1
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"NStgid:\00"
define i32 @nspid_for_pid(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %32, label %9
9: ; preds = %1
%10 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%11 = bitcast i64* %4 to i8*
%12 = call i32 @getline(i8** %3, i64* %4, i32* %7)
%13 = icmp eq i32 %12, -1
br i1 %13, label %27, label %14
14: ; preds = %9, %23
%15 = phi i32 [ %24, %23 ], [ %0, %9 ]
%16 = load i8*, i8** %3
%17 = call i32* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = call i32 @strrchr(i8* %16, i8 signext 9)
%21 = call i64 @strtol(i32 %20, i32* null, i32 10)
%22 = trunc i64 %21 to i32
br label %23
23: ; preds = %19, %14
%24 = phi i32 [ %22, %19 ], [ %15, %14 ]
%25 = call i32 @getline(i8** %3, i64* %4, i32* %7)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %14
27: ; preds = %23, %9
%28 = phi i32 [ %0, %9 ], [ %24, %23 ]
%29 = load i8*, i8** %3
%30 = call i32 @free(i8* %29)
%31 = call i32 @fclose(i32* %7)
br label %32
32: ; preds = %27, %1
%33 = phi i32 [ %28, %27 ], [ %0, %1 ]
ret i32 %33
}