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

/* Variables and functions */
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long const, int);
long ftell(int /*<<< orphan*/ *);
int find_last_in_filestream(FILE *file_stream, const char find_this) {
  const long int origin = ftell(file_stream);
  char current_character[1];
  int offset = 0;
  do {
    offset--;
    fseek(file_stream, -1, 1);
    fread(current_character, 1, 1, file_stream);
    fseek(file_stream, -1, 1);
  } while (*current_character != find_this && ftell(file_stream) > 0);
  fseek(file_stream, origin, 0);
  return offset;
}


x86 assembly (clang, -O0):
<find_last_in_filestream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0xffffffffffffffff,%rsi
mov    $0x1,%edx
call   1050 <fseek@plt>
lea    -0x19(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x8(%rbp),%rdi
mov    $0xffffffffffffffff,%rsi
mov    $0x1,%edx
call   1050 <fseek@plt>
movsbl -0x19(%rbp),%ecx
movsbl -0x9(%rbp),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x21(%rbp)
je     11c5 <find_last_in_filestream+0x95>
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
cmp    $0x0,%rax
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1156 <find_last_in_filestream+0x26>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_last_in_filestream>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%rbx
call   1040 <ftell@plt>
mov    %rax,%r14
mov    $0xffffffff,%ecx
lea    0xf(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ecx,%ebp
mov    %rbx,%rdi
mov    $0xffffffffffffffff,%rsi
mov    $0x1,%edx
call   1050 <fseek@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
mov    $0xffffffffffffffff,%rsi
mov    $0x1,%edx
call   1050 <fseek@plt>
cmp    %r15b,0xf(%rsp)
je     11b6 <find_last_in_filestream+0x86>
mov    %rbx,%rdi
call   1040 <ftell@plt>
lea    -0x1(%rbp),%ecx
test   %rax,%rax
jg     1160 <find_last_in_filestream+0x30>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <fseek@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @find_last_in_filestream(i32* %0, i8 signext %1) {
%3 = alloca i32*
%4 = alloca i8
%5 = alloca i64
%6 = alloca [1 x i8]
%7 = alloca i32
store i32* %0, i32** %3
store i8 %1, i8* %4
%8 = load i32*, i32** %3
%9 = call i64 @ftell(i32* %8)
store i64 %9, i64* %5
store i32 0, i32* %7
br label %10
10: ; preds = %31, %2
%11 = load i32, i32* %7
%12 = add nsw i32 %11, -1
store i32 %12, i32* %7
%13 = load i32*, i32** %3
%14 = call i32 @fseek(i32* %13, i64 -1, i32 1)
%15 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%16 = load i32*, i32** %3
%17 = call i32 @fread(i8* %15, i32 1, i32 1, i32* %16)
%18 = load i32*, i32** %3
%19 = call i32 @fseek(i32* %18, i64 -1, i32 1)
br label %20
20: ; preds = %10
%21 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = load i8, i8* %4
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %23, %25
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = load i32*, i32** %3
%29 = call i64 @ftell(i32* %28)
%30 = icmp sgt i64 %29, 0
br label %31
31: ; preds = %27, %20
%32 = phi i1 [ false, %20 ], [ %30, %27 ]
br i1 %32, label %10, label %33
33: ; preds = %31
%34 = load i32*, i32** %3
%35 = load i64, i64* %5
%36 = call i32 @fseek(i32* %34, i64 %35, i32 0)
%37 = load i32, i32* %7
ret i32 %37
}

LLVM IR (-O2):
define i32 @find_last_in_filestream(i32* nocapture %0, i8 signext %1) {
%3 = alloca [1 x i8]
%4 = tail call i64 @ftell(i32* %0)
%5 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
br label %6
6: ; preds = %14, %2
%7 = phi i32 [ 0, %2 ], [ %8, %14 ]
%8 = add nsw i32 %7, -1
%9 = tail call i32 @fseek(i32* %0, i64 -1, i32 1)
%10 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %0)
%11 = tail call i32 @fseek(i32* %0, i64 -1, i32 1)
%12 = load i8, i8* %5
%13 = icmp eq i8 %12, %1
br i1 %13, label %17, label %14
14: ; preds = %6
%15 = tail call i64 @ftell(i32* %0)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %6, label %17
17: ; preds = %6, %14
%18 = tail call i32 @fseek(i32* %0, i64 %4, i32 0)
ret i32 %8
}