#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 int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ pthread_detach(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
int readn(int, char *, int);
scalar_t__ shutdown(int, int /*<<< orphan*/);
scalar_t__ writen(int, char *, int);
void *downlink_thread(void *arg) {
  int conn_fd;
  char buf[4096];
  ssize_t nread;
  struct timeval tv;
  double t1, t2;
  conn_fd = *((int *)arg);
  free(arg);
  pthread_detach(pthread_self());
  memset(buf, '\0', 4096);
  gettimeofday(&tv, NULL);
  t1 = tv.tv_sec + tv.tv_usec / 1000000.0;
  t2 = t1;
  while ((t2 - t1) < 60) {
    if (writen(conn_fd, buf, 4096) < 0) {
      perror("writen() error");
      break;
    }
    gettimeofday(&tv, NULL);
    t2 = tv.tv_sec + tv.tv_usec / 1000000.0;
  }
  if (shutdown(conn_fd, SHUT_WR) < 0) {
    perror("shutdown() error");
  }
  if ((nread = readn(conn_fd, buf, 4096)) < 0) {
    perror("readn() error");
  } else {
    buf[nread - 1] = '\0';
    printf("Throughput: %sbps\n", buf);
  }
  close(conn_fd);
  return NULL;
}


x86 assembly (clang, -O0):
<downlink_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   10b0 <pthread_self@plt>
mov    %eax,%edi
call   10a0 <pthread_detach@plt>
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
lea    -0x1028(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
movsd  -0x1028(%rbp),%xmm0
ff
movsd  -0x1020(%rbp),%xmm1
ff
movsd  0xde6(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x1030(%rbp)
ff
movsd  -0x1030(%rbp),%xmm0
ff
movsd  %xmm0,-0x1038(%rbp)
ff
movsd  -0x1038(%rbp),%xmm1
ff
subsd  -0x1030(%rbp),%xmm1
ff
movsd  0xdb6(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    12c8 <downlink_thread+0x118>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   10d0 <writen@plt>
cmp    $0x0,%rax
jge    128b <downlink_thread+0xdb>
lea    0xd8f(%rip),%rdi
call   10c0 <perror@plt>
jmp    12c8 <downlink_thread+0x118>
lea    -0x1028(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
movsd  -0x1028(%rbp),%xmm0
ff
movsd  -0x1020(%rbp),%xmm1
ff
movsd  0xd4d(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x1038(%rbp)
ff
jmp    123a <downlink_thread+0x8a>
mov    -0xc(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
cmp    $0x0,%rax
jge    12ef <downlink_thread+0x13f>
lea    0xd35(%rip),%rdi
call   10c0 <perror@plt>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <readn@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jge    1323 <downlink_thread+0x173>
lea    0xd17(%rip),%rdi
call   10c0 <perror@plt>
jmp    1349 <downlink_thread+0x199>
mov    -0x1014(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rsi
lea    0xcfa(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<downlink_thread>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1020,%rsp
mov    (%rdi),%r14d
call   1030 <free@plt>
xor    %eax,%eax
call   10b0 <pthread_self@plt>
mov    %eax,%edi
call   10a0 <pthread_detach@plt>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
movsd  0x10(%rsp),%xmm0
divsd  0xe04(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x18(%rsp)
subsd  %xmm0,%xmm0
movsd  0xdf4(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    127b <downlink_thread+0xcb>
lea    0x20(%rsp),%rbx
lea    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10d0 <writen@plt>
test   %rax,%rax
js     12f2 <downlink_thread+0x142>
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
movsd  0x10(%rsp),%xmm0
divsd  0xd9f(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
subsd  0x18(%rsp),%xmm0
movsd  0xd93(%rip),%xmm1
ucomisd %xmm0,%xmm1
ja     1230 <downlink_thread+0x80>
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <shutdown@plt>
test   %rax,%rax
js     12d6 <downlink_thread+0x126>
lea    0x20(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <readn@plt>
test   %eax,%eax
js     12e4 <downlink_thread+0x134>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xd86(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd42(%rip),%rdi
call   10c0 <perror@plt>
jmp    1291 <downlink_thread+0xe1>
lea    0xd45(%rip),%rdi
call   10c0 <perror@plt>
jmp    12c0 <downlink_thread+0x110>
lea    0xd17(%rip),%rdi
call   10c0 <perror@plt>
jmp    127b <downlink_thread+0xcb>

LLVM IR (-O0):
%struct.timeval = type { double, double }
@.str = [15 x i8] c"writen() error\00"
@SHUT_WR = global i32 0
@.str.1 = [17 x i8] c"shutdown() error\00"
@.str.2 = [14 x i8] c"readn() error\00"
@.str.3 = [19 x i8] c"Throughput: %sbps\0A\00"
define i8* @downlink_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca double
%8 = alloca double
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %10
store i32 %11, i32* %3
%12 = load i8*, i8** %2
%13 = call i32 @free(i8* %12)
%14 = call i32 (...) @pthread_self()
%15 = call i32 @pthread_detach(i32 %14)
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%17 = call i32 @memset(i8* %16, i8 signext 0, i32 4096)
%18 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%20 = load double, double* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, 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* %7
%25 = load double, double* %7
store double %25, double* %8
br label %26
26: ; preds = %38, %1
%27 = load double, double* %8
%28 = load double, double* %7
%29 = fsub double %27, %28
%30 = fcmp olt double %29, 6.000000e+01
br i1 %30, label %31, label %46
31: ; preds = %26
%32 = load i32, i32* %3
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%34 = call i64 @writen(i32 %32, i8* %33, i32 4096)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %46
38: ; preds = %31
%39 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%41 = load double, double* %40
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%43 = load double, double* %42
%44 = fdiv double %43, 1.000000e+06
%45 = fadd double %41, %44
store double %45, double* %8
br label %26
46: ; preds = %36, %26
%47 = load i32, i32* %3
%48 = load i32, i32* @SHUT_WR
%49 = call i64 @shutdown(i32 %47, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %53
53: ; preds = %51, %46
%54 = load i32, i32* %3
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%56 = call i32 @readn(i32 %54, i8* %55, i32 4096)
store i32 %56, i32* %5
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %67
60: ; preds = %53
%61 = load i32, i32* %5
%62 = sub nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%66 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %60, %58
%68 = load i32, i32* %3
%69 = call i32 @close(i32 %68)
ret i8* null
}

LLVM IR (-O2):
%struct.timeval = type { double, double }
@.str = [15 x i8] c"writen() error\00"
@SHUT_WR = global i32 0
@.str.1 = [17 x i8] c"shutdown() error\00"
@.str.2 = [14 x i8] c"readn() error\00"
@.str.3 = [19 x i8] c"Throughput: %sbps\0A\00"
define noalias i8* @downlink_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 @memset(i8* %4, i8 signext 0, i32 4096)
%12 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%14 = load double, double* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%16 = load double, double* %15
%17 = fdiv double %16, 1.000000e+06
%18 = fadd double %14, %17
%19 = fsub double %18, %18
%20 = fcmp olt double %19, 6.000000e+01
br i1 %20, label %21, label %34
21: ; preds = %1, %26
%22 = call i64 @writen(i32 %7, i8* %4, i32 4096)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %34
26: ; preds = %21
%27 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%28 = load double, double* %13
%29 = load double, double* %15
%30 = fdiv double %29, 1.000000e+06
%31 = fadd double %28, %30
%32 = fsub double %31, %18
%33 = fcmp olt double %32, 6.000000e+01
br i1 %33, label %21, label %34
34: ; preds = %26, %1, %24
%35 = load i32, i32* @SHUT_WR
%36 = call i64 @shutdown(i32 %7, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = call i32 @readn(i32 %7, i8* %4, i32 4096)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %50
45: ; preds = %40
%46 = add nsw i32 %41, -1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %50
50: ; preds = %45, %43
%51 = call i32 @close(i32 %7)
ret i8* null
}