#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*/ off_t;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int check_size(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int read(int const, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int const, char *, int);
void do_test(const int fdin, const int fdout, const char *filename) {
  for (;;) {
    int n;
    int ret;
    char cmd[32];
    if ((n = read(fdin, cmd, sizeof(cmd))) < 1) {
      cmd[0] = 'R';
      if (write(fdout, cmd, 1) < 0)
        fprintf(stderr, "write to pipe failed: %s\n", strerror(errno));
      exit(0);
    }
    if (cmd[0] == 'q') {
      exit(0);
    }
    if (cmd[0] == 't') {
      int ret;
      off_t sz;
      sscanf(cmd + 1, "%zd", &sz);
      ret = check_size(filename, sz);
      switch (ret) {
      case (0):
        cmd[0] = 'p';
        if (write(fdout, cmd, 1) < 0)
          fprintf(stderr, "write to pipe failed: %s\n", strerror(errno));
        break;
      case (-1):
        cmd[0] = 'S';
        ret = write(fdout, cmd, 1);
        exit(ret);
        break;
      case (-2):
        cmd[0] = 'f';
        ret = write(fdout, cmd, 1);
        exit(ret);
      default:
        cmd[0] = 'U';
        ret = write(fdout, cmd, 1);
        exit(ret);
      }
    }
  }
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x1,%eax
jge    11f1 <do_test+0x81>
movb   $0x52,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11ea <do_test+0x7a>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xe1b(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
movsbl -0x40(%rbp),%eax
cmp    $0x71,%eax
jne    1205 <do_test+0x95>
xor    %edi,%edi
call   1080 <exit@plt>
movsbl -0x40(%rbp),%eax
cmp    $0x74,%eax
jne    131f <do_test+0x1af>
lea    -0x3f(%rbp),%rdi
lea    0xdfd(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%esi
call   1050 <check_size@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x50(%rbp)
sub    $0xfffffffe,%eax
je     12da <do_test+0x16a>
jmp    1249 <do_test+0xd9>
mov    -0x50(%rbp),%eax
sub    $0xffffffff,%eax
je     12ba <do_test+0x14a>
jmp    125a <do_test+0xea>
mov    -0x50(%rbp),%eax
test   %eax,%eax
jne    12fa <do_test+0x18a>
jmp    126a <do_test+0xfa>
movb   $0x70,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12b5 <do_test+0x145>
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xd50(%rip),%rsi
call   1060 <fprintf@plt>
jmp    131a <do_test+0x1aa>
movb   $0x53,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1080 <exit@plt>
movb   $0x66,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1080 <exit@plt>
movb   $0x55,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1080 <exit@plt>
jmp    131f <do_test+0x1af>
jmp    1182 <do_test+0x12>

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r13
mov    %esi,%r12d
mov    %edi,%r14d
lea    0x10(%rsp),%rsi
mov    $0x20,%edx
call   1040 <read@plt>
test   %eax,%eax
jg     11e5 <do_test+0x75>
movb   $0x52,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11de <do_test+0x6e>
mov    0x2e23(%rip),%rax
mov    (%rax),%ebx
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0x11(%rsp),%rbx
lea    0xe29(%rip),%rbp
lea    0xc(%rsp),%r15
mov    %r14d,0x8(%rsp)
jmp    121a <do_test+0xaa>
nopl   (%rax)
cmp    $0x71,%al
je     11de <do_test+0x6e>
mov    %r14d,%edi
lea    0x10(%rsp),%rsi
mov    $0x20,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    119a <do_test+0x2a>
movzbl 0x10(%rsp),%eax
cmp    $0x74,%al
jne    1200 <do_test+0x90>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1070 <sscanf@plt>
mov    0xc(%rsp),%esi
mov    %r13,%rdi
call   1050 <check_size@plt>
test   %eax,%eax
jne    12a3 <do_test+0x133>
movb   $0x70,0x10(%rsp)
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1204 <do_test+0x94>
mov    %r15,%r14
mov    %r13,%r15
mov    %r12d,%r13d
mov    0x2d73(%rip),%rax
mov    (%rax),%r12d
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %r12d,%edi
mov    %r13d,%r12d
mov    %r15,%r13
mov    %r14,%r15
mov    0x8(%rsp),%r14d
lea    0xd6a(%rip),%rsi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    1204 <do_test+0x94>
cmp    $0xfffffffe,%eax
je     12b4 <do_test+0x144>
cmp    $0xffffffff,%eax
jne    12bb <do_test+0x14b>
movb   $0x53,0x10(%rsp)
jmp    12c0 <do_test+0x150>
movb   $0x66,0x10(%rsp)
jmp    12c0 <do_test+0x150>
movb   $0x55,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"write to pipe failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"%zd\00"
define void @do_test(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [32 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
br label %12
12: ; preds = %87, %3
%13 = load i32, i32* %4
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 32)
store i32 %15, i32* %7
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %30
17: ; preds = %12
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
store i8 82, i8* %18
%19 = load i32, i32* %5
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%21 = call i64 @write(i32 %19, i8* %20, i32 1)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %23, %17
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %12
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 113
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %30
%38 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 116
br i1 %41, label %42, label %87
42: ; preds = %37
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%44 = getelementptr inbounds i8, i8* %43, i64 1
%45 = call i32 @sscanf(i8* %44, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %11)
%46 = load i8*, i8** %6
%47 = load i32, i32* %11
%48 = call i32 @check_size(i8* %46, i32 %47)
store i32 %48, i32* %10
%49 = load i32, i32* %10
switch i32 %49, label %78 [
i32 0, label %50
i32 -1, label %62
i32 -2, label %70
]
50: ; preds = %42
%51 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
store i8 112, i8* %51
%52 = load i32, i32* %5
%53 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%54 = call i64 @write(i32 %52, i8* %53, i32 1)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %50
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %56, %50
br label %86
62: ; preds = %42
%63 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
store i8 83, i8* %63
%64 = load i32, i32* %5
%65 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%66 = call i64 @write(i32 %64, i8* %65, i32 1)
%67 = trunc i64 %66 to i32
store i32 %67, i32* %10
%68 = load i32, i32* %10
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %42
%71 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
store i8 102, i8* %71
%72 = load i32, i32* %5
%73 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%74 = call i64 @write(i32 %72, i8* %73, i32 1)
%75 = trunc i64 %74 to i32
store i32 %75, i32* %10
%76 = load i32, i32* %10
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %42
%79 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
store i8 85, i8* %79
%80 = load i32, i32* %5
%81 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%82 = call i64 @write(i32 %80, i8* %81, i32 1)
%83 = trunc i64 %82 to i32
store i32 %83, i32* %10
%84 = load i32, i32* %10
%85 = call i32 @exit(i32 %84)
unreachable
86: ; preds = %61
br label %87
87: ; preds = %86, %37
br label %12
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"write to pipe failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"%zd\00"
define void @do_test(i32 %0, i32 %1, i8* %2) {
%4 = alloca [32 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = call i32 @read(i32 %0, i8* %6, i32 32)
%8 = icmp slt i32 %7, 1
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = bitcast i32* %5 to i8*
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 1
br label %22
12: ; preds = %51, %3
store i8 82, i8* %6
%13 = call i64 @write(i32 %1, i8* %6, i32 1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %15, %12
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %9, %51
%23 = load i8, i8* %6
switch i8 %23, label %51 [
i8 113, label %24
i8 116, label %26
]
24: ; preds = %22
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %22
%27 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%28 = load i32, i32* %5
%29 = call i32 @check_size(i8* %2, i32 %28)
switch i32 %29, label %46 [
i32 0, label %30
i32 -1, label %38
i32 -2, label %42
]
30: ; preds = %26
store i8 112, i8* %6
%31 = call i64 @write(i32 %1, i8* %6, i32 1)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %36)
br label %50
38: ; preds = %26
store i8 83, i8* %6
%39 = call i64 @write(i32 %1, i8* %6, i32 1)
%40 = trunc i64 %39 to i32
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %26
store i8 102, i8* %6
%43 = call i64 @write(i32 %1, i8* %6, i32 1)
%44 = trunc i64 %43 to i32
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %26
store i8 85, i8* %6
%47 = call i64 @write(i32 %1, i8* %6, i32 1)
%48 = trunc i64 %47 to i32
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %30, %33
br label %51
51: ; preds = %22, %50
%52 = call i32 @read(i32 %0, i8* %6, i32 32)
%53 = icmp slt i32 %52, 1
br i1 %53, label %12, label %22
}