#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 /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ ece391_fdputs(int, int /*<<< orphan*/ *);
int ece391_read(int, int /*<<< orphan*/ *, int);
int ece391_write(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int err_stdin_out(void) {
  int fail = 0;
  uint8_t buf[32];
  if (-1 != ece391_write(0, buf, 31)) {
    ece391_fdputs(1, (uint8_t *)"write to stdin fail\n");
    fail = 2;
  }
  if (-1 != ece391_read(1, buf, 31)) {
    ece391_fdputs(1, (uint8_t *)"read from stdout fail\n");
    fail = 2;
  }
  if (fail) {
    ece391_fdputs(1, (uint8_t *)"err_stdin_out: FAIL\n");
  } else {
    ece391_fdputs(1, (uint8_t *)"err_stdin_out: PASS\n");
  }
  return fail;
}


x86 assembly (clang, -O0):
<err_stdin_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x90(%rbp),%rsi
xor    %edi,%edi
mov    $0x1f,%edx
call   1050 <ece391_write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     117c <err_stdin_out+0x4c>
mov    $0x1,%edi
lea    0xe90(%rip),%rsi
call   1030 <ece391_fdputs@plt>
movl   $0x2,-0x4(%rbp)
lea    -0x90(%rbp),%rsi
mov    $0x1,%edi
mov    $0x1f,%edx
call   1040 <ece391_read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     11b9 <err_stdin_out+0x89>
mov    $0x1,%edi
lea    0xe68(%rip),%rsi
call   1030 <ece391_fdputs@plt>
movl   $0x2,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11d9 <err_stdin_out+0xa9>
mov    $0x1,%edi
lea    0xe5d(%rip),%rsi
call   1030 <ece391_fdputs@plt>
jmp    11ea <err_stdin_out+0xba>
mov    $0x1,%edi
lea    0xe5c(%rip),%rsi
call   1030 <ece391_fdputs@plt>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<err_stdin_out>:
push   %rbx
sub    $0x80,%rsp
xor    %ebx,%ebx
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x1f,%edx
call   1050 <ece391_write@plt>
cmp    $0xffffffff,%eax
je     1178 <err_stdin_out+0x48>
lea    0xeab(%rip),%rsi
mov    $0x1,%edi
call   1030 <ece391_fdputs@plt>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    $0x1f,%edx
call   1040 <ece391_read@plt>
cmp    $0xffffffff,%eax
jne    118f <err_stdin_out+0x5f>
jmp    11a0 <err_stdin_out+0x70>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    $0x1f,%edx
call   1040 <ece391_read@plt>
cmp    $0xffffffff,%eax
je     11c1 <err_stdin_out+0x91>
lea    0xe7f(%rip),%rsi
mov    $0x1,%edi
call   1030 <ece391_fdputs@plt>
mov    $0x2,%ebx
lea    0xe80(%rip),%rsi
mov    $0x1,%edi
call   1030 <ece391_fdputs@plt>
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
ret
lea    0xe79(%rip),%rsi
jmp    11ac <err_stdin_out+0x7c>

LLVM IR (-O0):
@.str = [21 x i8] c"write to stdin fail\0A\00"
@.str.1 = [23 x i8] c"read from stdout fail\0A\00"
@.str.2 = [21 x i8] c"err_stdin_out: FAIL\0A\00"
@.str.3 = [21 x i8] c"err_stdin_out: PASS\0A\00"
define i32 @err_stdin_out() {
%1 = alloca i32
%2 = alloca [32 x i32]
store i32 0, i32* %1
%3 = getelementptr inbounds [32 x i32], [32 x i32]* %2, i64 0, i64 0
%4 = call i32 @ece391_write(i32 0, i32* %3, i32 31)
%5 = icmp ne i32 -1, %4
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([21 x i8]* @.str to i32*))
store i32 2, i32* %1
br label %8
8: ; preds = %6, %0
%9 = getelementptr inbounds [32 x i32], [32 x i32]* %2, i64 0, i64 0
%10 = call i32 @ece391_read(i32 1, i32* %9, i32 31)
%11 = icmp ne i32 -1, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([23 x i8]* @.str.1 to i32*))
store i32 2, i32* %1
br label %14
14: ; preds = %12, %8
%15 = load i32, i32* %1
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([21 x i8]* @.str.2 to i32*))
br label %21
19: ; preds = %14
%20 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([21 x i8]* @.str.3 to i32*))
br label %21
21: ; preds = %19, %17
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [21 x i8] c"write to stdin fail\0A\00"
@.str.1 = [23 x i8] c"read from stdout fail\0A\00"
@.str.2 = [21 x i8] c"err_stdin_out: FAIL\0A\00"
@.str.3 = [21 x i8] c"err_stdin_out: PASS\0A\00"
define i32 @err_stdin_out() {
%1 = alloca [32 x i32]
%2 = bitcast [32 x i32]* %1 to i8*
%3 = getelementptr inbounds [32 x i32], [32 x i32]* %1, i64 0, i64 0
%4 = call i32 @ece391_write(i32 0, i32* %3, i32 31)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @ece391_read(i32 1, i32* %3, i32 31)
%8 = icmp eq i32 %7, -1
br i1 %8, label %15, label %13
9: ; preds = %0
%10 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([21 x i8]* @.str to i32*))
%11 = call i32 @ece391_read(i32 1, i32* %3, i32 31)
%12 = icmp eq i32 %11, -1
br i1 %12, label %15, label %13
13: ; preds = %9, %6
%14 = call i32 @ece391_fdputs(i32 1, i32* bitcast ([23 x i8]* @.str.1 to i32*))
br label %15
15: ; preds = %6, %13, %9
%16 = phi i32* [ bitcast ([21 x i8]* @.str.2 to i32*), %9 ], [ bitcast ([21 x i8]* @.str.2 to i32*), %13 ], [ bitcast ([21 x i8]* @.str.3 to i32*), %6 ]
%17 = phi i32 [ 2, %9 ], [ 2, %13 ], [ 0, %6 ]
%18 = call i32 @ece391_fdputs(i32 1, i32* %16)
ret i32 %17
}