#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int read_packet(int fd, unsigned char *buf) {
  int x;
  int n;
  int byte = 0;
  int idx;
  int bytes_read = 0;
  for (x = 0; x < 14; x++)
    buf[x] = 0;
  for (x = 0; x < 15; x++) {
    n = read(fd, &byte, 1);
    if (n <= 0) {
      printf("Read EOF\n");
      exit(0);
    }
    if (byte == 0) {
      printf("Meter ON.\n");
      return -1;
    }
    idx = (byte >> 4) & 0xF;
    if ((idx == 0) || (idx == 0xF)) {
      printf("Read invalid byte 0x%02X\n", byte);
      return -1;
    }
    buf[idx - 1] = byte & 0xF;
    bytes_read++;
    if (idx == 0xE) {
      if (bytes_read < 13) {
        printf("Only read %d bytes of packet.\n", bytes_read);
        return -1;
      } else
        return 0;
    }
  }
  printf("Read too many bytes.\n");
  return -1;
}


x86 assembly (clang, -O0):
<read_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xe,-0x14(%rbp)
jge    1178 <read_packet+0x48>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1154 <read_packet+0x24>
movl   $0x0,-0x14(%rbp)
cmpl   $0xf,-0x14(%rbp)
jge    128e <read_packet+0x15e>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11bc <read_packet+0x8c>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11e0 <read_packet+0xb0>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <read_packet+0x173>
mov    -0x1c(%rbp),%eax
sar    $0x4,%eax
and    $0xf,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1200 <read_packet+0xd0>
cmpl   $0xf,-0x20(%rbp)
jne    121d <read_packet+0xed>
mov    -0x1c(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <read_packet+0x173>
mov    -0x1c(%rbp),%eax
and    $0xf,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xe,-0x20(%rbp)
jne    127b <read_packet+0x14b>
cmpl   $0xd,-0x24(%rbp)
jge    126f <read_packet+0x13f>
mov    -0x24(%rbp),%esi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <read_packet+0x173>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <read_packet+0x173>
jmp    1280 <read_packet+0x150>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117f <read_packet+0x4f>
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
movl   $0x0,0x4(%rsp)
movq   $0x0,0x6(%rsi)
movq   $0x0,(%rsi)
xor    %ebx,%ebx
lea    0x4(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11fe <read_packet+0xbe>
mov    0x4(%rsp),%esi
test   %esi,%esi
je     11cd <read_packet+0x8d>
mov    %esi,%eax
shr    $0x4,%eax
and    $0xf,%eax
cmp    $0xf,%eax
je     11bd <read_packet+0x7d>
test   %eax,%eax
je     11bd <read_packet+0x7d>
and    $0xf,%sil
lea    -0x1(%rax),%ecx
mov    %sil,(%r14,%rcx,1)
cmp    $0xe,%eax
je     11e9 <read_packet+0xa9>
add    $0x1,%ebx
cmp    $0xf,%ebx
jne    1170 <read_packet+0x30>
lea    0xe7e(%rip),%rdi
jmp    11d4 <read_packet+0x94>
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d9 <read_packet+0x99>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
cmp    $0xb,%ebx
ja     11de <read_packet+0x9e>
add    $0x1,%ebx
lea    0xe20(%rip),%rdi
mov    %ebx,%esi
jmp    11c4 <read_packet+0x84>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"Read EOF\0A\00"
@.str.1 = [11 x i8] c"Meter ON.\0A\00"
@.str.2 = [26 x i8] c"Read invalid byte 0x%02X\0A\00"
@.str.3 = [31 x i8] c"Only read %d bytes of packet.\0A\00"
@.str.4 = [22 x i8] c"Read too many bytes.\0A\00"
define i32 @read_packet(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %8
store i32 0, i32* %10
store i32 0, i32* %6
br label %11
11: ; preds = %19, %2
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 14
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %14
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
store i32 %21, i32* %6
br label %11
22: ; preds = %11
store i32 0, i32* %6
br label %23
23: ; preds = %72, %22
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 15
br i1 %25, label %26, label %75
26: ; preds = %23
%27 = load i32, i32* %4
%28 = call i32 @read(i32 %27, i32* %8, i32 1)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp sle i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %26
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %77
39: ; preds = %34
%40 = load i32, i32* %8
%41 = ashr i32 %40, 4
%42 = and i32 %41, 15
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %39
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, 15
br i1 %47, label %48, label %51
48: ; preds = %45, %39
%49 = load i32, i32* %8
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %49)
store i32 -1, i32* %3
br label %77
51: ; preds = %45
%52 = load i32, i32* %8
%53 = and i32 %52, 15
%54 = trunc i32 %53 to i8
%55 = load i8*, i8** %5
%56 = load i32, i32* %9
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 %54, i8* %59
%60 = load i32, i32* %10
%61 = add nsw i32 %60, 1
store i32 %61, i32* %10
%62 = load i32, i32* %9
%63 = icmp eq i32 %62, 14
br i1 %63, label %64, label %71
64: ; preds = %51
%65 = load i32, i32* %10
%66 = icmp slt i32 %65, 13
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32, i32* %10
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %68)
store i32 -1, i32* %3
br label %77
70: ; preds = %64
store i32 0, i32* %3
br label %77
71: ; preds = %51
br label %72
72: ; preds = %71
%73 = load i32, i32* %6
%74 = add nsw i32 %73, 1
store i32 %74, i32* %6
br label %23
75: ; preds = %23
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %77
77: ; preds = %75, %70, %67, %48, %37
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@.str.2 = [26 x i8] c"Read invalid byte 0x%02X\0A\00"
@.str.3 = [31 x i8] c"Only read %d bytes of packet.\0A\00"
@str = [21 x i8] c"Read too many bytes.\00"
@str.5 = [10 x i8] c"Meter ON.\00"
@str.6 = [9 x i8] c"Read EOF\00"
define i32 @read_packet(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
br label %7
5: ; preds = %24
%6 = icmp eq i32 %30, 15
br i1 %6, label %36, label %7
7: ; preds = %2, %5
%8 = phi i32 [ %30, %5 ], [ 0, %2 ]
%9 = call i32 @read(i32 %0, i32* %3, i32 1)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.6, i64 0, i64 0))
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %7
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.5, i64 0, i64 0))
br label %38
19: ; preds = %14
%20 = lshr i32 %15, 4
%21 = and i32 %20, 15
switch i32 %21, label %24 [
i32 15, label %22
i32 0, label %22
]
22: ; preds = %19, %19
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %15)
br label %38
24: ; preds = %19
%25 = trunc i32 %15 to i8
%26 = and i8 %25, 15
%27 = add nsw i32 %21, -1
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %1, i64 %28
store i8 %26, i8* %29
%30 = add nuw nsw i32 %8, 1
%31 = icmp eq i32 %21, 14
br i1 %31, label %32, label %5
32: ; preds = %24
%33 = icmp ult i32 %8, 12
br i1 %33, label %34, label %38
34: ; preds = %32
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %38
36: ; preds = %5
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %32, %36, %34, %22, %17
%39 = phi i32 [ -1, %17 ], [ -1, %22 ], [ -1, %34 ], [ -1, %36 ], [ 0, %32 ]
ret i32 %39
}