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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, int, int /*<<< orphan*/);
size_t fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main() {
  char buf[1024];
  size_t nBytes;
  int idx;
  for (idx = 0; idx < 10; idx++) {
    memset(buf, 0, sizeof(buf));
    nBytes = fread(buf, 1, 5, stdin);
    fprintf(stderr, "pong process: received \"%s\"\n", buf);
    if (nBytes != 5) {
      fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
              nBytes);
      exit(1);
    }
    if (strcmp(buf, "ping") != 0) {
      fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n", buf);
      exit(1);
    }
    strcpy(buf, "pong");
    fprintf(stderr, "pong process: sending \"%s\"\n", buf);
    nBytes = fwrite(buf, 1, 5, stdout);
    if (nBytes != 5) {
      fprintf(stderr, "pong process: fwrite failed\n");
      exit(1);
    }
    fflush(stdout);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x41c(%rbp)
cmpl   $0xa,-0x41c(%rbp)
jge    1323 <main+0x1a3>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x5,%edx
call   1040 <fread@plt>
mov    %rax,-0x418(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
cmpq   $0x5,-0x418(%rbp)
05
je     1236 <main+0xb6>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    -0x418(%rbp),%rdx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x410(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     127b <main+0xfb>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x410(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2d16(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x5,%edx
call   10a0 <fwrite@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x5,-0x418(%rbp)
05
je     1301 <main+0x181>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    119c <main+0x1c>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    $0xa,%r14d
mov    %rsp,%rbx
mov    0x2e37(%rip),%r15
lea    0xedc(%rip),%r13
mov    0x2e21(%rip),%r12
nop
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
call   1040 <fread@plt>
mov    %rax,%rbp
mov    (%r15),%edi
lea    0xe19(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
cmp    $0x5,%rbp
jne    126d <main+0xed>
mov    %rbx,%rdi
lea    0xe4d(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    127c <main+0xfc>
mov    %rbx,%rdi
lea    0xe6a(%rip),%rsi
call   1030 <strcpy@plt>
mov    (%r15),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%r12),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
call   10a0 <fwrite@plt>
cmp    $0x5,%rax
jne    129a <main+0x11a>
mov    (%r12),%edi
call   1080 <fflush@plt>
add    $0xffffffff,%r14d
jne    11b0 <main+0x30>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%edi
lea    0xda6(%rip),%rsi
mov    %rbp,%rdx
jmp    1289 <main+0x109>
mov    (%r15),%edi
lea    0xdcd(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    (%r15),%edi
lea    0xdfc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"pong process: received \22%s\22\0A\00"
@.str.1 = [49 x i8] c"pong process: expected 5 bytes but got %d bytes\0A\00"
@.str.2 = [5 x i8] c"ping\00"
@.str.3 = [44 x i8] c"pong process: expected \22ping\22 but got \22%s\22\0A\00"
@.str.4 = [5 x i8] c"pong\00"
@.str.5 = [28 x i8] c"pong process: sending \22%s\22\0A\00"
@stdout = global i32 0
@.str.6 = [29 x i8] c"pong process: fwrite failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
br label %5
5: ; preds = %51, %0
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 10
br i1 %7, label %8, label %54
8: ; preds = %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 1024)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%12 = load i32, i32* @stdin
%13 = call i64 @fread(i8* %11, i32 1, i32 5, i32 %12)
store i64 %13, i64* %3
%14 = load i32, i32* @stderr
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i64, i64* %3
%18 = icmp ne i64 %17, 5
br i1 %18, label %19, label %24
19: ; preds = %8
%20 = load i32, i32* @stderr
%21 = load i64, i64* %3
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i64 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %8
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %24
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%35 = call i32 @strcpy(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%40 = load i32, i32* @stdout
%41 = call i64 @fwrite(i8* %39, i32 1, i32 5, i32 %40)
store i64 %41, i64* %3
%42 = load i64, i64* %3
%43 = icmp ne i64 %42, 5
br i1 %43, label %44, label %48
44: ; preds = %33
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %33
%49 = load i32, i32* @stdout
%50 = call i32 @fflush(i32 %49)
br label %51
51: ; preds = %48
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
store i32 %53, i32* %4
br label %5
54: ; preds = %5
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"pong process: received \22%s\22\0A\00"
@.str.1 = [49 x i8] c"pong process: expected 5 bytes but got %d bytes\0A\00"
@.str.2 = [5 x i8] c"ping\00"
@.str.3 = [44 x i8] c"pong process: expected \22ping\22 but got \22%s\22\0A\00"
@.str.4 = [5 x i8] c"pong\00"
@.str.5 = [28 x i8] c"pong process: sending \22%s\22\0A\00"
@stdout = global i32 0
@.str.6 = [29 x i8] c"pong process: fwrite failed\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %33
%4 = phi i32 [ 0, %0 ], [ %36, %33 ]
%5 = call i32 @memset(i8* %2, i32 0, i32 1024)
%6 = load i32, i32* @stdin
%7 = call i64 @fread(i8* %2, i32 1, i32 5, i32 %6)
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %2)
%10 = icmp eq i64 %7, 5
br i1 %10, label %15, label %11
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i64 %7)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%26 = load i32, i32* @stdout
%27 = call i64 @fwrite(i8* %2, i32 1, i32 5, i32 %26)
%28 = icmp eq i64 %27, 5
br i1 %28, label %33, label %29
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %22
%34 = load i32, i32* @stdout
%35 = call i32 @fflush(i32 %34)
%36 = add nuw nsw i32 %4, 1
%37 = icmp eq i32 %36, 10
br i1 %37, label %38, label %3
38: ; preds = %33
ret i32 0
}