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

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ regexec(int /*<<< orphan*/ *, char *, int /*<<< orphan*/,
                   int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
void do_grep(regex_t *pat, FILE *src) {
  char buf[4096];
  while (fgets(buf, sizeof buf, src)) {
    if (regexec(pat, buf, 0, NULL, 0) == 0) {
      fputs(buf, stdout);
    }
  }
}


x86 assembly (clang, -O0):
<do_grep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11a0 <do_grep+0x70>
mov    -0x8(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
xor    %r8d,%r8d
xor    %eax,%eax
mov    %eax,%ecx
mov    %r8d,%edx
call   1050 <regexec@plt>
cmp    $0x0,%rax
jne    119b <do_grep+0x6b>
lea    -0x1010(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1030 <fputs@plt>
jmp    1143 <do_grep+0x13>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_grep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11aa <do_grep+0x7a>
mov    %rsp,%rbx
mov    0x2e7d(%rip),%r12
jmp    1185 <do_grep+0x55>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11aa <do_grep+0x7a>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <regexec@plt>
test   %rax,%rax
jne    1170 <do_grep+0x40>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1030 <fputs@plt>
jmp    1170 <do_grep+0x40>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdout = global i32 0
define void @do_grep(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [4096 x i8]
store i32* %0, i32** %3
store i32* %1, i32** %4
br label %6
6: ; preds = %20, %2
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = load i32*, i32** %4
%9 = call i64 @fgets(i8* %7, i32 4096, i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %6
%12 = load i32*, i32** %3
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%14 = call i64 @regexec(i32* %12, i8* %13, i32 0, i32* null, i32 0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%18 = load i32, i32* @stdout
%19 = call i32 @fputs(i8* %17, i32 %18)
br label %20
20: ; preds = %16, %11
br label %6
21: ; preds = %6
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
define void @do_grep(i32* %0, i32* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i64 @fgets(i8* %4, i32 4096, i32* %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %2, %13
%8 = call i64 @regexec(i32* %0, i8* %4, i32 0, i32* null, i32 0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @stdout
%12 = call i32 @fputs(i8* %4, i32 %11)
br label %13
13: ; preds = %10, %7
%14 = call i64 @fgets(i8* %4, i32 4096, i32* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %7
16: ; preds = %13, %2
ret void
}