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

/* Variables and functions */
int EAGAIN;
int read(int, char *, int);
int sscanf(char *, char *, long *, long *, long *, ...);
int inv_read_buffer(int fd, long *data, long long *timestamp) {
  char str[35];
  int count;
  count = read(fd, str, sizeof(str));
  if (!count)
    return count;
  if (!timestamp)
    count = sscanf(str, "%ld%ld%ld", &data[0], &data[1], &data[2]);
  else
    count =
        sscanf(str, "%ld%ld%ld%lld", &data[0], &data[1], &data[2], timestamp);
  if (count < (timestamp ? 4 : 3))
    return -EAGAIN;
  return count;
}


x86 assembly (clang, -O0):
<inv_read_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x23,%edx
call   1030 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    115c <inv_read_buffer+0x3c>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <inv_read_buffer+0xdb>
cmpq   $0x0,-0x18(%rbp)
jne    1195 <inv_read_buffer+0x75>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x10(%rbp),%rcx
add    $0x8,%rcx
mov    -0x10(%rbp),%r8
add    $0x10,%r8
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1040 <sscanf@plt>
mov    %eax,-0x44(%rbp)
jmp    11c2 <inv_read_buffer+0xa2>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x10(%rbp),%rcx
add    $0x8,%rcx
mov    -0x10(%rbp),%r8
add    $0x10,%r8
mov    -0x18(%rbp),%r9
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1040 <sscanf@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    -0x18(%rbp),%rsi
mov    $0x3,%ecx
mov    $0x4,%edx
cmp    $0x0,%rsi
cmovne %edx,%ecx
cmp    %ecx,%eax
jge    11f5 <inv_read_buffer+0xd5>
xor    %eax,%eax
mov    0x2ded(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <inv_read_buffer+0xdb>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inv_read_buffer>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rsp,%rsi
mov    $0x23,%edx
call   1030 <read@plt>
test   %eax,%eax
je     116d <inv_read_buffer+0x4d>
lea    0x8(%rbx),%rcx
lea    0x10(%rbx),%r8
test   %r14,%r14
je     1171 <inv_read_buffer+0x51>
lea    0xeb8(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
mov    %r14,%r9
xor    %eax,%eax
call   1040 <sscanf@plt>
mov    $0x4,%ecx
cmp    %ecx,%eax
jl     118e <inv_read_buffer+0x6e>
jmp    1199 <inv_read_buffer+0x79>
xor    %eax,%eax
jmp    1199 <inv_read_buffer+0x79>
lea    0xe88(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <sscanf@plt>
mov    $0x3,%ecx
cmp    %ecx,%eax
jge    1199 <inv_read_buffer+0x79>
mov    0x2e43(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"%ld%ld%ld\00"
@.str.1 = [14 x i8] c"%ld%ld%ld%lld\00"
@EAGAIN = global i32 0
define i32 @inv_read_buffer(i32 %0, i64* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca [35 x i8]
%9 = alloca i32
store i32 %0, i32* %5
store i64* %1, i64** %6
store i64* %2, i64** %7
%10 = load i32, i32* %5
%11 = getelementptr inbounds [35 x i8], [35 x i8]* %8, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 35)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = load i32, i32* %9
store i32 %16, i32* %4
br label %51
17: ; preds = %3
%18 = load i64*, i64** %7
%19 = icmp ne i64* %18, null
br i1 %19, label %29, label %20
20: ; preds = %17
%21 = getelementptr inbounds [35 x i8], [35 x i8]* %8, i64 0, i64 0
%22 = load i64*, i64** %6
%23 = getelementptr inbounds i64, i64* %22, i64 0
%24 = load i64*, i64** %6
%25 = getelementptr inbounds i64, i64* %24, i64 1
%26 = load i64*, i64** %6
%27 = getelementptr inbounds i64, i64* %26, i64 2
%28 = call i32 (i8*, i8*, i64*, i64*, i64*, ...) @sscanf(i8* %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64* %23, i64* %25, i64* %27)
store i32 %28, i32* %9
br label %39
29: ; preds = %17
%30 = getelementptr inbounds [35 x i8], [35 x i8]* %8, i64 0, i64 0
%31 = load i64*, i64** %6
%32 = getelementptr inbounds i64, i64* %31, i64 0
%33 = load i64*, i64** %6
%34 = getelementptr inbounds i64, i64* %33, i64 1
%35 = load i64*, i64** %6
%36 = getelementptr inbounds i64, i64* %35, i64 2
%37 = load i64*, i64** %7
%38 = call i32 (i8*, i8*, i64*, i64*, i64*, ...) @sscanf(i8* %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64* %32, i64* %34, i64* %36, i64* %37)
store i32 %38, i32* %9
br label %39
39: ; preds = %29, %20
%40 = load i32, i32* %9
%41 = load i64*, i64** %7
%42 = icmp ne i64* %41, null
%43 = zext i1 %42 to i64
%44 = select i1 %42, i32 4, i32 3
%45 = icmp slt i32 %40, %44
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* @EAGAIN
%48 = sub nsw i32 0, %47
store i32 %48, i32* %4
br label %51
49: ; preds = %39
%50 = load i32, i32* %9
store i32 %50, i32* %4
br label %51
51: ; preds = %49, %46, %15
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@.str = [10 x i8] c"%ld%ld%ld\00"
@.str.1 = [14 x i8] c"%ld%ld%ld%lld\00"
@EAGAIN = global i32 0
define i32 @inv_read_buffer(i32 %0, i64* %1, i64* %2) {
%4 = alloca [35 x i8]
%5 = getelementptr inbounds [35 x i8], [35 x i8]* %4, i64 0, i64 0
%6 = call i32 @read(i32 %0, i8* %5, i32 35)
%7 = icmp eq i32 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %3
%9 = icmp eq i64* %2, null
%10 = getelementptr inbounds i64, i64* %1, i64 1
%11 = getelementptr inbounds i64, i64* %1, i64 2
br i1 %9, label %12, label %14
12: ; preds = %8
%13 = call i32 (i8*, i8*, i64*, i64*, i64*, ...) @sscanf(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64* %1, i64* %10, i64* %11)
br label %16
14: ; preds = %8
%15 = call i32 (i8*, i8*, i64*, i64*, i64*, ...) @sscanf(i8* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64* %1, i64* %10, i64* %11, i64* %2)
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ 4, %14 ], [ 3, %12 ]
%18 = phi i32 [ %15, %14 ], [ %13, %12 ]
%19 = icmp slt i32 %18, %17
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @EAGAIN
%22 = sub nsw i32 0, %21
br label %23
23: ; preds = %16, %3, %20
%24 = phi i32 [ %22, %20 ], [ 0, %3 ], [ %18, %16 ]
ret i32 %24
}