#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 */
scalar_t__ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int read_verify_pkt(int, char const);
int /*<<< orphan*/ send_pkt(int, char *, char const);
int setup_rx();
int setup_tx(char **);
int main(int argc, char **argv) {
  const char payload_patterns[] = "ab";
  char *ring;
  int fdr, fdt, ret = 0;
  fdr = setup_rx();
  fdt = setup_tx(&ring);
  send_pkt(fdt, ring, payload_patterns[0]);
  send_pkt(fdt, ring, payload_patterns[1]);
  ret |= read_verify_pkt(fdr, payload_patterns[0]);
  ret |= read_verify_pkt(fdr, payload_patterns[1]);
  if (close(fdt))
    error(1, errno, "close t");
  if (close(fdr))
    error(1, errno, "close r");
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe83(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe7b(%rip),%al
mov    %al,-0x11(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <setup_rx@plt>
mov    %eax,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <setup_tx@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
movsbl -0x13(%rbp),%edx
call   1060 <send_pkt@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
movsbl -0x12(%rbp),%edx
call   1060 <send_pkt@plt>
mov    -0x24(%rbp),%edi
movsbl -0x13(%rbp),%esi
call   1070 <read_verify_pkt@plt>
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
movsbl -0x12(%rbp),%esi
call   1070 <read_verify_pkt@plt>
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1217 <main+0xb7>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdf1(%rip),%rdx
call   1080 <error@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1243 <main+0xe3>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdcd(%rip),%rdx
call   1080 <error@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <setup_rx@plt>
mov    %eax,%r15d
mov    %rsp,%rdi
call   1050 <setup_tx@plt>
mov    %eax,%ebx
mov    (%rsp),%rsi
mov    %eax,%edi
mov    $0x61,%edx
call   1060 <send_pkt@plt>
mov    (%rsp),%rsi
mov    %ebx,%edi
mov    $0x62,%edx
call   1060 <send_pkt@plt>
mov    %r15d,%edi
mov    $0x61,%esi
call   1070 <read_verify_pkt@plt>
mov    %eax,%r14d
mov    %r15d,%edi
mov    $0x62,%esi
call   1070 <read_verify_pkt@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
je     11e0 <main+0x80>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
mov    %r15d,%edi
call   1040 <close@plt>
test   %rax,%rax
je     1207 <main+0xa7>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xe0b(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
or     %r14d,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.payload_patterns = [3 x i8] c"ab\00"
@errno = global i32 0
@.str = [8 x i8] c"close t\00"
@.str.1 = [8 x i8] c"close r\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [3 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [3 x i8]* %6 to i8*
store i32 0, i32* %10
%12 = call i32 (...) @setup_rx()
store i32 %12, i32* %8
%13 = call i32 @setup_tx(i8** %7)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = load i8*, i8** %7
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%17 = load i8, i8* %16
%18 = call i32 @send_pkt(i32 %14, i8* %15, i8 signext %17)
%19 = load i32, i32* %9
%20 = load i8*, i8** %7
%21 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 1
%22 = load i8, i8* %21
%23 = call i32 @send_pkt(i32 %19, i8* %20, i8 signext %22)
%24 = load i32, i32* %8
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%26 = load i8, i8* %25
%27 = call i32 @read_verify_pkt(i32 %24, i8 signext %26)
%28 = load i32, i32* %10
%29 = or i32 %28, %27
store i32 %29, i32* %10
%30 = load i32, i32* %8
%31 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 1
%32 = load i8, i8* %31
%33 = call i32 @read_verify_pkt(i32 %30, i8 signext %32)
%34 = load i32, i32* %10
%35 = or i32 %34, %33
store i32 %35, i32* %10
%36 = load i32, i32* %9
%37 = call i64 @close(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %2
%40 = load i32, i32* @errno
%41 = call i32 @error(i32 1, i32 %40, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %39, %2
%43 = load i32, i32* %8
%44 = call i64 @close(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* @errno
%48 = call i32 @error(i32 1, i32 %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %46, %42
%50 = load i32, i32* %10
ret i32 %50
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [8 x i8] c"close t\00"
@.str.1 = [8 x i8] c"close r\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i32 (...) @setup_rx()
%6 = call i32 @setup_tx(i8** %3)
%7 = load i8*, i8** %3
%8 = call i32 @send_pkt(i32 %6, i8* %7, i8 signext 97)
%9 = load i8*, i8** %3
%10 = call i32 @send_pkt(i32 %6, i8* %9, i8 signext 98)
%11 = call i32 @read_verify_pkt(i32 %5, i8 signext 97)
%12 = call i32 @read_verify_pkt(i32 %5, i8 signext 98)
%13 = call i64 @close(i32 %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i32 @error(i32 1, i32 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %2
%19 = call i64 @close(i32 %5)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = call i32 @error(i32 1, i32 %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %18
%25 = or i32 %12, %11
ret i32 %25
}