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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int, ...);
int receive_data(int, char *, int, int);
int send_data(int, char *, int);
int send_pingpong(int fd, char *tbuf) {
  int i, ret;
  char rbuf[64];
  for (i = 2; i < 64; i++) {
    tbuf[0] = i;
    ret = send_data(fd, tbuf, i);
    if (ret > 0) {
      ret = receive_data(fd, rbuf, sizeof(rbuf), 1);
      if (ret > 0) {
        if (ret == i)
          printf("Ping Pong %d bytes ok\n", i);
        else
          printf("Wrong size! send %d receive %d\n", i, ret);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_pingpong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2,-0x14(%rbp)
cmpl   $0x40,-0x14(%rbp)
jge    11ed <send_pingpong+0xbd>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <send_data@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11da <send_pingpong+0xaa>
mov    -0x4(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
mov    $0x1,%ecx
call   1030 <receive_data@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11d5 <send_pingpong+0xa5>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11bc <send_pingpong+0x8c>
mov    -0x14(%rbp),%esi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d0 <send_pingpong+0xa0>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d5 <send_pingpong+0xa5>
jmp    11da <send_pingpong+0xaa>
jmp    11df <send_pingpong+0xaf>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <send_pingpong+0x16>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_pingpong>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    $0x2,%ebp
mov    %rsp,%r15
lea    0xead(%rip),%r12
lea    0xebd(%rip),%r13
jmp    1178 <send_pingpong+0x48>
mov    %r13,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    $0x40,%ebp
je     11b7 <send_pingpong+0x87>
mov    %bpl,(%rbx)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <send_data@plt>
test   %eax,%eax
jle    1170 <send_pingpong+0x40>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x40,%edx
mov    $0x1,%ecx
call   1030 <receive_data@plt>
test   %eax,%eax
jle    1170 <send_pingpong+0x40>
cmp    %eax,%ebp
jne    115c <send_pingpong+0x2c>
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1170 <send_pingpong+0x40>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Ping Pong %d bytes ok\0A\00"
@.str.1 = [32 x i8] c"Wrong size! send %d receive %d\0A\00"
define i32 @send_pingpong(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [64 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 2, i32* %5
br label %8
8: ; preds = %42, %2
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 64
br i1 %10, label %11, label %45
11: ; preds = %8
%12 = load i32, i32* %5
%13 = trunc i32 %12 to i8
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 %13, i8* %15
%16 = load i32, i32* %3
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = call i32 @send_data(i32 %16, i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %41
22: ; preds = %11
%23 = load i32, i32* %3
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%25 = call i32 @receive_data(i32 %23, i8* %24, i32 64, i32 1)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %22
%29 = load i32, i32* %6
%30 = load i32, i32* %5
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* %5
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %33)
br label %39
35: ; preds = %28
%36 = load i32, i32* %5
%37 = load i32, i32* %6
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %37)
br label %39
39: ; preds = %35, %32
br label %40
40: ; preds = %39, %22
br label %41
41: ; preds = %40, %11
br label %42
42: ; preds = %41
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %8
45: ; preds = %8
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"Ping Pong %d bytes ok\0A\00"
@.str.1 = [32 x i8] c"Wrong size! send %d receive %d\0A\00"
define i32 @send_pingpong(i32 %0, i8* %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %2, %19
%6 = phi i32 [ 2, %2 ], [ %20, %19 ]
%7 = trunc i32 %6 to i8
store i8 %7, i8* %1
%8 = call i32 @send_data(i32 %0, i8* %1, i32 %6)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %5
%11 = call i32 @receive_data(i32 %0, i8* %4, i32 64, i32 1)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = icmp eq i32 %11, %6
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %19
17: ; preds = %13
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %11)
br label %19
19: ; preds = %5, %15, %17, %10
%20 = add nuw nsw i32 %6, 1
%21 = icmp eq i32 %20, 64
br i1 %21, label %22, label %5
22: ; preds = %19
ret i32 0
}