#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 */
struct timeval {
  int dummy;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
double timediff(struct timeval *, struct timeval *);
void play_with_decoder(FILE *control, FILE *status) {
  char line[4096];
  struct timeval ta, tb, tc;
  int count = 100;
  int i = 0;
  double sum = 0.;
  if ((fgets(line, sizeof(line) / sizeof(char), status)) == NULL)
    return;
  fprintf(stderr, "Got greeting: %s", line);
  while (++i <= count) {
    double ms;
    gettimeofday(&ta, NULL);
    fprintf(control, "silence\n");
    if (fgets(line, sizeof(line) / sizeof(char), status) == NULL)
      return;
    gettimeofday(&tb, NULL);
    ms = timediff(&ta, &tb);
    sum += ms;
    fprintf(stderr, "sent silcence, got %s", line);
    fprintf(stderr, "ping-pong: %f ms \n", ms);
  }
  fprintf(stderr, "mean send/receive latency: %f ms\n", sum / count);
}


x86 assembly (clang, -O0):
<play_with_decoder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x64,-0x102c(%rbp)
movl   $0x0,-0x1030(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x1038(%rbp)
ff
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1196 <play_with_decoder+0x56>
jmp    12c7 <play_with_decoder+0x187>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rdx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1030(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1030(%rbp)
cmp    -0x102c(%rbp),%eax
jg     129c <play_with_decoder+0x15c>
lea    -0x1018(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x8(%rbp),%rax
mov    %eax,%edi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1217 <play_with_decoder+0xd7>
jmp    12c7 <play_with_decoder+0x187>
lea    -0x1020(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x1018(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1060 <timediff@plt>
movsd  %xmm0,-0x1040(%rbp)
ff
movsd  -0x1040(%rbp),%xmm0
ff
addsd  -0x1038(%rbp),%xmm0
ff
movsd  %xmm0,-0x1038(%rbp)
ff
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rdx
lea    0xda9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
movsd  -0x1040(%rbp),%xmm0
ff
lea    0xda0(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
jmp    11b4 <play_with_decoder+0x74>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
movsd  -0x1038(%rbp),%xmm0
ff
cvtsi2sdl -0x102c(%rbp),%xmm1
ff
divsd  %xmm1,%xmm0
lea    0xd83(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<play_with_decoder>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    %rsi,%r15
mov    %rdi,0x10(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     126c <play_with_decoder+0x12c>
mov    0x2e65(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe82(%rip),%rsi
lea    0x30(%rsp),%r12
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    $0x64,%r14d
lea    0x28(%rsp),%rbx
lea    0x20(%rsp),%rbp
nop
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x10(%rsp),%rdi
lea    0xe53(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     126c <play_with_decoder+0x12c>
mov    %rbp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <timediff@plt>
movapd %xmm0,%xmm1
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    0x0(%r13),%edi
lea    0xe02(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%edi
lea    0xe03(%rip),%rsi
movsd  0x18(%rsp),%xmm0
mov    $0x1,%al
call   1050 <fprintf@plt>
add    $0xffffffff,%r14d
jne    11b0 <play_with_decoder+0x70>
mov    0x0(%r13),%edi
movsd  0x8(%rsp),%xmm0
divsd  0xda2(%rip),%xmm0
lea    0xde6(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
add    $0x1038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@stderr = global i32 0
@.str = [17 x i8] c"Got greeting: %s\00"
@.str.1 = [9 x i8] c"silence\0A\00"
@.str.2 = [22 x i8] c"sent silcence, got %s\00"
@.str.3 = [19 x i8] c"ping-pong: %f ms \0A\00"
@.str.4 = [34 x i8] c"mean send/receive latency: %f ms\0A\00"
define void @play_with_decoder(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
%11 = alloca double
%12 = alloca double
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 100, i32* %9
store i32 0, i32* %10
store double 0.000000e+00, double* %11
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i32* @fgets(i8* %13, i32 4096, i32* %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %2
br label %56
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %37, %18
%23 = load i32, i32* %10
%24 = add nsw i32 %23, 1
store i32 %24, i32* %10
%25 = load i32, i32* %9
%26 = icmp sle i32 %24, %25
br i1 %26, label %27, label %49
27: ; preds = %22
%28 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%29 = load i32*, i32** %3
%30 = ptrtoint i32* %29 to i32
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%33 = load i32*, i32** %4
%34 = call i32* @fgets(i8* %32, i32 4096, i32* %33)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %27
br label %56
37: ; preds = %27
%38 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%39 = call double @timediff(%struct.timeval* %6, %struct.timeval* %7)
store double %39, double* %12
%40 = load double, double* %12
%41 = load double, double* %11
%42 = fadd double %41, %40
store double %42, double* %11
%43 = load i32, i32* @stderr
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = load i32, i32* @stderr
%47 = load double, double* %12
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), double %47)
br label %22
49: ; preds = %22
%50 = load i32, i32* @stderr
%51 = load double, double* %11
%52 = load i32, i32* %9
%53 = sitofp i32 %52 to double
%54 = fdiv double %51, %53
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), double %54)
br label %56
56: ; preds = %49, %36, %17
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@stderr = global i32 0
@.str = [17 x i8] c"Got greeting: %s\00"
@.str.1 = [9 x i8] c"silence\0A\00"
@.str.2 = [22 x i8] c"sent silcence, got %s\00"
@.str.3 = [19 x i8] c"ping-pong: %f ms \0A\00"
@.str.4 = [34 x i8] c"mean send/receive latency: %f ms\0A\00"
define void @play_with_decoder(i32* %0, i32* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.timeval* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = call i32* @fgets(i8* %6, i32 4096, i32* %1)
%10 = icmp eq i32* %9, null
br i1 %10, label %37, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %6)
%14 = ptrtoint i32* %0 to i64
%15 = trunc i64 %14 to i32
br label %16
16: ; preds = %11, %23
%17 = phi i32 [ 1, %11 ], [ %31, %23 ]
%18 = phi double [ 0.000000e+00, %11 ], [ %26, %23 ]
%19 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32* @fgets(i8* %6, i32 4096, i32* %1)
%22 = icmp eq i32* %21, null
br i1 %22, label %37, label %23
23: ; preds = %16
%24 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%25 = call double @timediff(%struct.timeval* %4, %struct.timeval* %5)
%26 = fadd double %18, %25
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), double %25)
%31 = add nuw nsw i32 %17, 1
%32 = icmp eq i32 %31, 101
br i1 %32, label %33, label %16
33: ; preds = %23
%34 = load i32, i32* @stderr
%35 = fdiv double %26, 1.000000e+02
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), double %35)
br label %37
37: ; preds = %16, %2, %33
ret void
}