#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 size_t ssize_t;
typedef int pid_t;

/* Variables and functions */
scalar_t__ ENOENT;
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ debugf(char *, int, ...);
int /*<<< orphan*/ die(int);
scalar_t__ errno;
int openat(int, char *, int, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int sscanf(char const *, char *, char *, unsigned long *, unsigned long *);
char *strrchr(char *, char);
int /*<<< orphan*/ xclose(int);
long get_process_cpu_ticks(pid_t pid) {
  char name[16];
  snprintf(name, sizeof name, "%u/stat", pid);
  int fd = openat(6, name, O_RDONLY | O_CLOEXEC, 0);
  if (fd < 0) {
    if (errno == ENOENT) {
      debugf("executor: pid %d stat file does not exist", pid);
      return -1;
    }
    die(67);
  }
  char buf[512];
  ssize_t len = read(fd, buf, sizeof buf - 1);
  if (len < 0)
    die(68);
  buf[len] = '\0';
  xclose(fd);
  const char *s = strrchr(buf, ')');
  if (s == NULL)
    die(69);
  char state = '\0';
  unsigned long utime = 0;
  unsigned long stime = 0;
  if (sscanf(s, ") %c %*d %*d %*d %*d %*d %*d %*u %*u %*u %*u %lu %lu ", &state,
             &utime, &stime) != 3)
    die(69);
  debugf("executor: pid %d state is %c", pid, state);
  switch (state) {
  case 'Z':
  case 'X':
    return -1;
  }
  return utime + stime;
}


x86 assembly (clang, -O0):
<get_process_cpu_ticks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x10,%esi
lea    0xe5f(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
mov    0x2e06(%rip),%rax
or     (%rax),%edx
mov    $0x6,%edi
xor    %ecx,%ecx
call   1040 <openat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1217 <get_process_cpu_ticks+0x97>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
jne    120d <get_process_cpu_ticks+0x8d>
mov    -0xc(%rbp),%esi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1070 <debugf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1338 <get_process_cpu_ticks+0x1b8>
mov    $0x43,%edi
call   10a0 <die@plt>
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x1ff,%edx
call   1080 <read@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
jae    124a <get_process_cpu_ticks+0xca>
mov    $0x44,%edi
call   10a0 <die@plt>
mov    -0x238(%rbp),%rax
movb   $0x0,-0x230(%rbp,%rax,1)
mov    -0x24(%rbp),%edi
call   1030 <xclose@plt>
lea    -0x230(%rbp),%rdi
mov    $0x29,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x240(%rbp)
cmpq   $0x0,-0x240(%rbp)
jne    1291 <get_process_cpu_ticks+0x111>
mov    $0x45,%edi
call   10a0 <die@plt>
movb   $0x0,-0x241(%rbp)
movq   $0x0,-0x250(%rbp)
movq   $0x0,-0x258(%rbp)
mov    -0x240(%rbp),%rdi
lea    0xd76(%rip),%rsi
lea    -0x241(%rbp),%rdx
lea    -0x250(%rbp),%rcx
lea    -0x258(%rbp),%r8
call   1090 <sscanf@plt>
cmp    $0x3,%eax
je     12e9 <get_process_cpu_ticks+0x169>
mov    $0x45,%edi
call   10a0 <die@plt>
mov    -0xc(%rbp),%esi
movsbl -0x241(%rbp),%edx
lea    0xd6e(%rip),%rdi
xor    %eax,%eax
call   1070 <debugf@plt>
movsbl -0x241(%rbp),%eax
or     $0x2,%eax
sub    $0x5a,%eax
je     1319 <get_process_cpu_ticks+0x199>
jmp    1326 <get_process_cpu_ticks+0x1a6>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1338 <get_process_cpu_ticks+0x1b8>
mov    -0x250(%rbp),%rax
add    -0x258(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_process_cpu_ticks>:
push   %rbp
push   %r14
push   %rbx
sub    $0x230,%rsp
mov    %edi,%r14d
lea    0xe6b(%rip),%rdx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x10,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    0x2e37(%rip),%rax
mov    0x2e08(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x6,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
call   1040 <openat@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    120e <get_process_cpu_ticks+0x8e>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <get_process_cpu_ticks+0x84>
lea    0xe1a(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <debugf@plt>
mov    $0xffffffffffffffff,%rax
jmp    12c5 <get_process_cpu_ticks+0x145>
mov    $0x43,%edi
call   10a0 <die@plt>
lea    0x30(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1ff,%edx
call   1080 <read@plt>
movb   $0x0,0x30(%rsp,%rax,1)
mov    %ebp,%edi
call   1030 <xclose@plt>
mov    %rbx,%rdi
mov    $0x29,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    124d <get_process_cpu_ticks+0xcd>
mov    $0x45,%edi
call   10a0 <die@plt>
movb   $0x0,0xf(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xdc7(%rip),%rsi
lea    0xf(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %rbp,%rdi
call   1090 <sscanf@plt>
cmp    $0x3,%eax
je     1291 <get_process_cpu_ticks+0x111>
mov    $0x45,%edi
call   10a0 <die@plt>
movsbl 0xf(%rsp),%edx
lea    0xdcb(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <debugf@plt>
movsbl 0xf(%rsp),%ecx
mov    $0xffffffffffffffff,%rax
or     $0x2,%ecx
cmp    $0x5a,%ecx
je     12c5 <get_process_cpu_ticks+0x145>
mov    0x10(%rsp),%rax
add    0x18(%rsp),%rax
add    $0x230,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%u/stat\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [42 x i8] c"executor: pid %d stat file does not exist\00"
@.str.2 = [54 x i8] c") %c %*d %*d %*d %*d %*d %*d %*u %*u %*u %*u %lu %lu \00"
@.str.3 = [29 x i8] c"executor: pid %d state is %c\00"
define i64 @get_process_cpu_ticks(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %3
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%13 = load i32, i32* %3
%14 = call i32 @snprintf(i8* %12, i32 16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* @O_RDONLY
%17 = load i32, i32* @O_CLOEXEC
%18 = or i32 %16, %17
%19 = call i32 @openat(i32 6, i8* %15, i32 %18, i32 0)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %1
%23 = load i64, i64* @errno
%24 = load i64, i64* @ENOENT
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* %3
%28 = call i32 (i8*, i32, ...) @debugf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i64 -1, i64* %2
br label %68
29: ; preds = %22
%30 = call i32 @die(i32 67)
br label %31
31: ; preds = %29, %1
%32 = load i32, i32* %5
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%34 = call i64 @read(i32 %32, i8* %33, i32 511)
store i64 %34, i64* %7
%35 = load i64, i64* %7
%36 = icmp ult i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @die(i32 68)
br label %39
39: ; preds = %37, %31
%40 = load i64, i64* %7
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* %5
%43 = call i32 @xclose(i32 %42)
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%45 = call i8* @strrchr(i8* %44, i8 signext 41)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = call i32 @die(i32 69)
br label %50
50: ; preds = %48, %39
store i8 0, i8* %9
store i64 0, i64* %10
store i64 0, i64* %11
%51 = load i8*, i8** %8
%52 = call i32 @sscanf(i8* %51, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i8* %9, i64* %10, i64* %11)
%53 = icmp ne i32 %52, 3
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @die(i32 69)
br label %56
56: ; preds = %54, %50
%57 = load i32, i32* %3
%58 = load i8, i8* %9
%59 = sext i8 %58 to i32
%60 = call i32 (i8*, i32, ...) @debugf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %57, i32 %59)
%61 = load i8, i8* %9
%62 = sext i8 %61 to i32
switch i32 %62, label %64 [
i32 90, label %63
i32 88, label %63
]
63: ; preds = %56, %56
store i64 -1, i64* %2
br label %68
64: ; preds = %56
%65 = load i64, i64* %10
%66 = load i64, i64* %11
%67 = add i64 %65, %66
store i64 %67, i64* %2
br label %68
68: ; preds = %64, %63, %26
%69 = load i64, i64* %2
ret i64 %69
}

LLVM IR (-O2):
@.str = [8 x i8] c"%u/stat\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [42 x i8] c"executor: pid %d stat file does not exist\00"
@.str.2 = [54 x i8] c") %c %*d %*d %*d %*d %*d %*d %*u %*u %*u %*u %lu %lu \00"
@.str.3 = [29 x i8] c"executor: pid %d state is %c\00"
define i64 @get_process_cpu_ticks(i32 %0) {
%2 = alloca [16 x i8]
%3 = alloca [512 x i8]
%4 = alloca i8
%5 = alloca i64
%6 = alloca i64
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_CLOEXEC
%11 = or i32 %10, %9
%12 = call i32 @openat(i32 6, i8* %7, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %1
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (i8*, i32, ...) @debugf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %50
20: ; preds = %14
%21 = call i32 @die(i32 67)
br label %22
22: ; preds = %20, %1
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%24 = call i64 @read(i32 %12, i8* %23, i32 511)
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 @xclose(i32 %12)
%27 = call i8* @strrchr(i8* %23, i8 signext 41)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @die(i32 69)
br label %31
31: ; preds = %29, %22
store i8 0, i8* %4
%32 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%33 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%34 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i8* %4, i64* %5, i64* %6)
%35 = icmp eq i32 %34, 3
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = call i32 @die(i32 69)
br label %38
38: ; preds = %36, %31
%39 = load i8, i8* %4
%40 = sext i8 %39 to i32
%41 = call i32 (i8*, i32, ...) @debugf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %40)
%42 = load i8, i8* %4
%43 = sext i8 %42 to i32
switch i32 %43, label %44 [
i32 90, label %48
i32 88, label %48
]
44: ; preds = %38
%45 = load i64, i64* %5
%46 = load i64, i64* %6
%47 = add i64 %46, %45
br label %48
48: ; preds = %38, %38, %44
%49 = phi i64 [ %47, %44 ], [ -1, %38 ], [ -1, %38 ]
br label %50
50: ; preds = %48, %18
%51 = phi i64 [ -1, %18 ], [ %49, %48 ]
ret i64 %51
}