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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ match(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
int grep(char *regexp, FILE *fp, char *name) {
  int n, nmatch = 0;
  char buf[BUFSIZ];
  while (fgets(buf, sizeof(buf), fp) != NULL) {
    n = strlen(buf);
    if (n > 0 && buf[n - 1] == '\n')
      buf[n - 1] = '\0';
    if (match(regexp, buf)) {
      nmatch++;
      if (name != NULL)
        printf("%s: ", name);
      printf("%s\n", buf);
    }
  }
  return nmatch;
}


x86 assembly (clang, -O0):
<grep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1239 <grep+0xf9>
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11e9 <grep+0xa9>
mov    -0x38(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11e9 <grep+0xa9>
mov    -0x38(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rdi
call   1060 <match@plt>
cmp    $0x0,%rax
je     1234 <grep+0xf4>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1224 <grep+0xe4>
mov    -0x18(%rbp),%rsi
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x38(%rbp),%rsi
lea    0xdd6(%rip),%rdi
call   1040 <printf@plt>
jmp    118b <grep+0x4b>
mov    -0x20(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grep>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x2e82(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1206 <grep+0xc6>
mov    %r14,-0x30(%rbp)
xor    %r14d,%r14d
jmp    11c6 <grep+0x86>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14d
lea    0xe5a(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1209 <grep+0xc9>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11df <grep+0x9f>
add    $0xffffffff,%eax
cmpb   $0xa,(%rbx,%rax,1)
jne    11df <grep+0x9f>
movb   $0x0,(%rbx,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <match@plt>
test   %rax,%rax
je     11b3 <grep+0x73>
mov    -0x30(%rbp),%rsi
test   %rsi,%rsi
je     11a0 <grep+0x60>
lea    0xe01(%rip),%rdi
call   1040 <printf@plt>
jmp    11a0 <grep+0x60>
xor    %r14d,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [5 x i8] c"%s: \00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @grep(i8* %0, i32* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
store i32 0, i32* %8
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %9
%14 = alloca i8, i64 %12
store i64 %12, i64* %10
br label %15
15: ; preds = %51, %3
%16 = trunc i64 %12 to i32
%17 = load i32*, i32** %5
%18 = call i32* @fgets(i8* %14, i32 %16, i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %52
20: ; preds = %15
%21 = call i32 @strlen(i8* %14)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %37
24: ; preds = %20
%25 = load i32, i32* %7
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %14, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %37
32: ; preds = %24
%33 = load i32, i32* %7
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %14, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %32, %24, %20
%38 = load i8*, i8** %4
%39 = call i64 @match(i8* %38, i8* %14)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %37
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
%44 = load i8*, i8** %6
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i8*, i8** %6
%48 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %46, %41
%50 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %51
51: ; preds = %49, %37
br label %15
52: ; preds = %15
%53 = load i32, i32* %8
%54 = load i8*, i8** %9
ret i32 %53
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [5 x i8] c"%s: \00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @grep(i8* %0, i32* nocapture %1, i8* %2) {
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32* @fgets(i8* %6, i32 %4, i32* %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %35, label %9
9: ; preds = %3
%10 = icmp eq i8* %2, null
br label %11
11: ; preds = %9, %31
%12 = phi i32 [ 0, %9 ], [ %32, %31 ]
%13 = call i32 @strlen(i8* %6)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = add nsw i32 %13, -1
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %6, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %22
21: ; preds = %15
store i8 0, i8* %18
br label %22
22: ; preds = %21, %15, %11
%23 = call i64 @match(i8* %0, i8* %6)
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = add nsw i32 %12, 1
br i1 %10, label %29, label %27
27: ; preds = %25
%28 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %29
29: ; preds = %27, %25
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %31
31: ; preds = %29, %22
%32 = phi i32 [ %26, %29 ], [ %12, %22 ]
%33 = call i32* @fgets(i8* %6, i32 %4, i32* %1)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %11
35: ; preds = %31, %3
%36 = phi i32 [ 0, %3 ], [ %32, %31 ]
ret i32 %36
}