#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_status(int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int transfer_byte(int from, int to, int is_control) {
  char c;
  int ret;
  do {
    ret = read(from, &c, 1);
  } while (ret < 0 && errno == EINTR);
  if (ret == 1) {
    if (is_control) {
      if (c == '\x01') {
        return -1;
      } else if (c == '\x18') {
        print_status(to);
        return 0;
      }
    }
    while (write(to, &c, 1) == -1) {
      if (errno != EAGAIN && errno != EINTR) {
        perror("write failed");
        break;
      }
    }
  } else {
    fprintf(stderr, "\nnothing to read. probably port disconnected.\n");
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<transfer_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jge    119e <transfer_byte+0x4e>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1161 <transfer_byte+0x11>
cmpl   $0x1,-0x18(%rbp)
jne    126f <transfer_byte+0x11f>
cmpl   $0x0,-0x10(%rbp)
je     1201 <transfer_byte+0xb1>
movsbl -0x11(%rbp),%eax
cmp    $0x1,%eax
jne    11d6 <transfer_byte+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <transfer_byte+0x147>
movsbl -0x11(%rbp),%eax
cmp    $0x18,%eax
jne    11f7 <transfer_byte+0xa7>
mov    -0xc(%rbp),%edi
call   1040 <print_status@plt>
movl   $0x0,-0x4(%rbp)
jmp    1297 <transfer_byte+0x147>
jmp    11fc <transfer_byte+0xac>
jmp    1201 <transfer_byte+0xb1>
jmp    1206 <transfer_byte+0xb6>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    126a <transfer_byte+0x11a>
mov    0x2db1(%rip),%rax
mov    (%rax),%rax
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
je     1265 <transfer_byte+0x115>
mov    0x2d97(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
je     1265 <transfer_byte+0x115>
lea    0xda5(%rip),%rdi
call   1070 <perror@plt>
jmp    126a <transfer_byte+0x11a>
jmp    1206 <transfer_byte+0xb6>
jmp    1290 <transfer_byte+0x140>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1297 <transfer_byte+0x147>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_byte>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ebp
lea    0x7(%rsp),%rbx
mov    0x2e69(%rip),%r12
mov    0x2e5a(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    119d <transfer_byte+0x4d>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1180 <transfer_byte+0x30>
cmp    $0x1,%eax
jne    11f1 <transfer_byte+0xa1>
test   %r14d,%r14d
je     11d6 <transfer_byte+0x86>
mov    0x7(%rsp),%al
cmp    $0x1,%al
je     121c <transfer_byte+0xcc>
cmp    $0x18,%al
jne    11d6 <transfer_byte+0x86>
mov    %r15d,%edi
call   1040 <print_status@plt>
xor    %ebp,%ebp
jmp    120b <transfer_byte+0xbb>
nop
mov    (%r12),%rax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
je     11d6 <transfer_byte+0x86>
cmp    0x0(%r13),%rax
jne    1223 <transfer_byte+0xd3>
lea    0x7(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
je     11c0 <transfer_byte+0x70>
jmp    120b <transfer_byte+0xbb>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    120b <transfer_byte+0xbb>
lea    0xdd6(%rip),%rdi
call   1070 <perror@plt>
jmp    120b <transfer_byte+0xbb>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [13 x i8] c"write failed\00"
@stderr = global i32 0
@.str.1 = [47 x i8] c"\0Anothing to read. probably port disconnected.\0A\00"
define i32 @transfer_byte(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
br label %10
10: ; preds = %20, %3
%11 = load i32, i32* %5
%12 = call i32 @read(i32 %11, i8* %8, i32 1)
store i32 %12, i32* %9
br label %13
13: ; preds = %10
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br label %20
20: ; preds = %16, %13
%21 = phi i1 [ false, %13 ], [ %19, %16 ]
br i1 %21, label %10, label %22
22: ; preds = %20
%23 = load i32, i32* %9
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %59
25: ; preds = %22
%26 = load i32, i32* %7
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %25
%29 = load i8, i8* %8
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %4
br label %63
33: ; preds = %28
%34 = load i8, i8* %8
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 24
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* %6
%39 = call i32 @print_status(i32 %38)
store i32 0, i32* %4
br label %63
40: ; preds = %33
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41, %25
br label %43
43: ; preds = %57, %42
%44 = load i32, i32* %6
%45 = call i32 @write(i32 %44, i8* %8, i32 1)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %58
47: ; preds = %43
%48 = load i64, i64* @errno
%49 = load i64, i64* @EAGAIN
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = load i64, i64* @errno
%53 = load i64, i64* @EINTR
%54 = icmp ne i64 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %58
57: ; preds = %51, %47
br label %43
58: ; preds = %55, %43
br label %62
59: ; preds = %22
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %4
br label %63
62: ; preds = %58
store i32 0, i32* %4
br label %63
63: ; preds = %62, %59, %37, %32
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [13 x i8] c"write failed\00"
@stderr = global i32 0
@.str.1 = [47 x i8] c"\0Anothing to read. probably port disconnected.\0A\00"
define i32 @transfer_byte(i32 %0, i32 %1, i32 %2) {
%4 = alloca i8
br label %5
5: ; preds = %5, %3
%6 = call i32 @read(i32 %0, i8* %4, i32 1)
%7 = icmp slt i32 %6, 0
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %5, label %12
12: ; preds = %5
%13 = icmp eq i32 %6, 1
br i1 %13, label %14, label %33
14: ; preds = %12
%15 = icmp eq i32 %2, 0
br i1 %15, label %16, label %17
16: ; preds = %17, %14
br label %21
17: ; preds = %14
%18 = load i8, i8* %4
switch i8 %18, label %16 [
i8 1, label %36
i8 24, label %19
]
19: ; preds = %17
%20 = tail call i32 @print_status(i32 %1)
br label %36
21: ; preds = %16, %24
%22 = call i32 @write(i32 %1, i8* %4, i32 1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %36
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EAGAIN
%27 = icmp eq i64 %25, %26
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %25, %28
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %21, label %31
31: ; preds = %24
%32 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %36
33: ; preds = %12
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %21, %31, %17, %33, %19
%37 = phi i32 [ 0, %19 ], [ -2, %33 ], [ -1, %17 ], [ 0, %31 ], [ 0, %21 ]
ret i32 %37
}