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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, int *, int, int /*<<< orphan*/);
int send(int, int *, int, int /*<<< orphan*/);
int startup(int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  if (argc < 3) {
    return 0;
  }
  int fd = startup(atoi(argv[1]));
  FILE *file = fopen(argv[2], "r");
  if (file == NULL) {
    printf("open file: %s failed.", argv[2]);
    return -1;
  }
  for (;;) {
    int ch = getc(file);
    if (ch == EOF) {
      break;
    }
    assert(1 == send(fd, &ch, 1, 0));
    printf("%c", ch);
    fflush(stdout);
    usleep(20 * 1000);
  }
  for (;;) {
    int ch;
    int len = recv(fd, &ch, 1, 0);
    if (len != 1) {
      break;
    }
    printf("%c", ch);
    fflush(stdout);
  }
  close(fd);
  fclose(file);
  return 0;
}


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)
cmpl   $0x3,-0x8(%rbp)
jge    11ec <main+0x2c>
movl   $0x0,-0x4(%rbp)
jmp    132d <main+0x16d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   10a0 <startup@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdee(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1248 <main+0x88>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132d <main+0x16d>
jmp    124d <main+0x8d>
mov    -0x20(%rbp),%rdi
call   10d0 <getc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%eax
jne    1270 <main+0xb0>
jmp    12c7 <main+0x107>
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x24(%rbp),%esi
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x4e20,%edi
call   10e0 <usleep@plt>
jmp    124d <main+0x8d>
jmp    12cc <main+0x10c>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
je     12f1 <main+0x131>
jmp    1315 <main+0x155>
mov    -0x28(%rbp),%esi
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    12cc <main+0x10c>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %r15d,%r15d
cmp    $0x3,%edi
jl     1311 <main+0x141>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   10b0 <startup@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
lea    0xdf8(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12f9 <main+0x129>
mov    %rax,%r14
mov    %rax,%rdi
call   10e0 <getc@plt>
mov    %eax,0x8(%rsp)
mov    0x2dbc(%rip),%r12
cmp    (%r12),%eax
je     128c <main+0xbc>
lea    0x8(%rsp),%r15
mov    0x2d9a(%rip),%rbp
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1090 <assert@plt>
mov    0x8(%rsp),%edi
call   1040 <putchar@plt>
mov    0x0(%rbp),%edi
call   10a0 <fflush@plt>
mov    $0x4e20,%edi
call   10f0 <usleep@plt>
mov    %r14,%rdi
call   10e0 <getc@plt>
mov    %eax,0x8(%rsp)
cmp    (%r12),%eax
jne    1240 <main+0x70>
lea    0xc(%rsp),%rsi
xor    %r15d,%r15d
mov    %ebx,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x1,%eax
jne    12e8 <main+0x118>
mov    0x2d2a(%rip),%r12
lea    0xc(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%edi
call   1040 <putchar@plt>
mov    (%r12),%edi
call   10a0 <fflush@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x1,%eax
je     12c0 <main+0xf0>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    1311 <main+0x141>
mov    0x10(%rbp),%rsi
lea    0xcfe(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"open file: %s failed.\00"
@EOF = global i32 0
@.str.2 = [3 x i8] c"%c\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%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 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %3
br label %67
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
%19 = call i32 @startup(i32 %18)
store i32 %19, i32* %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %31
26: ; preds = %14
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 -1, i32* %3
br label %67
31: ; preds = %14
br label %32
32: ; preds = %39, %31
%33 = load i32*, i32** %7
%34 = call i32 @getc(i32* %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = load i32, i32* @EOF
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %32
br label %50
39: ; preds = %32
%40 = load i32, i32* %6
%41 = call i32 @send(i32 %40, i32* %8, i32 1, i32 0)
%42 = icmp eq i32 1, %41
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i32, i32* %8
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = load i32, i32* @stdout
%48 = call i32 @fflush(i32 %47)
%49 = call i32 @usleep(i32 20000)
br label %32
50: ; preds = %38
br label %51
51: ; preds = %57, %50
%52 = load i32, i32* %6
%53 = call i32 @recv(i32 %52, i32* %9, i32 1, i32 0)
store i32 %53, i32* %10
%54 = load i32, i32* %10
%55 = icmp ne i32 %54, 1
br i1 %55, label %56, label %57
56: ; preds = %51
br label %62
57: ; preds = %51
%58 = load i32, i32* %9
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %58)
%60 = load i32, i32* @stdout
%61 = call i32 @fflush(i32 %60)
br label %51
62: ; preds = %56
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
%65 = load i32*, i32** %7
%66 = call i32 @fclose(i32* %65)
store i32 0, i32* %3
br label %67
67: ; preds = %62, %26, %13
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"open file: %s failed.\00"
@EOF = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = icmp slt i32 %0, 3
br i1 %5, label %50, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = tail call i32 @startup(i32 %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %20, label %15
15: ; preds = %6
%16 = bitcast i32* %3 to i8*
%17 = call i32 @getc(i32* %13)
store i32 %17, i32* %3
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %36, label %23
20: ; preds = %6
%21 = load i8*, i8** %11
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %50
23: ; preds = %15, %23
%24 = call i32 @send(i32 %10, i32* %3, i32 1, i32 0)
%25 = icmp eq i32 %24, 1
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32, i32* %3
%29 = call i32 @putchar(i32 %28)
%30 = load i32, i32* @stdout
%31 = call i32 @fflush(i32 %30)
%32 = call i32 @usleep(i32 20000)
%33 = call i32 @getc(i32* %13)
store i32 %33, i32* %3
%34 = load i32, i32* @EOF
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %23
36: ; preds = %23, %15
%37 = bitcast i32* %4 to i8*
%38 = call i32 @recv(i32 %10, i32* %4, i32 1, i32 0)
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %47
40: ; preds = %36, %40
%41 = load i32, i32* %4
%42 = call i32 @putchar(i32 %41)
%43 = load i32, i32* @stdout
%44 = call i32 @fflush(i32 %43)
%45 = call i32 @recv(i32 %10, i32* %4, i32 1, i32 0)
%46 = icmp eq i32 %45, 1
br i1 %46, label %40, label %47
47: ; preds = %40, %36
%48 = call i32 @close(i32 %10)
%49 = call i32 @fclose(i32* %13)
br label %50
50: ; preds = %20, %47, %2
%51 = phi i32 [ 0, %2 ], [ -1, %20 ], [ 0, %47 ]
ret i32 %51
}