#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*/ F_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
void length() {
  FILE *fp;
  int size, low, high;
  if (access("circular_buffer.txt", F_OK) == -1) {
    fprintf(stderr, "Buffer is not initialized.\n");
    exit(2);
  }
  if (!(fp = fopen("circular_buffer.txt", "r"))) {
    fprintf(stderr, "Could not open file.\n");
    exit(4);
  }
  fscanf(fp, "%d\n", &size);
  fscanf(fp, "%d\n", &low);
  fscanf(fp, "%d\n", &high);
  if (low <= high)
    printf("%d\n", high - low);
  else
    printf("%d\n", size + high - low);
  fclose(fp);
}


x86 assembly (clang, -O0):
<length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <access@plt>
cmp    $0xffffffff,%eax
jne    11b5 <length+0x45>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xe44(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    11f5 <length+0x85>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe48(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe34(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe20(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1254 <length+0xe4>
mov    -0x14(%rbp),%esi
sub    -0x10(%rbp),%esi
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
jmp    1269 <length+0xf9>
mov    -0xc(%rbp),%esi
add    -0x14(%rbp),%esi
sub    -0x10(%rbp),%esi
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<length>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1070 <access@plt>
cmp    $0xffffffff,%eax
je     1218 <length+0xa8>
lea    0xe64(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1237 <length+0xc7>
mov    %rax,%rbx
lea    0xe8d(%rip),%r14
lea    0x14(%rsp),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <fscanf@plt>
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fscanf@plt>
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fscanf@plt>
mov    0xc(%rsp),%esi
mov    0x10(%rsp),%eax
mov    %esi,%ecx
sub    %eax,%ecx
add    0x14(%rsp),%ecx
sub    %eax,%esi
cmovl  %ecx,%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x4,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"circular_buffer.txt\00"
@F_OK = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"Buffer is not initialized.\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [22 x i8] c"Could not open file.\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @length() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @F_OK
%6 = call i32 @access(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 2)
unreachable
12: ; preds = %0
%13 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %13, i32** %1
%14 = icmp ne i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @exit(i32 4)
unreachable
19: ; preds = %12
%20 = load i32*, i32** %1
%21 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%22 = load i32*, i32** %1
%23 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%24 = load i32*, i32** %1
%25 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %4)
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = icmp sle i32 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = load i32, i32* %4
%31 = load i32, i32* %3
%32 = sub nsw i32 %30, %31
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br label %41
34: ; preds = %19
%35 = load i32, i32* %2
%36 = load i32, i32* %4
%37 = add nsw i32 %35, %36
%38 = load i32, i32* %3
%39 = sub nsw i32 %37, %38
%40 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %34, %29
%42 = load i32*, i32** %1
%43 = call i32 @fclose(i32* %42)
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"circular_buffer.txt\00"
@F_OK = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"Buffer is not initialized.\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [22 x i8] c"Could not open file.\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @length() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @F_OK
%8 = tail call i32 @access(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 2)
unreachable
14: ; preds = %0
%15 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 4)
unreachable
21: ; preds = %14
%22 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%23 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%24 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = icmp slt i32 %26, %25
%28 = load i32, i32* %1
%29 = sub i32 %26, %25
%30 = add i32 %29, %28
%31 = sub nsw i32 %26, %25
%32 = select i1 %27, i32 %30, i32 %31
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = call i32 @fclose(i32* %15)
ret void
}