#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 */
scalar_t__ EINVAL;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ ted_fini();
scalar_t__ ted_init(char *);
scalar_t__ ted_read(int *, int *, int *, int *);
int main(int argc, char *argv[]) {
  int addr, count, volts, watts;
  if (ted_init("/dev/ttyAMA0") < 0) {
    perror("/dev/ttyAMA0");
    return 1;
  }
  for (;;) {
    if (ted_read(&addr, &count, &watts, &volts) < 0) {
      if (errno != EINVAL) {
        perror("ted_read");
        break;
      }
      fprintf(stderr, "bad packet\n");
      continue;
    }
    printf("addr=%d count=%d volts=%d watts=%d\n", addr, count, volts, watts);
  }
  ted_fini();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rdi
call   1030 <ted_init@plt>
cmp    $0x0,%rax
jge    11a4 <main+0x44>
lea    0xe6d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1239 <main+0xd9>
jmp    11a9 <main+0x49>
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1060 <ted_read@plt>
cmp    $0x0,%rax
jge    120d <main+0xad>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
je     11f3 <main+0x93>
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
jmp    122b <main+0xcb>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11a9 <main+0x49>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
lea    0xe01(%rip),%rdi
call   1040 <printf@plt>
jmp    11a9 <main+0x49>
mov    $0x0,%al
call   1070 <ted_fini@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8b(%rip),%rdi
call   1030 <ted_init@plt>
test   %rax,%rax
jns    1195 <main+0x35>
lea    0xe7a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    122d <main+0xcd>
lea    0x14(%rsp),%r14
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0xc(%rsp),%rbx
lea    0xe66(%rip),%r13
lea    0xe6b(%rip),%rbp
jmp    11d9 <main+0x79>
nopl   0x0(%rax)
mov    0x14(%rsp),%esi
mov    0x10(%rsp),%edx
mov    0xc(%rsp),%ecx
mov    0x8(%rsp),%r8d
mov    %rbp,%rdi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1060 <ted_read@plt>
test   %rax,%rax
jns    11c0 <main+0x60>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%rax
jne    1218 <main+0xb8>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
call   1050 <fprintf@plt>
jmp    11d9 <main+0x79>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <ted_fini@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.1 = [9 x i8] c"ted_read\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"bad packet\0A\00"
@.str.3 = [36 x i8] c"addr=%d count=%d volts=%d watts=%d\0A\00"
define i32 @main(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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 @ted_init(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %35
14: ; preds = %2
br label %15
15: ; preds = %27, %24, %14
%16 = call i64 @ted_read(i32* %6, i32* %7, i32* %9, i32* %8)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINVAL
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %33
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %15
27: ; preds = %15
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29, i32 %30, i32 %31)
br label %15
33: ; preds = %22
%34 = call i32 (...) @ted_fini()
store i32 0, i32* %3
br label %35
35: ; preds = %33, %12
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.1 = [9 x i8] c"ted_read\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"bad packet\0A\00"
@.str.3 = [36 x i8] c"addr=%d count=%d volts=%d watts=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = tail call i64 @ted_init(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %35
15: ; preds = %2, %28
%16 = call i64 @ted_read(i32* %3, i32* %4, i32* %6, i32* %5)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINVAL
%21 = icmp eq i64 %19, %20
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 (...) @ted_fini()
br label %35
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %25, %29
br label %15
29: ; preds = %15
%30 = load i32, i32* %3
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = load i32, i32* %6
%34 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 %31, i32 %32, i32 %33)
br label %28
35: ; preds = %22, %13
%36 = phi i32 [ 1, %13 ], [ 0, %22 ]
ret i32 %36
}