#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 */

/* Variables and functions */
scalar_t__ getline(char *, int);
int /*<<< orphan*/ printf(char *, int, char *);
int strrindex(char *, char *);
main() {
  extern char pattern[];
  char line[1000];
  int found = 0;
  int position;
  while (getline(line, 1000) > 0)
    if ((position = strrindex(line, pattern)) >= 0) {
      printf("%d, %s", position, line);
      found++;
    }
  return found;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x3f4(%rbp)
lea    -0x3f0(%rbp),%rdi
mov    $0x3e8,%esi
call   1050 <getline@plt>
cmp    $0x0,%rax
jle    11b6 <main+0x86>
lea    -0x3f0(%rbp),%rdi
mov    0x2e73(%rip),%rsi
call   1040 <strrindex@plt>
mov    %eax,-0x3f8(%rbp)
cmp    $0x0,%eax
jl     11b1 <main+0x81>
mov    -0x3f8(%rbp),%esi
lea    -0x3f0(%rbp),%rdx
lea    0xe63(%rip),%rdi
call   1030 <printf@plt>
mov    -0x3f4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3f4(%rbp)
jmp    114c <main+0x1c>
mov    -0x3f4(%rbp),%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3e8,%rsp
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1050 <getline@plt>
xor    %ebp,%ebp
test   %rax,%rax
jle    11a3 <main+0x73>
mov    0x2e90(%rip),%r14
mov    %rsp,%rbx
lea    0xe9e(%rip),%r15
jmp    1182 <main+0x52>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   1050 <getline@plt>
test   %rax,%rax
jle    11a3 <main+0x73>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strrindex@plt>
test   %eax,%eax
js     1170 <main+0x40>
mov    %r15,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1030 <printf@plt>
add    $0x1,%ebp
jmp    1170 <main+0x40>
mov    %ebp,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@pattern = external global [0 x i8]
@.str = [7 x i8] c"%d, %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
br label %5
5: ; preds = %19, %0
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%7 = call i64 @getline(i8* %6, i32 1000)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %5
%10 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%11 = call i32 @strrindex(i8* %10, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @pattern, i64 0, i64 0))
store i32 %11, i32* %4
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i32, i32* %4
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%16 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %14, i8* %15)
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %19
19: ; preds = %13, %9
br label %5
20: ; preds = %5
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
@pattern = external global [0 x i8]
@.str = [7 x i8] c"%d, %s\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%3 = call i64 @getline(i8* %2, i32 1000)
%4 = icmp sgt i64 %3, 0
br i1 %4, label %5, label %16
5: ; preds = %0, %12
%6 = phi i32 [ %13, %12 ], [ 0, %0 ]
%7 = call i32 @strrindex(i8* %2, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @pattern, i64 0, i64 0))
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %2)
%11 = add nsw i32 %6, 1
br label %12
12: ; preds = %9, %5
%13 = phi i32 [ %11, %9 ], [ %6, %5 ]
%14 = call i64 @getline(i8* %2, i32 1000)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %5, label %16
16: ; preds = %12, %0
%17 = phi i32 [ 0, %0 ], [ %13, %12 ]
ret i32 %17
}