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

/* Variables and functions */
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 bcc_perf_map_nstgid(int pid) {
  char status_path[64];
  FILE *status;
  snprintf(status_path, sizeof(status_path), "/proc/%d/status", pid);
  status = fopen(status_path, "r");
  if (!status)
    return -1;
  int nstgid = pid;
  size_t size = 0;
  char *line = NULL;
  while (getline(&line, &size, status) != -1) {
    if (strstr(line, "Tgid:") != NULL)
      nstgid = (int)strtol(strrchr(line, '\t'), NULL, 10);
    if (strstr(line, "NStgid:") != NULL)
      nstgid = (int)strtol(strrchr(line, '\t'), NULL, 10);
  }
  free(line);
  return nstgid;
}


x86 assembly (clang, -O0):
<bcc_perf_map_nstgid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe72(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11be <bcc_perf_map_nstgid+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <bcc_perf_map_nstgid+0x108>
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
movq   $0x0,-0x68(%rbp)
movq   $0x0,-0x70(%rbp)
mov    -0x58(%rbp),%rdx
lea    -0x70(%rbp),%rdi
lea    -0x68(%rbp),%rsi
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1269 <bcc_perf_map_nstgid+0xf9>
mov    -0x70(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1229 <bcc_perf_map_nstgid+0xb9>
mov    -0x70(%rbp),%rdi
mov    $0x9,%esi
call   1050 <strrchr@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x70(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1264 <bcc_perf_map_nstgid+0xf4>
mov    -0x70(%rbp),%rdi
mov    $0x9,%esi
call   1050 <strrchr@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x5c(%rbp)
jmp    11d4 <bcc_perf_map_nstgid+0x64>
mov    -0x70(%rbp),%rdi
call   1030 <free@plt>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcc_perf_map_nstgid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %edi,%ebx
lea    0xe79(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
lea    0xe6e(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1288 <bcc_perf_map_nstgid+0x118>
mov    %rax,%r14
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     127d <bcc_perf_map_nstgid+0x10d>
lea    0xe31(%rip),%r12
mov    %rsp,%r13
lea    0x8(%rsp),%rbp
jmp    1213 <bcc_perf_map_nstgid+0xa3>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     127d <bcc_perf_map_nstgid+0x10d>
mov    (%rsp),%r15
mov    %r15,%rdi
lea    0xdf1(%rip),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     124d <bcc_perf_map_nstgid+0xdd>
mov    %r15,%rdi
mov    $0x9,%esi
call   1050 <strrchr@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%rbx
mov    (%rsp),%r15
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     1200 <bcc_perf_map_nstgid+0x90>
mov    %r15,%rdi
mov    $0x9,%esi
call   1050 <strrchr@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%rbx
jmp    1200 <bcc_perf_map_nstgid+0x90>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    128d <bcc_perf_map_nstgid+0x11d>
mov    $0xffffffff,%ebx
mov    %ebx,%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 = [6 x i8] c"Tgid:\00"
@.str.3 = [8 x i8] c"NStgid:\00"
define i32 @bcc_perf_map_nstgid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
store i32 %0, i32* %3
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %1
store i32 -1, i32* %2
br label %46
17: ; preds = %1
%18 = load i32, i32* %3
store i32 %18, i32* %6
store i64 0, i64* %7
store i8* null, i8** %8
br label %19
19: ; preds = %41, %17
%20 = load i32*, i32** %5
%21 = call i32 @getline(i8** %8, i64* %7, i32* %20)
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %42
23: ; preds = %19
%24 = load i8*, i8** %8
%25 = call i32* @strstr(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i8*, i8** %8
%29 = call i32 @strrchr(i8* %28, i8 signext 9)
%30 = call i64 @strtol(i32 %29, i32* null, i32 10)
%31 = trunc i64 %30 to i32
store i32 %31, i32* %6
br label %32
32: ; preds = %27, %23
%33 = load i8*, i8** %8
%34 = call i32* @strstr(i8* %33, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i8*, i8** %8
%38 = call i32 @strrchr(i8* %37, i8 signext 9)
%39 = call i64 @strtol(i32 %38, i32* null, i32 10)
%40 = trunc i64 %39 to i32
store i32 %40, i32* %6
br label %41
41: ; preds = %36, %32
br label %19
42: ; preds = %19
%43 = load i8*, i8** %8
%44 = call i32 @free(i8* %43)
%45 = load i32, i32* %6
store i32 %45, i32* %2
br label %46
46: ; preds = %42, %16
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"Tgid:\00"
@.str.3 = [8 x i8] c"NStgid:\00"
define i32 @bcc_perf_map_nstgid(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca i64
%4 = alloca i8*
%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 %41, label %9
9: ; preds = %1
%10 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%11 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%12 = call i32 @getline(i8** %4, i64* %3, i32* %7)
%13 = icmp eq i32 %12, -1
br i1 %13, label %37, label %14
14: ; preds = %9, %33
%15 = phi i32 [ %34, %33 ], [ %0, %9 ]
%16 = load i8*, i8** %4
%17 = call i32* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %24, 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
%23 = load i8*, i8** %4
br label %24
24: ; preds = %19, %14
%25 = phi i8* [ %23, %19 ], [ %16, %14 ]
%26 = phi i32 [ %22, %19 ], [ %15, %14 ]
%27 = call i32* @strstr(i8* dereferenceable(1) %25, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = call i32 @strrchr(i8* %25, i8 signext 9)
%31 = call i64 @strtol(i32 %30, i32* null, i32 10)
%32 = trunc i64 %31 to i32
br label %33
33: ; preds = %29, %24
%34 = phi i32 [ %32, %29 ], [ %26, %24 ]
%35 = call i32 @getline(i8** %4, i64* %3, i32* %7)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %14
37: ; preds = %33, %9
%38 = phi i32 [ %0, %9 ], [ %34, %33 ]
%39 = load i8*, i8** %4
%40 = call i32 @free(i8* %39)
br label %41
41: ; preds = %1, %37
%42 = phi i32 [ %38, %37 ], [ -1, %1 ]
ret i32 %42
}