#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*/ close(int);
int read(int, char *, int);
int sync_pipe_wait(int fd[]) {
  char buf;
  int r;
  if (fd[1] != -1) {
    close(fd[1]);
    fd[1] = -1;
  }
  r = read(fd[0], &buf, 1);
  if ((r != 1) || (buf != 'A'))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<sync_pipe_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,0x4(%rax)
je     1151 <sync_pipe_wait+0x31>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    117f <sync_pipe_wait+0x5f>
movsbl -0x11(%rbp),%eax
cmp    $0x41,%eax
je     118b <sync_pipe_wait+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <sync_pipe_wait+0x72>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sync_pipe_wait>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x4(%rdi),%edi
cmp    $0xffffffff,%edi
je     113c <sync_pipe_wait+0x1c>
call   1030 <close@plt>
movl   $0xffffffff,0x4(%rbx)
mov    (%rbx),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
setne  %al
cmpb   $0x41,0xf(%rsp)
setne  %cl
or     %al,%cl
movzbl %cl,%eax
neg    %eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @sync_pipe_wait(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = getelementptr inbounds i32, i32* %6, i64 1
%8 = load i32, i32* %7
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = getelementptr inbounds i32, i32* %11, i64 1
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = load i32*, i32** %3
%16 = getelementptr inbounds i32, i32* %15, i64 1
store i32 -1, i32* %16
br label %17
17: ; preds = %10, %1
%18 = load i32*, i32** %3
%19 = getelementptr inbounds i32, i32* %18, i64 0
%20 = load i32, i32* %19
%21 = call i32 @read(i32 %20, i8* %4, i32 1)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 1
br i1 %23, label %28, label %24
24: ; preds = %17
%25 = load i8, i8* %4
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 65
br i1 %27, label %28, label %29
28: ; preds = %24, %17
store i32 -1, i32* %2
br label %30
29: ; preds = %24
store i32 0, i32* %2
br label %30
30: ; preds = %29, %28
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
define i32 @sync_pipe_wait(i32* nocapture %0) {
%2 = alloca i8
%3 = getelementptr inbounds i32, i32* %0, i64 1
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, -1
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 @close(i32 %4)
store i32 -1, i32* %3
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* %0
%10 = call i32 @read(i32 %9, i8* %2, i32 1)
%11 = icmp ne i32 %10, 1
%12 = load i8, i8* %2
%13 = icmp ne i8 %12, 65
%14 = select i1 %11, i1 true, i1 %13
%15 = sext i1 %14 to i32
ret i32 %15
}