#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 */
typedef int ssize_t;

/* Variables and functions */
int ab_read(int, char *, int);
int /*<<< orphan*/ ab_write(int, char *, size_t);
int errno;
scalar_t__ strcmp(char *, char *);
int do_echo(int fdin, int fdout) {
  char buff[1024];
  ssize_t retsize;
  while ((retsize = ab_read(fdin, buff, 1024)) > 0) {
    ab_write(fdout, buff, (size_t)retsize);
    if (strcmp(buff, "exit\n") == 0)
      break;
  }
  if (retsize == -1)
    return errno;
  else
    return 0;
}


x86 assembly (clang, -O0):
<do_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <ab_read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    11a1 <do_echo+0x71>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
movslq -0x414(%rbp),%rdx
call   1030 <ab_write@plt>
lea    -0x410(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    119c <do_echo+0x6c>
jmp    11a1 <do_echo+0x71>
jmp    1141 <do_echo+0x11>
cmpl   $0xffffffff,-0x414(%rbp)
jne    11bf <do_echo+0x8f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11c6 <do_echo+0x96>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_echo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %esi,%r14d
mov    %edi,%r12d
mov    %rsp,%rbx
lea    0xeb1(%rip),%r15
nop
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <ab_read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1183 <do_echo+0x53>
mov    %ebp,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <ab_write@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1150 <do_echo+0x20>
xor    %eax,%eax
cmp    $0xffffffff,%ebp
jne    1193 <do_echo+0x63>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"exit\0A\00"
@errno = global i32 0
define i32 @do_echo(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %8
8: ; preds = %23, %2
%9 = load i32, i32* %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = call i32 @ab_read(i32 %9, i8* %10, i32 1024)
store i32 %11, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %8
%14 = load i32, i32* %5
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = call i32 @ab_write(i32 %14, i8* %15, i64 %17)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %13
br label %24
23: ; preds = %13
br label %8
24: ; preds = %22, %8
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i32, i32* @errno
store i32 %28, i32* %3
br label %30
29: ; preds = %24
store i32 0, i32* %3
br label %30
30: ; preds = %29, %27
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [6 x i8] c"exit\0A\00"
@errno = global i32 0
define i32 @do_echo(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %8, %2
%6 = call i32 @ab_read(i32 %0, i8* %4, i32 1024)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = zext i32 %6 to i64
%10 = call i32 @ab_write(i32 %1, i8* %4, i64 %9)
%11 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %5
13: ; preds = %8, %5
%14 = icmp eq i32 %6, -1
%15 = load i32, i32* @errno
%16 = select i1 %14, i32 %15, i32 0
ret i32 %16
}