#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*/ perror(char *);
int read(int, unsigned char *, int);
int write(int, unsigned char *, int);
int si_send_char(int fd, int data) {
  unsigned char wbyte, rbyte;
  int n;
  if (fd < 0)
    return 0;
  wbyte = (unsigned char)data;
  if (write(fd, &wbyte, 1) != 1) {
    perror("write");
    return -1;
  }
  if ((n = read(fd, &rbyte, 1)) != 1) {
    if (n < 0)
      perror("read");
    return -1;
  }
  if (wbyte != rbyte)
    return -1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<si_send_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1154 <si_send_char+0x24>
movl   $0x0,-0x4(%rbp)
jmp    11ee <si_send_char+0xbe>
mov    -0xc(%rbp),%eax
mov    %al,-0xd(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     118c <si_send_char+0x5c>
lea    0xe85(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <si_send_char+0xbe>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x1,%eax
je     11cb <si_send_char+0x9b>
cmpl   $0x0,-0x14(%rbp)
jge    11bf <si_send_char+0x8f>
lea    0xe4c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <si_send_char+0xbe>
movzbl -0xd(%rbp),%eax
movzbl -0xe(%rbp),%ecx
cmp    %ecx,%eax
je     11e7 <si_send_char+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <si_send_char+0xbe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<si_send_char>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
js     1177 <si_send_char+0x47>
mov    %esi,%ebx
mov    %edi,%ebp
mov    %bl,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    118d <si_send_char+0x5d>
lea    0x6(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    117b <si_send_char+0x4b>
xor    %ebp,%ebp
cmp    %bl,0x6(%rsp)
setne  %bpl
neg    %ebp
jmp    1184 <si_send_char+0x54>
xor    %ebp,%ebp
jmp    1184 <si_send_char+0x54>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11a0 <si_send_char+0x70>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6c(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1184 <si_send_char+0x54>
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
jmp    1184 <si_send_char+0x54>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @si_send_char(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %38
12: ; preds = %2
%13 = load i32, i32* %5
%14 = trunc i32 %13 to i8
store i8 %14, i8* %6
%15 = load i32, i32* %4
%16 = call i32 @write(i32 %15, i8* %6, i32 1)
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
20: ; preds = %12
%21 = load i32, i32* %4
%22 = call i32 @read(i32 %21, i8* %7, i32 1)
store i32 %22, i32* %8
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
store i32 -1, i32* %3
br label %38
30: ; preds = %20
%31 = load i8, i8* %6
%32 = zext i8 %31 to i32
%33 = load i8, i8* %7
%34 = zext i8 %33 to i32
%35 = icmp ne i32 %32, %34
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 -1, i32* %3
br label %38
37: ; preds = %30
store i32 0, i32* %3
br label %38
38: ; preds = %37, %36, %29, %18, %11
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @si_send_char(i32 %0, i32 %1) {
%3 = alloca i8
%4 = alloca i8
%5 = icmp slt i32 %0, 0
br i1 %5, label %23, label %6
6: ; preds = %2
%7 = trunc i32 %1 to i8
store i8 %7, i8* %3
%8 = call i32 @write(i32 %0, i8* %3, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %23
12: ; preds = %6
%13 = call i32 @read(i32 %0, i8* %4, i32 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = icmp slt i32 %13, 0
br i1 %16, label %17, label %23
17: ; preds = %15
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
19: ; preds = %12
%20 = load i8, i8* %4
%21 = icmp ne i8 %20, %7
%22 = sext i1 %21 to i32
br label %23
23: ; preds = %19, %15, %17, %2, %10
%24 = phi i32 [ -1, %10 ], [ 0, %2 ], [ -1, %17 ], [ -1, %15 ], [ %22, %19 ]
ret i32 %24
}