#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 {
  double tv_sec;
  double tv_usec;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ pthread_detach(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
scalar_t__ readn(int, char *, int);
scalar_t__ snprintf(char *, int, char *, long);
scalar_t__ writen(int, char *, scalar_t__);
void *uplink_thread(void *arg) {
  int conn_fd;
  char buf[4096];
  ssize_t nread, nwrite;
  struct timeval tv;
  double t1, t2;
  long throughput = 0;
  conn_fd = *((int *)arg);
  free(arg);
  pthread_detach(pthread_self());
  gettimeofday(&tv, NULL);
  t1 = tv.tv_sec + tv.tv_usec / 1000000.0;
  while ((nread = readn(conn_fd, buf, 4096)) > 0) {
    throughput += nread;
  }
  gettimeofday(&tv, NULL);
  t2 = tv.tv_sec + tv.tv_usec / 1000000.0;
  throughput = throughput * 8 / (t2 - t1);
  printf("Throughput: %ldbps\n", throughput);
  nwrite = snprintf(buf, 4096, "%ld\n", throughput);
  if (writen(conn_fd, buf, nwrite) < 0) {
    perror("writen() error");
  }
  close(conn_fd);
  return NULL;
}


x86 assembly (clang, -O0):
<uplink_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x1048(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   10a0 <pthread_self@plt>
mov    %eax,%edi
call   1090 <pthread_detach@plt>
lea    -0x1030(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
movsd  -0x1030(%rbp),%xmm0
ff
movsd  -0x1028(%rbp),%xmm1
ff
movsd  0xdfe(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x1038(%rbp)
ff
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <readn@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jle    1251 <uplink_thread+0xb1>
mov    -0x1018(%rbp),%rax
add    -0x1048(%rbp),%rax
mov    %rax,-0x1048(%rbp)
jmp    1212 <uplink_thread+0x72>
lea    -0x1030(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
movsd  -0x1030(%rbp),%xmm0
ff
movsd  -0x1028(%rbp),%xmm1
ff
movsd  0xd87(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x1040(%rbp)
ff
mov    -0x1048(%rbp),%rax
shl    $0x3,%rax
cvtsi2sd %rax,%xmm0
movsd  -0x1040(%rbp),%xmm1
ff
subsd  -0x1038(%rbp),%xmm1
ff
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x1048(%rbp)
mov    -0x1048(%rbp),%rsi
lea    0xd41(%rip),%rdi
call   1040 <printf@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x1048(%rbp),%rcx
mov    $0x1000,%esi
lea    0xd36(%rip),%rdx
call   1050 <snprintf@plt>
mov    %rax,-0x1020(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1020(%rbp),%rdx
call   10c0 <writen@plt>
cmp    $0x0,%rax
jge    131e <uplink_thread+0x17e>
lea    0xd08(%rip),%rdi
call   10b0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uplink_thread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1028,%rsp
mov    (%rdi),%r15d
call   1030 <free@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   10a0 <pthread_self@plt>
mov    %eax,%edi
call   1090 <pthread_detach@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
movsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%rsp)
movsd  0x10(%rsp),%xmm0
divsd  0xe16(%rip),%xmm0
movsd  %xmm0,0x18(%rsp)
lea    0x20(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rbx
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1070 <readn@plt>
add    %rax,%rbp
test   %rax,%rax
jg     1200 <uplink_thread+0x60>
movsd  (%rsp),%xmm0
addsd  0x18(%rsp),%xmm0
movsd  %xmm0,(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
movsd  0x10(%rsp),%xmm0
divsd  0xdbb(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
shl    $0x3,%rbx
cvtsi2sd %rbx,%xmm1
subsd  (%rsp),%xmm0
divsd  %xmm0,%xmm1
cvttsd2si %xmm1,%rbx
lea    0xd9f(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xda4(%rip),%rdx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   10c0 <writen@plt>
test   %rax,%rax
js     12b8 <uplink_thread+0x118>
mov    %r15d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd62(%rip),%rdi
call   10b0 <perror@plt>
jmp    12a0 <uplink_thread+0x100>

LLVM IR (-O0):
%struct.timeval = type { double, double }
@.str = [20 x i8] c"Throughput: %ldbps\0A\00"
@.str.1 = [5 x i8] c"%ld\0A\00"
@.str.2 = [15 x i8] c"writen() error\00"
define i8* @uplink_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca i64
%6 = alloca i64
%7 = alloca %struct.timeval
%8 = alloca double
%9 = alloca double
%10 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %10
%11 = load i8*, i8** %2
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
store i32 %13, i32* %3
%14 = load i8*, i8** %2
%15 = call i32 @free(i8* %14)
%16 = call i32 (...) @pthread_self()
%17 = call i32 @pthread_detach(i32 %16)
%18 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%20 = load double, double* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%22 = load double, double* %21
%23 = fdiv double %22, 1.000000e+06
%24 = fadd double %20, %23
store double %24, double* %8
br label %25
25: ; preds = %30, %1
%26 = load i32, i32* %3
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%28 = call i64 @readn(i32 %26, i8* %27, i32 4096)
store i64 %28, i64* %5
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i64, i64* %5
%32 = load i64, i64* %10
%33 = add nsw i64 %32, %31
store i64 %33, i64* %10
br label %25
34: ; preds = %25
%35 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%37 = load double, double* %36
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%39 = load double, double* %38
%40 = fdiv double %39, 1.000000e+06
%41 = fadd double %37, %40
store double %41, double* %9
%42 = load i64, i64* %10
%43 = mul nsw i64 %42, 8
%44 = sitofp i64 %43 to double
%45 = load double, double* %9
%46 = load double, double* %8
%47 = fsub double %45, %46
%48 = fdiv double %44, %47
%49 = fptosi double %48 to i64
store i64 %49, i64* %10
%50 = load i64, i64* %10
%51 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %50)
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%53 = load i64, i64* %10
%54 = call i64 @snprintf(i8* %52, i32 4096, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %53)
store i64 %54, i64* %6
%55 = load i32, i32* %3
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%57 = load i64, i64* %6
%58 = call i64 @writen(i32 %55, i8* %56, i64 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %34
%61 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %62
62: ; preds = %60, %34
%63 = load i32, i32* %3
%64 = call i32 @close(i32 %63)
ret i8* null
}

LLVM IR (-O2):
%struct.timeval = type { double, double }
@.str = [20 x i8] c"Throughput: %ldbps\0A\00"
@.str.1 = [5 x i8] c"%ld\0A\00"
@.str.2 = [15 x i8] c"writen() error\00"
define noalias i8* @uplink_thread(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = alloca %struct.timeval
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast i8* %0 to i32*
%7 = load i32, i32* %6
%8 = tail call i32 @free(i8* %0)
%9 = tail call i32 (...) @pthread_self()
%10 = tail call i32 @pthread_detach(i32 %9)
%11 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%13 = load double, double* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%15 = load double, double* %14
%16 = fdiv double %15, 1.000000e+06
br label %17
17: ; preds = %17, %1
%18 = phi i64 [ 0, %1 ], [ %21, %17 ]
%19 = call i64 @readn(i32 %7, i8* %4, i32 4096)
%20 = icmp sgt i64 %19, 0
%21 = add nsw i64 %19, %18
br i1 %20, label %17, label %22
22: ; preds = %17
%23 = fadd double %13, %16
%24 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%25 = load double, double* %12
%26 = load double, double* %14
%27 = fdiv double %26, 1.000000e+06
%28 = fadd double %25, %27
%29 = shl nsw i64 %18, 3
%30 = sitofp i64 %29 to double
%31 = fsub double %28, %23
%32 = fdiv double %30, %31
%33 = fptosi double %32 to i64
%34 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %33)
%35 = call i64 @snprintf(i8* %4, i32 4096, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %33)
%36 = call i64 @writen(i32 %7, i8* %4, i64 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %22
%39 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %22
%41 = call i32 @close(i32 %7)
ret i8* null
}