#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getbyte(int, unsigned char *);
int /*<<< orphan*/ printf(char *, unsigned char);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, unsigned char *, int);
int putbyte(int fd, unsigned char b) {
  unsigned char ack;
  printf("write %02X\n", b);
  if (write(fd, &b, 1) != 1) {
    fprintf(stderr, "error write: %s\n", strerror(errno));
    return 0;
  }
  if (!getbyte(fd, &ack)) {
    fprintf(stderr, "error read: %s\n", strerror(errno));
    return 0;
  }
  printf("read %02X\n", ack);
  if (ack != 0xFA) {
    fprintf(stderr, "error ack\n");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<putbyte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
lea    0xe98(%rip),%rdi
movzbl -0x9(%rbp),%esi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     11c5 <putbyte+0x75>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125d <putbyte+0x10d>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
call   1030 <getbyte@plt>
cmp    $0x0,%eax
jne    1214 <putbyte+0xc4>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125d <putbyte+0x10d>
lea    0xe12(%rip),%rdi
movzbl -0xa(%rbp),%esi
call   1050 <printf@plt>
movzbl -0xa(%rbp),%eax
cmp    $0xfa,%eax
je     1256 <putbyte+0x106>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    125d <putbyte+0x10d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putbyte>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %sil,0x7(%rsp)
lea    0xe9f(%rip),%rdi
call   1050 <printf@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    11c4 <putbyte+0x74>
lea    0x6(%rsp),%rsi
mov    %ebx,%edi
call   1030 <getbyte@plt>
test   %eax,%eax
je     11e4 <putbyte+0x94>
movzbl 0x6(%rsp),%esi
lea    0xe95(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%ebx
cmpb   $0xfa,0x6(%rsp)
je     1210 <putbyte+0xc0>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1210 <putbyte+0xc0>
mov    0x2e15(%rip),%rax
mov    (%rax),%ebp
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe2a(%rip),%rsi
jmp    1202 <putbyte+0xb2>
mov    0x2df5(%rip),%rax
mov    (%rax),%ebp
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe1b(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"write %02X\0A\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"error write: %s\0A\00"
@errno = global i32 0
@.str.2 = [16 x i8] c"error read: %s\0A\00"
@.str.3 = [11 x i8] c"read %02X\0A\00"
@.str.4 = [11 x i8] c"error ack\0A\00"
define i32 @putbyte(i32 %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
store i32 %0, i32* %4
store i8 %1, i8* %5
%7 = load i8, i8* %5
%8 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8 zeroext %7)
%9 = load i32, i32* %4
%10 = call i32 @write(i32 %9, i8* %5, i32 1)
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 0, i32* %3
br label %36
17: ; preds = %2
%18 = load i32, i32* %4
%19 = call i32 @getbyte(i32 %18, i8* %6)
%20 = icmp ne i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 0, i32* %3
br label %36
26: ; preds = %17
%27 = load i8, i8* %6
%28 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %27)
%29 = load i8, i8* %6
%30 = zext i8 %29 to i32
%31 = icmp ne i32 %30, 250
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %36
35: ; preds = %26
store i32 1, i32* %3
br label %36
36: ; preds = %35, %32, %21, %12
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [12 x i8] c"write %02X\0A\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"error write: %s\0A\00"
@errno = global i32 0
@.str.2 = [16 x i8] c"error read: %s\0A\00"
@.str.3 = [11 x i8] c"read %02X\0A\00"
@.str.4 = [11 x i8] c"error ack\0A\00"
define i32 @putbyte(i32 %0, i8 zeroext %1) {
%3 = alloca i8
%4 = alloca i8
store i8 %1, i8* %3
%5 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8 zeroext %1)
%6 = call i32 @write(i32 %0, i8* %3, i32 1)
%7 = icmp eq i32 %6, 1
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %29
13: ; preds = %2
%14 = call i32 @getbyte(i32 %0, i8* %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %29
21: ; preds = %13
%22 = load i8, i8* %4
%23 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %22)
%24 = load i8, i8* %4
%25 = icmp eq i8 %24, -6
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %21, %26, %16, %8
%30 = phi i32 [ 0, %8 ], [ 0, %26 ], [ 0, %16 ], [ 1, %21 ]
ret i32 %30
}