#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  size_t rm_eo;
  size_t rm_so;
};
typedef TYPE_1__ regmatch_t;
typedef int /*<<< orphan*/ regex_t;
typedef int /*<<< orphan*/ pmatch;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int REG_EXTENDED;
int REG_NEWLINE;
int REG_NOMATCH;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, int);
int regcomp(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ regerror(int, int /*<<< orphan*/ *, char *, int);
int regexec(int /*<<< orphan*/ *, char *, int, TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main() {
  regex_t regex;
  int reti = regcomp(&regex, "^PPid:\t([0-9]+)$", REG_EXTENDED | REG_NEWLINE);
  regmatch_t pmatch[2];
  if (reti) {
    fprintf(stderr, "Could not compile regex\n");
    return 1;
  }
  char buff[255];
  FILE *stat_file = fopen("/proc/self/status", "r");
  int pid = -1;
  while (NULL != fgets(buff, sizeof(buff), stat_file)) {
    reti = regexec(&regex, buff, sizeof(pmatch), pmatch, 0);
    if (0 == reti) {
      buff[pmatch[1].rm_eo] = 0;
      pid = atoi(&buff[pmatch[1].rm_so]);
      break;
    } else if (reti == REG_NOMATCH) {
      continue;
    } else {
      regerror(reti, &regex, buff, sizeof(buff));
      fprintf(stderr, "Regex match failed: %s\n", buff);
      fclose(stat_file);
      regfree(&regex);
      return 1;
    }
  }
  printf("%d\n", pid);
  fclose(stat_file);
  regfree(&regex);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
lea    -0x8(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1060 <regcomp@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1204 <main+0x64>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133b <main+0x19b>
lea    0xe1f(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x138(%rbp)
movl   $0xffffffff,-0x13c(%rbp)
ff ff ff
lea    -0x130(%rbp),%rdi
mov    -0x138(%rbp),%rdx
mov    $0xff,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     130d <main+0x16d>
lea    -0x130(%rbp),%rsi
lea    -0x30(%rbp),%rcx
lea    -0x8(%rbp),%rdi
mov    $0x20,%edx
xor    %r8d,%r8d
call   10a0 <regexec@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
jne    129f <main+0xff>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
lea    -0x130(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x13c(%rbp)
jmp    130d <main+0x16d>
mov    -0xc(%rbp),%eax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%eax
jne    12b6 <main+0x116>
jmp    1228 <main+0x88>
mov    -0xc(%rbp),%edi
lea    -0x130(%rbp),%rdx
lea    -0x8(%rbp),%rsi
mov    $0xff,%ecx
call   1090 <regerror@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    -0x130(%rbp),%rdx
lea    0xd59(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x138(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x8(%rbp),%rdi
call   1040 <regfree@plt>
movl   $0x1,-0x4(%rbp)
jmp    133b <main+0x19b>
mov    -0x13c(%rbp),%esi
lea    0xd3c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x138(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x8(%rbp),%rdi
call   1040 <regfree@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x130,%rsp
mov    0x2e11(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe37(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1060 <regcomp@plt>
test   %eax,%eax
je     11f8 <main+0x58>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebx
jmp    12df <main+0x13f>
lea    0xe2b(%rip),%rdi
lea    0xe36(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
lea    0x30(%rsp),%rbx
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
mov    0x2dbc(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     129f <main+0xff>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x20,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   10a0 <regexec@plt>
test   %eax,%eax
je     12a6 <main+0x106>
cmp    0x0(%r13),%eax
je     1230 <main+0x90>
lea    0xc(%rsp),%rsi
lea    0x30(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rdx
mov    $0xff,%ecx
call   1090 <regerror@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebx
jmp    12cd <main+0x12d>
mov    $0xffffffff,%esi
jmp    12bf <main+0x11f>
mov    0x20(%rsp),%rax
movb   $0x0,0x30(%rsp,%rax,1)
add    0x28(%rsp),%rbx
mov    %rbx,%rdi
call   10c0 <atoi@plt>
mov    %eax,%esi
lea    0xd90(%rip),%rdi
call   1050 <printf@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
lea    0xc(%rsp),%rdi
call   1040 <regfree@plt>
mov    %ebx,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i64 }
@.str = [17 x i8] c"^PPid:\09([0-9]+)$\00"
@REG_EXTENDED = global i32 0
@REG_NEWLINE = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not compile regex\0A\00"
@.str.2 = [18 x i8] c"/proc/self/status\00"
@.str.3 = [2 x i8] c"r\00"
@REG_NOMATCH = global i32 0
@.str.4 = [24 x i8] c"Regex match failed: %s\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x %struct.TYPE_3__]
%5 = alloca [255 x i8]
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @REG_EXTENDED
%9 = load i32, i32* @REG_NEWLINE
%10 = or i32 %8, %9
%11 = call i32 @regcomp(i32* %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %61
17: ; preds = %0
%18 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %18, i32** %6
store i32 -1, i32* %7
br label %19
19: ; preds = %44, %17
%20 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%21 = load i32*, i32** %6
%22 = call i32* @fgets(i8* %20, i32 255, i32* %21)
%23 = icmp ne i32* null, %22
br i1 %23, label %24, label %55
24: ; preds = %19
%25 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%26 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %4, i64 0, i64 0
%27 = call i32 @regexec(i32* %2, i8* %25, i32 32, %struct.TYPE_3__* %26, i32 0)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp eq i32 0, %28
br i1 %29, label %30, label %40
30: ; preds = %24
%31 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %4, i64 0, i64 1
%32 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %4, i64 0, i64 1
%36 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %35, i32 0, i32 1
%37 = load i64, i64* %36
%38 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 %37
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %7
br label %55
40: ; preds = %24
%41 = load i32, i32* %3
%42 = load i32, i32* @REG_NOMATCH
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
br label %19
45: ; preds = %40
%46 = load i32, i32* %3
%47 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%48 = call i32 @regerror(i32 %46, i32* %2, i8* %47, i32 255)
%49 = load i32, i32* @stderr
%50 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %50)
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = call i32 @regfree(i32* %2)
store i32 1, i32* %1
br label %61
55: ; preds = %30, %19
%56 = load i32, i32* %7
%57 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %56)
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
%60 = call i32 @regfree(i32* %2)
store i32 0, i32* %1
br label %61
61: ; preds = %55, %45, %14
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i64 }
@.str = [17 x i8] c"^PPid:\09([0-9]+)$\00"
@REG_EXTENDED = global i32 0
@REG_NEWLINE = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not compile regex\0A\00"
@.str.2 = [18 x i8] c"/proc/self/status\00"
@.str.3 = [2 x i8] c"r\00"
@REG_NOMATCH = global i32 0
@.str.4 = [24 x i8] c"Regex match failed: %s\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x %struct.TYPE_3__]
%3 = alloca [255 x i8]
%4 = bitcast i32* %1 to i8*
%5 = load i32, i32* @REG_EXTENDED
%6 = load i32, i32* @REG_NEWLINE
%7 = or i32 %6, %5
%8 = call i32 @regcomp(i32* %1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = bitcast [2 x %struct.TYPE_3__]* %2 to i8*
%10 = icmp eq i32 %8, 0
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %46
14: ; preds = %0
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%16 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %2, i64 0, i64 0
br label %18
18: ; preds = %32, %14
%19 = call i32* @fgets(i8* %15, i32 255, i32* %16)
%20 = icmp eq i32* %19, null
br i1 %20, label %39, label %21
21: ; preds = %18
%22 = call i32 @regexec(i32* %1, i8* %15, i32 32, %struct.TYPE_3__* %17, i32 0)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %2, i64 0, i64 1, i32 0
%26 = load i64, i64* %25
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %2, i64 0, i64 1, i32 1
%29 = load i64, i64* %28
%30 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 %29
%31 = call i32 @atoi(i8* %30)
br label %39
32: ; preds = %21
%33 = load i32, i32* @REG_NOMATCH
%34 = icmp eq i32 %22, %33
br i1 %34, label %18, label %35
35: ; preds = %32
%36 = call i32 @regerror(i32 %22, i32* %1, i8* %15, i32 255)
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %15)
br label %42
39: ; preds = %18, %24
%40 = phi i32 [ %31, %24 ], [ -1, %18 ]
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %39, %35
%43 = phi i32 [ 0, %39 ], [ 1, %35 ]
%44 = call i32 @fclose(i32* %16)
%45 = call i32 @regfree(i32* %1)
br label %46
46: ; preds = %42, %11
%47 = phi i32 [ 1, %11 ], [ %43, %42 ]
ret i32 %47
}