#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ time_t;
struct tm {
  int dummy;
};
struct timeval {
  int /*<<< orphan*/ tv_usec;
};
struct stat {
  int /*<<< orphan*/ st_size;
};
struct TYPE_2__ {
  unsigned int s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ pthread_t;
typedef int /*<<< orphan*/ off_t;
typedef int /*<<< orphan*/ in_buf;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
unsigned int accept(unsigned int, struct sockaddr *, int *);
int /*<<< orphan*/ insertion(unsigned int, char *, int /*<<< orphan*/,
                             unsigned int, int *, char *);
int /*<<< orphan*/ listen(unsigned int, int);
struct tm *localtime(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(unsigned int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(int *, int, int *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strftime(int *, int, char *, struct tm *);
int strlen(char *);
char *strtok(char *, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void *thread_listen(void *arg) {
  unsigned int sockfd = *((unsigned int *)arg);
  int i, size;
  unsigned int acceptfd, ids2;
  socklen_t clilen;
  int newsockfd[10], c;
  int n;
  char buffer[256];
  pthread_t t_serve[10];
  struct sockaddr_in cli_addr;
  clilen = sizeof(cli_addr);
  unsigned int retval;
  char request_buffer[1024];
  int retcode;
  off_t file_size;
  char in_buf[1024];
  char *fname = malloc(sizeof(char *));
  struct stat st;
  int k, j;
  int l;
  listen(sockfd, 5);
  while (1) {
    acceptfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    if (acceptfd < 0)
      perror("error in accepting");
    unsigned int ip = cli_addr.sin_addr.s_addr;
    time_t now;
    time(&now);
    struct tm *ct = localtime(&now);
    int ch[128], time_arrival[128];
    struct timeval tv;
    strftime(ch, sizeof ch, "[%d/%b/%Y : %H:%M:%S %z]", ct);
    snprintf(time_arrival, sizeof time_arrival, ch, tv.tv_usec);
    char *file_name = malloc(sizeof(char *));
    memset(in_buf, 0, sizeof(in_buf));
    retcode = recv(acceptfd, in_buf, 1024, 0);
    if (retcode < 0) {
      printf("recv error detected ...\n");
    } else {
      strtok(in_buf, " ");
      file_name = strtok(NULL, " ");
    }
    if (file_name != NULL) {
      k = 1, j = 0;
      while (k < strlen(file_name)) {
        fname[j] = file_name[k];
        k++;
        j++;
      }
      if (stat(fname, &st) == 0)
        file_size = st.st_size;
      insertion(acceptfd, file_name, file_size, ip, time_arrival, in_buf);
    } else {
      continue;
    }
  }
}


x86 assembly (clang, -O0):
<thread_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe00,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x4,-0x20(%rbp)
mov    $0x8,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x9b8(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
mov    -0xc(%rbp),%edi
lea    -0x198(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1110 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jae    1259 <thread_listen+0x69>
lea    0xdac(%rip),%rdi
call   10f0 <perror@plt>
mov    -0x198(%rbp),%eax
mov    %eax,-0x9d0(%rbp)
lea    -0x9d4(%rbp),%rdi
call   10a0 <time@plt>
lea    -0x9d4(%rbp),%rdi
call   1040 <localtime@plt>
mov    %rax,-0x9e0(%rbp)
lea    -0xbe0(%rbp),%rdi
mov    -0x9e0(%rbp),%rcx
mov    $0x200,%esi
lea    0xd75(%rip),%rdx
call   10e0 <strftime@plt>
lea    -0xde0(%rbp),%rdi
lea    -0xbe0(%rbp),%rdx
mov    -0xde8(%rbp),%ecx
mov    $0x200,%esi
call   1070 <snprintf@plt>
mov    $0x8,%edi
call   10b0 <malloc@plt>
mov    %rax,-0xdf0(%rbp)
lea    -0x9b0(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x9b0(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x5a4(%rbp)
cmpl   $0x0,-0x5a4(%rbp)
jge    131f <thread_listen+0x12f>
lea    0xd17(%rip),%rdi
call   1060 <printf@plt>
jmp    1349 <thread_listen+0x159>
lea    -0x9b0(%rbp),%rdi
lea    0xd18(%rip),%rsi
call   1100 <strtok@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd08(%rip),%rsi
call   1100 <strtok@plt>
mov    %rax,-0xdf0(%rbp)
cmpq   $0x0,-0xdf0(%rbp)
je     142f <thread_listen+0x23f>
movl   $0x1,-0x9c4(%rbp)
movl   $0x0,-0x9c8(%rbp)
mov    -0x9c4(%rbp),%eax
mov    %eax,-0xdf4(%rbp)
mov    -0xdf0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0xdf4(%rbp),%eax
cmp    %ecx,%eax
jge    13d8 <thread_listen+0x1e8>
mov    -0xdf0(%rbp),%rax
movslq -0x9c4(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x9b8(%rbp),%rax
movslq -0x9c8(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x9c4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c4(%rbp)
mov    -0x9c8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c8(%rbp)
jmp    136b <thread_listen+0x17b>
mov    -0x9b8(%rbp),%rdi
lea    -0x9c0(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    1401 <thread_listen+0x211>
mov    -0x9c0(%rbp),%eax
mov    %eax,-0x5a8(%rbp)
mov    -0x18(%rbp),%edi
mov    -0xdf0(%rbp),%rsi
mov    -0x5a8(%rbp),%edx
mov    -0x9d0(%rbp),%ecx
lea    -0xde0(%rbp),%r8
lea    -0x9b0(%rbp),%r9
call   10c0 <insertion@plt>
jmp    1434 <thread_listen+0x244>
jmp    122d <thread_listen+0x3d>
jmp    122d <thread_listen+0x3d>

x86 assembly (clang, -O2):
<thread_listen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    (%rdi),%ebp
movl   $0x4,0x18(%rsp)
mov    $0x8,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,0x14(%rsp)
mov    %ebp,%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
lea    0x20(%rsp),%r14
jmp    1240 <thread_listen+0x60>
nop
mov    %r13d,%edi
mov    %r15,%rsi
mov    0x10(%rsp),%edx
mov    %r12d,%ecx
mov    %rbp,%r14
mov    %rbp,%r8
lea    0x420(%rsp),%r9
call   10c0 <insertion@plt>
mov    0x14(%rsp),%edi
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
call   1100 <accept@plt>
mov    %eax,%r13d
mov    0x8(%rsp),%r12d
lea    0x1c(%rsp),%rbp
mov    %rbp,%rdi
call   10a0 <time@plt>
mov    %rbp,%rdi
call   1040 <localtime@plt>
lea    0x220(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
lea    0xd79(%rip),%rdx
mov    %rax,%rcx
call   10e0 <strftime@plt>
mov    %r14,%rdi
mov    $0x200,%esi
mov    %rbp,%rdx
call   1070 <snprintf@plt>
mov    $0x8,%edi
call   10b0 <malloc@plt>
mov    %rax,%r15
lea    0x420(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1310 <thread_listen+0x130>
mov    %rbp,%rdi
lea    0xd36(%rip),%rbp
mov    %rbp,%rsi
call   10f0 <strtok@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10f0 <strtok@plt>
mov    %rax,%r15
test   %r15,%r15
jne    1325 <thread_listen+0x145>
jmp    1240 <thread_listen+0x60>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xd04(%rip),%rdi
call   1050 <puts@plt>
test   %r15,%r15
je     1240 <thread_listen+0x60>
mov    %r14,%rbp
mov    %r15,%rdi
call   1060 <strlen@plt>
cmp    $0x2,%eax
jl     135d <thread_listen+0x17d>
mov    $0x1,%r14d
nopl   0x0(%rax,%rax,1)
movzbl (%r15,%r14,1),%eax
mov    %al,-0x1(%rbx,%r14,1)
add    $0x1,%r14
mov    %r15,%rdi
call   1060 <strlen@plt>
cltq
cmp    %rax,%r14
jl     1340 <thread_listen+0x160>
mov    %rbx,%rdi
mov    %rsp,%rsi
call   1090 <stat@plt>
test   %rax,%rax
jne    1220 <thread_listen+0x40>
mov    (%rsp),%eax
mov    %eax,0x10(%rsp)
jmp    1220 <thread_listen+0x40>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.stat = type { i32 }
%struct.tm = type { i32 }
%struct.timeval = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [19 x i8] c"error in accepting\00"
@.str.1 = [25 x i8] c"[%d/%b/%Y : %H:%M:%S %z]\00"
@.str.2 = [25 x i8] c"recv error detected ...\0A\00"
@.str.3 = [2 x i8] c" \00"
define i8* @thread_listen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [10 x i32]
%10 = alloca i32
%11 = alloca i32
%12 = alloca [256 x i8]
%13 = alloca [10 x i32]
%14 = alloca %struct.sockaddr_in
%15 = alloca i32
%16 = alloca [1024 x i8]
%17 = alloca i32
%18 = alloca i32
%19 = alloca [1024 x i8]
%20 = alloca i8*
%21 = alloca %struct.stat
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
%25 = alloca i32
%26 = alloca i32
%27 = alloca %struct.tm*
%28 = alloca [128 x i32]
%29 = alloca [128 x i32]
%30 = alloca %struct.timeval
%31 = alloca i8*
store i8* %0, i8** %2
%32 = load i8*, i8** %2
%33 = bitcast i8* %32 to i32*
%34 = load i32, i32* %33
store i32 %34, i32* %3
store i32 4, i32* %8
%35 = call i8* @malloc(i32 8)
store i8* %35, i8** %20
%36 = load i32, i32* %3
%37 = call i32 @listen(i32 %36, i32 5)
br label %38
38: ; preds = %1, %112, %113
%39 = load i32, i32* %3
%40 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%41 = call i32 @accept(i32 %39, %struct.sockaddr* %40, i32* %8)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp ult i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %44, %38
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
%49 = load i32, i32* %48
store i32 %49, i32* %25
%50 = call i32 @time(i32* %26)
%51 = call %struct.tm* @localtime(i32* %26)
store %struct.tm* %51, %struct.tm** %27
%52 = getelementptr inbounds [128 x i32], [128 x i32]* %28, i64 0, i64 0
%53 = load %struct.tm*, %struct.tm** %27
%54 = call i32 @strftime(i32* %52, i32 512, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %53)
%55 = getelementptr inbounds [128 x i32], [128 x i32]* %29, i64 0, i64 0
%56 = getelementptr inbounds [128 x i32], [128 x i32]* %28, i64 0, i64 0
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %30, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @snprintf(i32* %55, i32 512, i32* %56, i32 %58)
%60 = call i8* @malloc(i32 8)
store i8* %60, i8** %31
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %19, i64 0, i64 0
%62 = call i32 @memset(i8* %61, i32 0, i32 1024)
%63 = load i32, i32* %6
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %19, i64 0, i64 0
%65 = call i32 @recv(i32 %63, i8* %64, i32 1024, i32 0)
store i32 %65, i32* %17
%66 = load i32, i32* %17
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %46
%69 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %74
70: ; preds = %46
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %19, i64 0, i64 0
%72 = call i8* @strtok(i8* %71, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%73 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %73, i8** %31
br label %74
74: ; preds = %70, %68
%75 = load i8*, i8** %31
%76 = icmp ne i8* %75, null
br i1 %76, label %77, label %112
77: ; preds = %74
store i32 1, i32* %22
store i32 0, i32* %23
br label %78
78: ; preds = %83, %77
%79 = load i32, i32* %22
%80 = load i8*, i8** %31
%81 = call i32 @strlen(i8* %80)
%82 = icmp slt i32 %79, %81
br i1 %82, label %83, label %97
83: ; preds = %78
%84 = load i8*, i8** %31
%85 = load i32, i32* %22
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
%88 = load i8, i8* %87
%89 = load i8*, i8** %20
%90 = load i32, i32* %23
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %89, i64 %91
store i8 %88, i8* %92
%93 = load i32, i32* %22
%94 = add nsw i32 %93, 1
store i32 %94, i32* %22
%95 = load i32, i32* %23
%96 = add nsw i32 %95, 1
store i32 %96, i32* %23
br label %78
97: ; preds = %78
%98 = load i8*, i8** %20
%99 = call i64 @stat(i8* %98, %struct.stat* %21)
%100 = icmp eq i64 %99, 0
br i1 %100, label %101, label %104
101: ; preds = %97
%102 = getelementptr inbounds %struct.stat, %struct.stat* %21, i32 0, i32 0
%103 = load i32, i32* %102
store i32 %103, i32* %18
br label %104
104: ; preds = %101, %97
%105 = load i32, i32* %6
%106 = load i8*, i8** %31
%107 = load i32, i32* %18
%108 = load i32, i32* %25
%109 = getelementptr inbounds [128 x i32], [128 x i32]* %29, i64 0, i64 0
%110 = getelementptr inbounds [1024 x i8], [1024 x i8]* %19, i64 0, i64 0
%111 = call i32 @insertion(i32 %105, i8* %106, i32 %107, i32 %108, i32* %109, i8* %110)
br label %113
112: ; preds = %74
br label %38
113: ; preds = %104
br label %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
%struct.tm = type { i32 }
@.str.1 = [25 x i8] c"[%d/%b/%Y : %H:%M:%S %z]\00"
@.str.3 = [2 x i8] c" \00"
@str = [24 x i8] c"recv error detected ...\00"
define noalias i8* @thread_listen(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca [128 x i32]
%8 = alloca [128 x i32]
%9 = bitcast i8* %0 to i32*
%10 = load i32, i32* %9
%11 = bitcast i32* %2 to i8*
%12 = bitcast %struct.sockaddr_in* %3 to i8*
store i32 4, i32* %2
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%15 = bitcast %struct.stat* %5 to i8*
%16 = tail call i32 @listen(i32 %10, i32 5)
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%19 = bitcast i32* %6 to i8*
%20 = bitcast [128 x i32]* %7 to i8*
%21 = bitcast [128 x i32]* %8 to i8*
%22 = getelementptr inbounds [128 x i32], [128 x i32]* %7, i64 0, i64 0
%23 = getelementptr inbounds [128 x i32], [128 x i32]* %8, i64 0, i64 0
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %25
25: ; preds = %65, %1
%26 = phi i32 [ undef, %1 ], [ %66, %65 ]
%27 = call i32 @accept(i32 %10, %struct.sockaddr* %17, i32* %2)
%28 = load i32, i32* %18
%29 = call i32 @time(i32* %6)
%30 = call %struct.tm* @localtime(i32* %6)
%31 = call i32 @strftime(i32* %22, i32 512, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %30)
%32 = call i32 @snprintf(i32* %23, i32 512, i32* %22, i32 undef)
%33 = call dereferenceable_or_null(8) i8* @malloc(i32 8)
%34 = call i32 @memset(i8* %13, i32 0, i32 1024)
%35 = call i32 @recv(i32 %27, i8* %13, i32 1024, i32 0)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %25
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %42
39: ; preds = %25
%40 = call i8* @strtok(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%41 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %39, %37
%43 = phi i8* [ %33, %37 ], [ %41, %39 ]
%44 = icmp eq i8* %43, null
br i1 %44, label %65, label %45
45: ; preds = %42
%46 = call i32 @strlen(i8* %43)
%47 = icmp sgt i32 %46, 1
br i1 %47, label %48, label %59
48: ; preds = %45, %48
%49 = phi i64 [ %55, %48 ], [ 0, %45 ]
%50 = phi i64 [ %54, %48 ], [ 1, %45 ]
%51 = getelementptr inbounds i8, i8* %43, i64 %50
%52 = load i8, i8* %51
%53 = getelementptr inbounds i8, i8* %14, i64 %49
store i8 %52, i8* %53
%54 = add nuw nsw i64 %50, 1
%55 = add nuw nsw i64 %49, 1
%56 = call i32 @strlen(i8* %43)
%57 = sext i32 %56 to i64
%58 = icmp slt i64 %54, %57
br i1 %58, label %48, label %59
59: ; preds = %48, %45
%60 = call i64 @stat(i8* %14, %struct.stat* %5)
%61 = icmp eq i64 %60, 0
%62 = load i32, i32* %24
%63 = select i1 %61, i32 %62, i32 %26
%64 = call i32 @insertion(i32 %27, i8* %43, i32 %63, i32 %28, i32* %23, i8* %13)
br label %65
65: ; preds = %42, %59
%66 = phi i32 [ %63, %59 ], [ %26, %42 ]
br label %25
}