#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 read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int writeword(int fd, int word, char ack) {
  char rwchar;
  rwchar = word;
  write(fd, &rwchar, 1);
  if ((read(fd, &rwchar, 1) == 1) && (rwchar == ack)) {
    rwchar = word >> 8;
    write(fd, &rwchar, 1);
    if ((read(fd, &rwchar, 1) == 1) && (rwchar == ack))
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writeword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dl,%al
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
mov    -0xc(%rbp),%eax
mov    %al,-0xe(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11c9 <writeword+0xa9>
movsbl -0xe(%rbp),%eax
movsbl -0xd(%rbp),%ecx
cmp    %ecx,%eax
jne    11c9 <writeword+0xa9>
mov    -0xc(%rbp),%eax
sar    $0x8,%eax
mov    %al,-0xe(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11c4 <writeword+0xa4>
movsbl -0xe(%rbp),%eax
movsbl -0xd(%rbp),%ecx
cmp    %ecx,%eax
jne    11c4 <writeword+0xa4>
movl   $0x1,-0x4(%rbp)
jmp    11d0 <writeword+0xb0>
jmp    11c9 <writeword+0xa9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeword>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%ebp
mov    %bl,0x7(%rsp)
lea    0x7(%rsp),%r15
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    1197 <writeword+0x77>
cmp    %r14b,0x7(%rsp)
jne    1197 <writeword+0x77>
mov    %bh,0x7(%rsp)
lea    0x7(%rsp),%r15
mov    $0x1,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    1197 <writeword+0x77>
cmp    %r14b,0x7(%rsp)
je     1199 <writeword+0x79>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @writeword(i32 %0, i32 %1, i8 signext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8 %2, i8* %7
%9 = load i32, i32* %6
%10 = trunc i32 %9 to i8
store i8 %10, i8* %8
%11 = load i32, i32* %5
%12 = call i32 @write(i32 %11, i8* %8, i32 1)
%13 = load i32, i32* %5
%14 = call i32 @read(i32 %13, i8* %8, i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %39
16: ; preds = %3
%17 = load i8, i8* %8
%18 = sext i8 %17 to i32
%19 = load i8, i8* %7
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %18, %20
br i1 %21, label %22, label %39
22: ; preds = %16
%23 = load i32, i32* %6
%24 = ashr i32 %23, 8
%25 = trunc i32 %24 to i8
store i8 %25, i8* %8
%26 = load i32, i32* %5
%27 = call i32 @write(i32 %26, i8* %8, i32 1)
%28 = load i32, i32* %5
%29 = call i32 @read(i32 %28, i8* %8, i32 1)
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %38
31: ; preds = %22
%32 = load i8, i8* %8
%33 = sext i8 %32 to i32
%34 = load i8, i8* %7
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %33, %35
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 1, i32* %4
br label %40
38: ; preds = %31, %22
br label %39
39: ; preds = %38, %16, %3
store i32 0, i32* %4
br label %40
40: ; preds = %39, %37
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
define i32 @writeword(i32 %0, i32 %1, i8 signext %2) {
%4 = alloca i8
%5 = trunc i32 %1 to i8
store i8 %5, i8* %4
%6 = call i32 @write(i32 %0, i8* %4, i32 1)
%7 = call i32 @read(i32 %0, i8* %4, i32 1)
%8 = icmp eq i32 %7, 1
%9 = load i8, i8* %4
%10 = icmp eq i8 %9, %2
%11 = select i1 %8, i1 %10, i1 false
br i1 %11, label %12, label %21
12: ; preds = %3
%13 = lshr i32 %1, 8
%14 = trunc i32 %13 to i8
store i8 %14, i8* %4
%15 = call i32 @write(i32 %0, i8* %4, i32 1)
%16 = call i32 @read(i32 %0, i8* %4, i32 1)
%17 = icmp eq i32 %16, 1
%18 = load i8, i8* %4
%19 = icmp eq i8 %18, %2
%20 = select i1 %17, i1 %19, i1 false
br i1 %20, label %22, label %21
21: ; preds = %12, %3
br label %22
22: ; preds = %12, %21
%23 = phi i32 [ 0, %21 ], [ 1, %12 ]
ret i32 %23
}