#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 */
int bsearch(int, int);
int forwsearch(int, int);
int readpattern(char *);
int backsearch(int f, int n) {
  int s;
  if (n == 0)
    n = 1;
  if (n < 1)
    return (forwsearch(f, -n));
  if ((s = readpattern("Reverse search")) != 1)
    return (s);
  return bsearch(f, n);
}


x86 assembly (clang, -O0):
<backsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    114f <backsearch+0x1f>
movl   $0x1,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    116e <backsearch+0x3e>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
sub    -0xc(%rbp),%esi
call   1030 <forwsearch@plt>
mov    %eax,-0x4(%rbp)
jmp    119f <backsearch+0x6f>
lea    0xe8b(%rip),%rdi
call   1040 <readpattern@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x1,%eax
je     1191 <backsearch+0x61>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119f <backsearch+0x6f>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <bsearch@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backsearch>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
test   %esi,%esi
mov    $0x1,%ebx
cmovne %esi,%ebx
test   %ebx,%ebx
jle    1163 <backsearch+0x33>
lea    0xeb6(%rip),%rdi
call   1040 <readpattern@plt>
cmp    $0x1,%eax
jne    1174 <backsearch+0x44>
mov    %ebp,%edi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <bsearch@plt>
neg    %ebx
mov    %ebp,%edi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <forwsearch@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Reverse search\00"
define i32 @backsearch(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 1, i32* %5
br label %10
10: ; preds = %9, %2
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = sub nsw i32 0, %15
%17 = call i32 @forwsearch(i32 %14, i32 %16)
store i32 %17, i32* %3
br label %27
18: ; preds = %10
%19 = call i32 @readpattern(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 %19, i32* %6
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i32, i32* %6
store i32 %22, i32* %3
br label %27
23: ; preds = %18
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = call i32 @bsearch(i32 %24, i32 %25)
store i32 %26, i32* %3
br label %27
27: ; preds = %23, %21, %13
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [15 x i8] c"Reverse search\00"
define i32 @backsearch(i32 %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = select i1 %3, i32 1, i32 %1
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = sub nsw i32 0, %4
%8 = tail call i32 @forwsearch(i32 %0, i32 %7)
br label %14
9: ; preds = %2
%10 = tail call i32 @readpattern(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @bsearch(i32 %0, i32 %4)
br label %14
14: ; preds = %9, %12, %6
%15 = phi i32 [ %8, %6 ], [ %13, %12 ], [ %10, %9 ]
ret i32 %15
}