#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*/ 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 *, size_t, 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);
  size_t number_of_pmatches = 2;
  regmatch_t pmatch[number_of_pmatches];
  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, number_of_pmatches, 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    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
or     %eax,%edx
lea    0xe33(%rip),%rsi
lea    -0x8(%rbp),%rdi
call   1060 <regcomp@plt>
mov    %eax,-0xc(%rbp)
movq   $0x2,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
shl    $0x4,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x148(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     123b <main+0x9b>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1389 <main+0x1e9>
lea    0xde8(%rip),%rdi
lea    0xdf3(%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     1354 <main+0x1b4>
mov    -0x148(%rbp),%rcx
lea    -0x130(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x8(%rbp),%rdi
xor    %r8d,%r8d
call   10a0 <regexec@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
jne    12df <main+0x13f>
mov    -0x148(%rbp),%rax
mov    0x10(%rax),%rcx
movb   $0x0,-0x130(%rbp,%rcx,1)
lea    -0x130(%rbp),%rdi
add    0x18(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x13c(%rbp)
jmp    1354 <main+0x1b4>
mov    -0xc(%rbp),%eax
mov    0x2cf7(%rip),%rcx
cmp    (%rcx),%eax
jne    12f6 <main+0x156>
jmp    125f <main+0xbf>
mov    -0xc(%rbp),%edi
lea    -0x130(%rbp),%rdx
lea    -0x8(%rbp),%rsi
mov    $0xff,%ecx
call   1090 <regerror@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
lea    -0x130(%rbp),%rdx
lea    0xd19(%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)
movl   $0x1,-0x2c(%rbp)
jmp    1389 <main+0x1e9>
mov    -0x13c(%rbp),%esi
lea    0xcf5(%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)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    0x2e10(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe36(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1060 <regcomp@plt>
mov    %rsp,%r13
test   %eax,%eax
je     11fc <main+0x5c>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebx
jmp    12de <main+0x13e>
lea    0xe27(%rip),%rdi
lea    0xe32(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
lea    0x30(%rsp),%rbx
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
mov    0x2db8(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     129e <main+0xfe>
mov    $0x2,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12,%rcx
xor    %r8d,%r8d
call   10a0 <regexec@plt>
test   %eax,%eax
je     12a5 <main+0x105>
cmp    0x0(%rbp),%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    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebx
jmp    12cc <main+0x12c>
mov    $0xffffffff,%esi
jmp    12be <main+0x11e>
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    0xd91(%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    %r13,%rsp
mov    %ebx,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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 i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca [255 x i8]
%9 = alloca i32*
%10 = alloca i32
store i32 0, i32* %1
%11 = load i32, i32* @REG_EXTENDED
%12 = load i32, i32* @REG_NEWLINE
%13 = or i32 %11, %12
%14 = call i32 @regcomp(i32* %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %3
store i64 2, i64* %4
%15 = load i64, i64* %4
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %5
%17 = alloca %struct.TYPE_3__, i64 %15
store i64 %15, i64* %6
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %0
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
store i32 1, i32* %7
br label %67
23: ; preds = %0
%24 = 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* %24, i32** %9
store i32 -1, i32* %10
br label %25
25: ; preds = %50, %23
%26 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%27 = load i32*, i32** %9
%28 = call i32* @fgets(i8* %26, i32 255, i32* %27)
%29 = icmp ne i32* null, %28
br i1 %29, label %30, label %61
30: ; preds = %25
%31 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%32 = load i64, i64* %4
%33 = call i32 @regexec(i32* %2, i8* %31, i64 %32, %struct.TYPE_3__* %17, i32 0)
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = icmp eq i32 0, %34
br i1 %35, label %36, label %46
36: ; preds = %30
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 1
%38 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 1
%42 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %41, i32 0, i32 1
%43 = load i64, i64* %42
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 %43
%45 = call i32 @atoi(i8* %44)
store i32 %45, i32* %10
br label %61
46: ; preds = %30
%47 = load i32, i32* %3
%48 = load i32, i32* @REG_NOMATCH
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
br label %25
51: ; preds = %46
%52 = load i32, i32* %3
%53 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%54 = call i32 @regerror(i32 %52, i32* %2, i8* %53, i32 255)
%55 = load i32, i32* @stderr
%56 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %56)
%58 = load i32*, i32** %9
%59 = call i32 @fclose(i32* %58)
%60 = call i32 @regfree(i32* %2)
store i32 1, i32* %1
store i32 1, i32* %7
br label %67
61: ; preds = %36, %25
%62 = load i32, i32* %10
%63 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %62)
%64 = load i32*, i32** %9
%65 = call i32 @fclose(i32* %64)
%66 = call i32 @regfree(i32* %2)
store i32 0, i32* %1
store i32 1, i32* %7
br label %67
67: ; preds = %61, %51, %20
%68 = load i8*, i8** %5
%69 = load i32, i32* %1
ret i32 %69
}

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 [255 x i8]
%3 = bitcast i32* %1 to i8*
%4 = load i32, i32* @REG_EXTENDED
%5 = load i32, i32* @REG_NEWLINE
%6 = or i32 %5, %4
%7 = call i32 @regcomp(i32* %1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i8* @llvm.stacksave()
%9 = alloca [2 x %struct.TYPE_3__]
%10 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %9, i64 0, i64 0
%11 = icmp eq i32 %7, 0
br i1 %11, label %15, label %12
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %46
15: ; preds = %0
%16 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%17 = 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))
br label %18
18: ; preds = %32, %15
%19 = call i32* @fgets(i8* %16, i32 255, i32* %17)
%20 = icmp eq i32* %19, null
br i1 %20, label %39, label %21
21: ; preds = %18
%22 = call i32 @regexec(i32* %1, i8* %16, i64 2, %struct.TYPE_3__* %10, 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__]* %9, i64 0, i64 1, i32 0
%26 = load i64, i64* %25
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %9, i64 0, i64 1, i32 1
%29 = load i64, i64* %28
%30 = getelementptr inbounds [255 x i8], [255 x i8]* %2, 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* %16, 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* %16)
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* %17)
%45 = call i32 @regfree(i32* %1)
br label %46
46: ; preds = %42, %12
%47 = phi i32 [ 1, %12 ], [ %43, %42 ]
ret i32 %47
}