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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ REG_EXTENDED;
int /*<<< orphan*/ SIGCONT;
int /*<<< orphan*/ SIGSTOP;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ regcomp(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ regexec(int /*<<< orphan*/ *, char *, int,
                           int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int autostop(char *command, char *regexp, int passes) {
  char *pids = "echo $$ && ";
  int commandLen = strlen(command);
  int pidsLen = strlen(pids);
  char pidAndcommand[commandLen + pidsLen];
  strcpy(pidAndcommand, pids);
  strcpy(pidAndcommand + pidsLen, command);
  FILE *output = popen(pidAndcommand, "r");
  if (output == NULL)
    return 1;
  setvbuf(output, NULL, _IONBF, 0);
  char lineBuffer[BUFSIZ];
  int result;
  int pid;
  fgets(lineBuffer, BUFSIZ, output);
  result = sscanf(lineBuffer, "%d\n", &pid);
  regex_t regex;
  regmatch_t regmatch;
  if (!regcomp(&regex, regexp, REG_EXTENDED)) {
    while (fgets(lineBuffer, BUFSIZ, output) != NULL) {
      printf("%s", lineBuffer);
      if (!regexec(&regex, lineBuffer, 1, &regmatch, 0)) {
        if (passes <= 0) {
          kill(pid, SIGSTOP);
          fgetc(stdin);
          kill(pid, SIGCONT);
        }
        passes--;
      }
    }
    regfree(&regex);
  }
  return pclose(output);
}


x86 assembly (clang, -O0):
<autostop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    0xe13(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x30(%rbp),%ecx
add    %ecx,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x70(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x70(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   10f0 <popen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1283 <autostop+0xb3>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    13c1 <autostop+0x1f1>
mov    -0x48(%rbp),%rdi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   10e0 <setvbuf@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x78(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x58(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
mov    -0x48(%rbp),%rdx
call   10a0 <fgets@plt>
mov    -0x78(%rbp),%rdi
lea    0xd33(%rip),%rsi
lea    -0x60(%rbp),%rdx
call   10c0 <sscanf@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2cce(%rip),%rax
mov    (%rax),%edx
lea    -0x64(%rbp),%rdi
call   1090 <regcomp@plt>
cmp    $0x0,%eax
jne    13ae <autostop+0x1de>
jmp    130b <autostop+0x13b>
mov    -0x78(%rbp),%rdi
mov    0x2cb2(%rip),%rax
mov    (%rax),%esi
mov    -0x48(%rbp),%rdx
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     13a5 <autostop+0x1d5>
mov    -0x78(%rbp),%rsi
lea    0xcdc(%rip),%rdi
call   1060 <printf@plt>
mov    -0x78(%rbp),%rsi
lea    -0x64(%rbp),%rdi
mov    $0x1,%edx
lea    -0x68(%rbp),%rcx
xor    %r8d,%r8d
call   10d0 <regexec@plt>
cmp    $0x0,%eax
jne    13a0 <autostop+0x1d0>
cmpl   $0x0,-0x1c(%rbp)
jg     1397 <autostop+0x1c7>
mov    -0x60(%rbp),%edi
mov    0x2c47(%rip),%rax
mov    (%rax),%esi
call   10b0 <kill@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
call   1080 <fgetc@plt>
mov    -0x60(%rbp),%edi
mov    0x2c58(%rip),%rax
mov    (%rax),%esi
call   10b0 <kill@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    130b <autostop+0x13b>
lea    -0x64(%rbp),%rdi
call   1040 <regfree@plt>
mov    -0x48(%rbp),%rdi
call   1070 <pclose@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<autostop>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,-0x2c(%rbp)
mov    %rsi,-0x40(%rbp)
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0xe07(%rip),%r13
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
add    %eax,%ebx
add    $0xf,%rbx
and    $0xfffffffffffffff0,%rbx
mov    %rsp,%r14
sub    %rbx,%r14
mov    %r14,%rsp
mov    %r14,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
movslq %r12d,%rdi
add    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    0xdd5(%rip),%rsi
mov    %r14,%rdi
call   10f0 <popen@plt>
test   %rax,%rax
je     135e <autostop+0x18e>
mov    %rax,%r15
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
xor    %ecx,%ecx
call   10e0 <setvbuf@plt>
mov    0x2d61(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r15,%rdx
call   10a0 <fgets@plt>
lea    0xd7e(%rip),%rsi
lea    -0x34(%rbp),%rdx
mov    %rbx,%rdi
call   10c0 <sscanf@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1090 <regcomp@plt>
test   %eax,%eax
jne    136e <autostop+0x19e>
mov    (%r14),%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1365 <autostop+0x195>
lea    -0x30(%rbp),%r13
lea    -0x44(%rbp),%r12
jmp    12f9 <autostop+0x129>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
mov    (%r14),%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1365 <autostop+0x195>
lea    0xd12(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   10d0 <regexec@plt>
test   %eax,%eax
jne    12e6 <autostop+0x116>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jg     12e0 <autostop+0x110>
mov    -0x34(%rbp),%edi
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
call   10b0 <kill@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
call   1080 <fgetc@plt>
mov    -0x34(%rbp),%edi
mov    0x2c96(%rip),%rax
mov    (%rax),%esi
call   10b0 <kill@plt>
mov    -0x2c(%rbp),%eax
jmp    12e0 <autostop+0x110>
mov    $0x1,%eax
jmp    1376 <autostop+0x1a6>
lea    -0x30(%rbp),%rdi
call   1040 <regfree@plt>
mov    %r15,%rdi
call   1070 <pclose@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"echo $$ && \00"
@.str.1 = [2 x i8] c"r\00"
@_IONBF = global i32 0
@BUFSIZ = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@REG_EXTENDED = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@SIGSTOP = global i32 0
@stdin = global i32 0
@SIGCONT = global i32 0
define i32 @autostop(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i64
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %8
%20 = load i8*, i8** %5
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %9
%22 = load i8*, i8** %8
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %10
%24 = load i32, i32* %9
%25 = load i32, i32* %10
%26 = add nsw i32 %24, %25
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
store i8* %28, i8** %11
%29 = alloca i8, i64 %27
store i64 %27, i64* %12
%30 = load i8*, i8** %8
%31 = call i32 @strcpy(i8* %29, i8* %30)
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %29, i64 %33
%35 = load i8*, i8** %5
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = call i32* @popen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %37, i32** %13
%38 = load i32*, i32** %13
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %41
40: ; preds = %3
store i32 1, i32* %4
store i32 1, i32* %14
br label %87
41: ; preds = %3
%42 = load i32*, i32** %13
%43 = load i32, i32* @_IONBF
%44 = call i32 @setvbuf(i32* %42, i32* null, i32 %43, i32 0)
%45 = load i32, i32* @BUFSIZ
%46 = zext i32 %45 to i64
%47 = alloca i8, i64 %46
store i64 %46, i64* %15
%48 = load i32, i32* @BUFSIZ
%49 = load i32*, i32** %13
%50 = call i32* @fgets(i8* %47, i32 %48, i32* %49)
%51 = call i32 @sscanf(i8* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %17)
store i32 %51, i32* %16
%52 = load i8*, i8** %6
%53 = load i32, i32* @REG_EXTENDED
%54 = call i32 @regcomp(i32* %18, i8* %52, i32 %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %84, label %56
56: ; preds = %41
br label %57
57: ; preds = %81, %56
%58 = load i32, i32* @BUFSIZ
%59 = load i32*, i32** %13
%60 = call i32* @fgets(i8* %47, i32 %58, i32* %59)
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %82
62: ; preds = %57
%63 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%64 = call i32 @regexec(i32* %18, i8* %47, i32 1, i32* %19, i32 0)
%65 = icmp ne i32 %64, 0
br i1 %65, label %81, label %66
66: ; preds = %62
%67 = load i32, i32* %7
%68 = icmp sle i32 %67, 0
br i1 %68, label %69, label %78
69: ; preds = %66
%70 = load i32, i32* %17
%71 = load i32, i32* @SIGSTOP
%72 = call i32 @kill(i32 %70, i32 %71)
%73 = load i32, i32* @stdin
%74 = call i32 @fgetc(i32 %73)
%75 = load i32, i32* %17
%76 = load i32, i32* @SIGCONT
%77 = call i32 @kill(i32 %75, i32 %76)
br label %78
78: ; preds = %69, %66
%79 = load i32, i32* %7
%80 = add nsw i32 %79, -1
store i32 %80, i32* %7
br label %81
81: ; preds = %78, %62
br label %57
82: ; preds = %57
%83 = call i32 @regfree(i32* %18)
br label %84
84: ; preds = %82, %41
%85 = load i32*, i32** %13
%86 = call i32 @pclose(i32* %85)
store i32 %86, i32* %4
store i32 1, i32* %14
br label %87
87: ; preds = %84, %40
%88 = load i8*, i8** %11
%89 = load i32, i32* %4
ret i32 %89
}

LLVM IR (-O2):
@.str = [12 x i8] c"echo $$ && \00"
@.str.1 = [2 x i8] c"r\00"
@_IONBF = global i32 0
@BUFSIZ = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@REG_EXTENDED = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@SIGSTOP = global i32 0
@stdin = global i32 0
@SIGCONT = global i32 0
define i32 @autostop(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = add nsw i32 %8, %7
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = sext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = call i32 @strcpy(i8* %14, i8* %0)
%16 = call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %63, label %18
18: ; preds = %3
%19 = load i32, i32* @_IONBF
%20 = call i32 @setvbuf(i32* %16, i32* null, i32 %19, i32 0)
%21 = load i32, i32* @BUFSIZ
%22 = zext i32 %21 to i64
%23 = alloca i8, i64 %22
%24 = bitcast i32* %4 to i8*
%25 = call i32* @fgets(i8* %23, i32 %21, i32* %16)
%26 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%27 = bitcast i32* %5 to i8*
%28 = bitcast i32* %6 to i8*
%29 = load i32, i32* @REG_EXTENDED
%30 = call i32 @regcomp(i32* %5, i8* %1, i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %61
32: ; preds = %18
%33 = load i32, i32* @BUFSIZ
%34 = call i32* @fgets(i8* %23, i32 %33, i32* %16)
%35 = icmp eq i32* %34, null
br i1 %35, label %59, label %36
36: ; preds = %32, %54
%37 = phi i32 [ %55, %54 ], [ %2, %32 ]
%38 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%39 = call i32 @regexec(i32* %5, i8* %23, i32 1, i32* %6, i32 0)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %54
41: ; preds = %36
%42 = icmp slt i32 %37, 1
br i1 %42, label %43, label %52
43: ; preds = %41
%44 = load i32, i32* %4
%45 = load i32, i32* @SIGSTOP
%46 = call i32 @kill(i32 %44, i32 %45)
%47 = load i32, i32* @stdin
%48 = call i32 @fgetc(i32 %47)
%49 = load i32, i32* %4
%50 = load i32, i32* @SIGCONT
%51 = call i32 @kill(i32 %49, i32 %50)
br label %52
52: ; preds = %43, %41
%53 = add nsw i32 %37, -1
br label %54
54: ; preds = %52, %36
%55 = phi i32 [ %37, %36 ], [ %53, %52 ]
%56 = load i32, i32* @BUFSIZ
%57 = call i32* @fgets(i8* %23, i32 %56, i32* %16)
%58 = icmp eq i32* %57, null
br i1 %58, label %59, label %36
59: ; preds = %54, %32
%60 = call i32 @regfree(i32* %5)
br label %61
61: ; preds = %59, %18
%62 = call i32 @pclose(i32* %16)
br label %63
63: ; preds = %3, %61
%64 = phi i32 [ %62, %61 ], [ 1, %3 ]
ret i32 %64
}