#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 scalar_t__ uint64_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
char *strstr(char *, char *);
scalar_t__ strtoull(char *, char **, int);
int _get_task_permitted_caps(int pid, int tid, uint64_t *cap) {
  char path[64];
  char buff[1024];
  int len;
  int fd;
  int result = -1;
  char *perm;
  char *end;
  snprintf(path, sizeof path, "/proc/%d/task/%d/status", pid, tid);
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    do {
    } while (0);
    return -1;
  }
  do {
    len = read(fd, buff, sizeof buff - 1);
  } while (len < 0 && errno == EINTR);
  if (len < 0) {
    do {
    } while (0);
    goto EXIT;
  }
  buff[len] = 0;
  perm = strstr(buff, "CapPrm:");
  if (perm == NULL) {
    do {
    } while (0);
    errno = EINVAL;
    goto EXIT;
  }
  errno = 0;
  *cap = (uint64_t)strtoull(perm + 8, &end, 16);
  if (errno == 0) {
    do {
    } while (0);
    result = 0;
  } else {
    do {
    } while (0);
  }
EXIT:
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<_get_task_permitted_caps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x490,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x46c(%rbp)
ff ff ff
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    $0x40,%esi
lea    0xe6a(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x468(%rbp)
cmpl   $0x0,-0x468(%rbp)
jge    11d6 <_get_task_permitted_caps+0x76>
jmp    11c5 <_get_task_permitted_caps+0x65>
jmp    11ca <_get_task_permitted_caps+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <_get_task_permitted_caps+0x1d4>
jmp    11db <_get_task_permitted_caps+0x7b>
mov    -0x468(%rbp),%edi
lea    -0x460(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,-0x464(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x464(%rbp)
mov    %al,-0x481(%rbp)
jge    122a <_get_task_permitted_caps+0xca>
mov    0x2dc4(%rip),%rax
mov    (%rax),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x481(%rbp)
mov    -0x481(%rbp),%al
test   $0x1,%al
jne    11db <_get_task_permitted_caps+0x7b>
cmpl   $0x0,-0x464(%rbp)
jge    1254 <_get_task_permitted_caps+0xf4>
jmp    124a <_get_task_permitted_caps+0xea>
jmp    124f <_get_task_permitted_caps+0xef>
jmp    1320 <_get_task_permitted_caps+0x1c0>
movslq -0x464(%rbp),%rax
movb   $0x0,-0x460(%rbp,%rax,1)
lea    -0x460(%rbp),%rdi
lea    0xda7(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x478(%rbp)
cmpq   $0x0,-0x478(%rbp)
jne    12ae <_get_task_permitted_caps+0x14e>
jmp    1290 <_get_task_permitted_caps+0x130>
jmp    1295 <_get_task_permitted_caps+0x135>
mov    0x2d2c(%rip),%rax
mov    (%rax),%rcx
mov    0x2d32(%rip),%rax
mov    %rcx,(%rax)
jmp    1320 <_get_task_permitted_caps+0x1c0>
mov    0x2d23(%rip),%rax
movq   $0x0,(%rax)
mov    -0x478(%rbp),%rdi
add    $0x8,%rdi
lea    -0x480(%rbp),%rsi
mov    $0x10,%edx
call   1060 <strtoull@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2cef(%rip),%rax
cmpq   $0x0,(%rax)
jne    130c <_get_task_permitted_caps+0x1ac>
jmp    12f8 <_get_task_permitted_caps+0x198>
jmp    12fd <_get_task_permitted_caps+0x19d>
movl   $0x0,-0x46c(%rbp)
jmp    131b <_get_task_permitted_caps+0x1bb>
jmp    1311 <_get_task_permitted_caps+0x1b1>
jmp    1316 <_get_task_permitted_caps+0x1b6>
jmp    131b <_get_task_permitted_caps+0x1bb>
jmp    1320 <_get_task_permitted_caps+0x1c0>
mov    -0x468(%rbp),%edi
call   1040 <close@plt>
mov    -0x46c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x490,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_task_permitted_caps>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x450,%rsp
mov    %rdx,%r14
mov    %esi,%r8d
mov    %edi,%ecx
lea    0xe82(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     122c <_get_task_permitted_caps+0xcc>
mov    %eax,%r12d
lea    0x50(%rsp),%rbx
mov    0x2e20(%rip),%r15
mov    0x2e11(%rip),%rbp
nop
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11dd <_get_task_permitted_caps+0x7d>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r15)
je     11c0 <_get_task_permitted_caps+0x60>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1240 <_get_task_permitted_caps+0xe0>
mov    %eax,%eax
movb   $0x0,0x50(%rsp,%rax,1)
lea    0xe24(%rip),%rsi
lea    0x50(%rsp),%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     1233 <_get_task_permitted_caps+0xd3>
movq   $0x0,(%r15)
add    $0x8,%rax
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0x10,%edx
call   1060 <strtoull@plt>
mov    %rax,(%r14)
xor    %eax,%eax
cmp    (%r15),%rax
sbb    %ebx,%ebx
jmp    1240 <_get_task_permitted_caps+0xe0>
mov    $0xffffffff,%ebx
jmp    1248 <_get_task_permitted_caps+0xe8>
mov    0x2d8e(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    %r12d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x450,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/proc/%d/task/%d/status\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"CapPrm:\00"
@EINVAL = global i64 0
define i32 @_get_task_permitted_caps(i32 %0, i32 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64*
%8 = alloca [64 x i8]
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64* %2, i64** %7
store i32 -1, i32* %12
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%16 = load i32, i32* %5
%17 = load i32, i32* %6
%18 = call i32 @snprintf(i8* %15, i32 64, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %3
br label %25
25: ; preds = %24
br label %26
26: ; preds = %25
store i32 -1, i32* %4
br label %77
27: ; preds = %3
br label %28
28: ; preds = %39, %27
%29 = load i32, i32* %11
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 1023)
store i32 %31, i32* %10
br label %32
32: ; preds = %28
%33 = load i32, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br label %39
39: ; preds = %35, %32
%40 = phi i1 [ false, %32 ], [ %38, %35 ]
br i1 %40, label %28, label %41
41: ; preds = %39
%42 = load i32, i32* %10
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45
br label %73
47: ; preds = %41
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %49
store i8 0, i8* %50
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%52 = call i8* @strstr(i8* %51, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %52, i8** %13
%53 = load i8*, i8** %13
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %59
55: ; preds = %47
br label %56
56: ; preds = %55
br label %57
57: ; preds = %56
%58 = load i64, i64* @EINVAL
store i64 %58, i64* @errno
br label %73
59: ; preds = %47
store i64 0, i64* @errno
%60 = load i8*, i8** %13
%61 = getelementptr inbounds i8, i8* %60, i64 8
%62 = call i64 @strtoull(i8* %61, i8** %14, i32 16)
%63 = load i64*, i64** %7
store i64 %62, i64* %63
%64 = load i64, i64* @errno
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %59
br label %67
67: ; preds = %66
br label %68
68: ; preds = %67
store i32 0, i32* %12
br label %72
69: ; preds = %59
br label %70
70: ; preds = %69
br label %71
71: ; preds = %70
br label %72
72: ; preds = %71, %68
br label %73
73: ; preds = %72, %57, %46
%74 = load i32, i32* %11
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %12
store i32 %76, i32* %4
br label %77
77: ; preds = %73, %26
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
@.str = [24 x i8] c"/proc/%d/task/%d/status\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"CapPrm:\00"
@EINVAL = global i64 0
define i32 @_get_task_permitted_caps(i32 %0, i32 %1, i64* nocapture writeonly %2) {
%4 = alloca [64 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = bitcast i8** %6 to i8*
%10 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %7, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %38, label %14
14: ; preds = %3, %14
%15 = call i32 @read(i32 %12, i8* %8, i32 1023)
%16 = icmp slt i32 %15, 0
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
%20 = select i1 %16, i1 %19, i1 false
br i1 %20, label %14, label %21
21: ; preds = %14
br i1 %16, label %35, label %22
22: ; preds = %21
%23 = zext i32 %15 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %23
store i8 0, i8* %24
%25 = call i8* @strstr(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i64, i64* @EINVAL
store i64 %28, i64* @errno
br label %35
29: ; preds = %22
store i64 0, i64* @errno
%30 = getelementptr inbounds i8, i8* %25, i64 8
%31 = call i64 @strtoull(i8* %30, i8** %6, i32 16)
store i64 %31, i64* %2
%32 = load i64, i64* @errno
%33 = icmp ne i64 %32, 0
%34 = sext i1 %33 to i32
br label %35
35: ; preds = %29, %21, %27
%36 = phi i32 [ -1, %21 ], [ -1, %27 ], [ %34, %29 ]
%37 = call i32 @close(i32 %12)
br label %38
38: ; preds = %3, %35
%39 = phi i32 [ %36, %35 ], [ -1, %3 ]
ret i32 %39
}