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

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
scalar_t__ fseeko(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftello(int /*<<< orphan*/ *);
int read_backwards(int /*<<< orphan*/ *, char *, int);
int64_t seek_to_start_of_line(FILE *fp, int64_t boundary) {
  char buf[16];
  int64_t curr;
  int64_t seek_size;
  int64_t ret;
  int64_t i;
  curr = ftello(fp);
  if (curr == 0) {
    return 0;
  } else if (curr < 0) {
    return -16;
  }
  while (curr > boundary) {
    seek_size = (curr < 16) ? curr : 16;
    if ((curr - seek_size) < boundary) {
      seek_size = curr - boundary;
    }
    ret = read_backwards(fp, buf, seek_size);
    if (ret < 0 || ret != seek_size)
      return -16;
    curr -= seek_size;
    for (i = 0; i < seek_size; ++i) {
      if (buf[i] == '\n') {
        if (fseeko(fp, i, SEEK_CUR) != 0) {
          return -16;
        }
        return curr + i;
      }
    }
  }
  return curr;
}


x86 assembly (clang, -O0):
<seek_to_start_of_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ftello@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1161 <seek_to_start_of_line+0x31>
movl   $0x0,-0x4(%rbp)
jmp    1289 <seek_to_start_of_line+0x159>
cmpl   $0x0,-0x34(%rbp)
jge    1177 <seek_to_start_of_line+0x47>
movl   $0xfffffff0,-0x4(%rbp)
jmp    1289 <seek_to_start_of_line+0x159>
jmp    117c <seek_to_start_of_line+0x4c>
jmp    1181 <seek_to_start_of_line+0x51>
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1283 <seek_to_start_of_line+0x153>
cmpl   $0x10,-0x34(%rbp)
jge    11a2 <seek_to_start_of_line+0x72>
mov    -0x34(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    11af <seek_to_start_of_line+0x7f>
mov    $0x10,%eax
mov    %eax,-0x44(%rbp)
jmp    11af <seek_to_start_of_line+0x7f>
mov    -0x44(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
sub    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11cd <seek_to_start_of_line+0x9d>
mov    -0x34(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1040 <read_backwards@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     11f6 <seek_to_start_of_line+0xc6>
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
je     1202 <seek_to_start_of_line+0xd2>
movl   $0xfffffff0,-0x4(%rbp)
jmp    1289 <seek_to_start_of_line+0x159>
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    127e <seek_to_start_of_line+0x14e>
movslq -0x40(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    126b <seek_to_start_of_line+0x13b>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
call   1050 <fseeko@plt>
cmp    $0x0,%rax
je     125d <seek_to_start_of_line+0x12d>
movl   $0xfffffff0,-0x4(%rbp)
jmp    1289 <seek_to_start_of_line+0x159>
mov    -0x34(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1289 <seek_to_start_of_line+0x159>
jmp    1270 <seek_to_start_of_line+0x140>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1214 <seek_to_start_of_line+0xe4>
jmp    1181 <seek_to_start_of_line+0x51>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seek_to_start_of_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
call   1030 <ftello@plt>
test   %eax,%eax
je     1164 <seek_to_start_of_line+0x34>
js     11d1 <seek_to_start_of_line+0xa1>
mov    %eax,%r13d
cmp    %r14d,%eax
jle    121b <seek_to_start_of_line+0xeb>
mov    %rsp,%r12
jmp    117c <seek_to_start_of_line+0x4c>
xor    %r13d,%r13d
jmp    121b <seek_to_start_of_line+0xeb>
nopl   0x0(%rax)
sub    %ebp,%r13d
cmp    %r14d,%r13d
jle    121b <seek_to_start_of_line+0xeb>
cmp    $0x10,%r13d
mov    $0x10,%eax
cmovl  %r13d,%eax
mov    %r13d,%ecx
sub    %eax,%ecx
mov    %r13d,%ebp
sub    %r14d,%ebp
cmp    %r14d,%ecx
cmovge %eax,%ebp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1040 <read_backwards@plt>
test   %eax,%eax
js     11d1 <seek_to_start_of_line+0xa1>
cmp    %ebp,%eax
jne    11d1 <seek_to_start_of_line+0xa1>
test   %ebp,%ebp
jle    1170 <seek_to_start_of_line+0x40>
mov    %ebp,%eax
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
cmpb   $0xa,(%rsp,%rbx,1)
je     11d9 <seek_to_start_of_line+0xa9>
add    $0x1,%rbx
cmp    %rax,%rbx
jb     11c0 <seek_to_start_of_line+0x90>
jmp    1170 <seek_to_start_of_line+0x40>
mov    $0xfffffff0,%r13d
jmp    121b <seek_to_start_of_line+0xeb>
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %ebx,%esi
call   1050 <fseeko@plt>
lea    (%r14,%r13,1),%ecx
cmp    $0x10,%r13d
mov    $0x10,%edx
cmovl  %r13d,%edx
sub    %edx,%r13d
cmp    %r13d,%r14d
cmovl  %r14d,%r13d
sub    %r13d,%ecx
sub    %edx,%ecx
add    %ebx,%ecx
test   %rax,%rax
mov    $0xfffffff0,%r13d
cmove  %ecx,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
define i32 @seek_to_start_of_line(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [16 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%11 = load i32*, i32** %4
%12 = call i32 @ftello(i32* %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %88
16: ; preds = %2
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 -16, i32* %3
br label %88
20: ; preds = %16
br label %21
21: ; preds = %20
br label %22
22: ; preds = %85, %21
%23 = load i32, i32* %7
%24 = load i32, i32* %5
%25 = icmp sgt i32 %23, %24
br i1 %25, label %26, label %86
26: ; preds = %22
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 16
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i32, i32* %7
br label %32
31: ; preds = %26
br label %32
32: ; preds = %31, %29
%33 = phi i32 [ %30, %29 ], [ 16, %31 ]
store i32 %33, i32* %8
%34 = load i32, i32* %7
%35 = load i32, i32* %8
%36 = sub nsw i32 %34, %35
%37 = load i32, i32* %5
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = load i32, i32* %7
%41 = load i32, i32* %5
%42 = sub nsw i32 %40, %41
store i32 %42, i32* %8
br label %43
43: ; preds = %39, %32
%44 = load i32*, i32** %4
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%46 = load i32, i32* %8
%47 = call i32 @read_backwards(i32* %44, i8* %45, i32 %46)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp slt i32 %48, 0
br i1 %49, label %54, label %50
50: ; preds = %43
%51 = load i32, i32* %9
%52 = load i32, i32* %8
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50, %43
store i32 -16, i32* %3
br label %88
55: ; preds = %50
%56 = load i32, i32* %8
%57 = load i32, i32* %7
%58 = sub nsw i32 %57, %56
store i32 %58, i32* %7
store i32 0, i32* %10
br label %59
59: ; preds = %82, %55
%60 = load i32, i32* %10
%61 = load i32, i32* %8
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %85
63: ; preds = %59
%64 = load i32, i32* %10
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 %65
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = icmp eq i32 %68, 10
br i1 %69, label %70, label %81
70: ; preds = %63
%71 = load i32*, i32** %4
%72 = load i32, i32* %10
%73 = load i32, i32* @SEEK_CUR
%74 = call i64 @fseeko(i32* %71, i32 %72, i32 %73)
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %77
76: ; preds = %70
store i32 -16, i32* %3
br label %88
77: ; preds = %70
%78 = load i32, i32* %7
%79 = load i32, i32* %10
%80 = add nsw i32 %78, %79
store i32 %80, i32* %3
br label %88
81: ; preds = %63
br label %82
82: ; preds = %81
%83 = load i32, i32* %10
%84 = add nsw i32 %83, 1
store i32 %84, i32* %10
br label %59
85: ; preds = %59
br label %22
86: ; preds = %22
%87 = load i32, i32* %7
store i32 %87, i32* %3
br label %88
88: ; preds = %86, %77, %76, %54, %19, %15
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
define i32 @seek_to_start_of_line(i32* %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @ftello(i32* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %45, label %7
7: ; preds = %2
%8 = icmp slt i32 %5, 0
br i1 %8, label %45, label %9
9: ; preds = %7
%10 = icmp sgt i32 %5, %1
br i1 %10, label %13, label %45
11: ; preds = %42, %25
%12 = icmp sgt i32 %26, %1
br i1 %12, label %13, label %45
13: ; preds = %9, %11
%14 = phi i32 [ %26, %11 ], [ %5, %9 ]
%15 = icmp slt i32 %14, 16
%16 = select i1 %15, i32 %14, i32 16
%17 = sub nsw i32 %14, %16
%18 = icmp slt i32 %17, %1
%19 = sub nsw i32 %14, %1
%20 = select i1 %18, i32 %19, i32 %16
%21 = call i32 @read_backwards(i32* %0, i8* %4, i32 %20)
%22 = icmp sgt i32 %21, -1
%23 = icmp eq i32 %21, %20
%24 = and i1 %22, %23
br i1 %24, label %25, label %45
25: ; preds = %13
%26 = sub nsw i32 %14, %20
%27 = icmp sgt i32 %20, 0
br i1 %27, label %28, label %11
28: ; preds = %25
%29 = zext i32 %20 to i64
br label %30
30: ; preds = %28, %42
%31 = phi i64 [ 0, %28 ], [ %43, %42 ]
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 10
br i1 %34, label %35, label %42
35: ; preds = %30
%36 = trunc i64 %31 to i32
%37 = load i32, i32* @SEEK_CUR
%38 = call i64 @fseeko(i32* %0, i32 %36, i32 %37)
%39 = icmp eq i64 %38, 0
%40 = add nsw i32 %26, %36
%41 = select i1 %39, i32 %40, i32 -16
br label %45
42: ; preds = %30
%43 = add nuw nsw i64 %31, 1
%44 = icmp ult i64 %43, %29
br i1 %44, label %30, label %11
45: ; preds = %13, %11, %9, %35, %7, %2
%46 = phi i32 [ 0, %2 ], [ -16, %7 ], [ %41, %35 ], [ %5, %9 ], [ -16, %13 ], [ %26, %11 ]
ret i32 %46
}