#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 tm {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int execcmd(char **, int, struct tm);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int isBG(char *);
int /*<<< orphan*/ logcommand(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ prompt();
int /*<<< orphan*/ readline(int, char *, int);
int /*<<< orphan*/ splitcommand(char *, char **);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
void clienthandler(int socket, char *ipaddr, struct tm start) {
  char line[2048];
  char *cmd[1024];
  int execstat;
  int bg = 0;
  dup2(socket, STDOUT_FILENO);
  dup2(socket, STDERR_FILENO);
  do {
    prompt();
    readline(socket, line, 2048);
    if (line[0] != '\0') {
      char logline[2048] = {0};
      strncpy(logline, line, strlen(line));
      bg = isBG(line);
      splitcommand(line, cmd);
      execstat = execcmd(cmd, bg, start);
      if (execstat == 0) {
        logcommand(ipaddr, logline);
      }
      fflush(stdout);
    }
  } while (execstat != -1);
  printf("disconnect.\n");
  close(socket);
}


x86 assembly (clang, -O0):
<clienthandler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3040,%rsp
mov    %edx,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2828(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1090 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
call   1090 <dup2@plt>
mov    $0x0,%al
call   1060 <prompt@plt>
mov    -0xc(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
call   10c0 <readline@plt>
movsbl -0x820(%rbp),%eax
cmp    $0x0,%eax
je     12f7 <clienthandler+0x127>
lea    -0x3030(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   10b0 <memset@plt>
lea    -0x3030(%rbp),%rax
mov    %rax,-0x3040(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x3038(%rbp)
lea    -0x820(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x3040(%rbp),%rdi
mov    -0x3038(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
lea    -0x820(%rbp),%rdi
call   1030 <isBG@plt>
mov    %eax,-0x2828(%rbp)
lea    -0x820(%rbp),%rdi
lea    -0x2820(%rbp),%rsi
call   10f0 <splitcommand@plt>
lea    -0x2820(%rbp),%rdi
mov    -0x2828(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1050 <execcmd@plt>
mov    %eax,-0x2824(%rbp)
cmpl   $0x0,-0x2824(%rbp)
jne    12e9 <clienthandler+0x119>
mov    -0x18(%rbp),%rdi
lea    -0x3030(%rbp),%rsi
call   1070 <logcommand@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10e0 <fflush@plt>
jmp    12fc <clienthandler+0x12c>
cmpl   $0xffffffff,-0x2824(%rbp)
jne    1211 <clienthandler+0x41>
lea    0xcf0(%rip),%rdi
call   10a0 <printf@plt>
mov    -0xc(%rbp),%edi
call   10d0 <close@plt>
add    $0x3040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clienthandler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3018,%rsp
mov    %edx,0x4(%rsp)
mov    %rsi,0x8(%rsp)
mov    %edi,%r15d
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
call   10a0 <dup2@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   10a0 <dup2@plt>
lea    0x10(%rsp),%rbp
lea    0x810(%rsp),%r12
lea    0x1010(%rsp),%r13
jmp    1244 <clienthandler+0x74>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10e0 <fflush@plt>
cmp    $0xffffffff,%r14d
je     12c2 <clienthandler+0xf2>
xor    %eax,%eax
call   1070 <prompt@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x800,%edx
call   10c0 <readline@plt>
cmpb   $0x0,0x10(%rsp)
je     123e <clienthandler+0x6e>
mov    $0x800,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10b0 <memset@plt>
mov    %rbp,%rdi
call   1090 <strlen@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    %rbp,%rdi
call   1030 <isBG@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    %r13,%rsi
call   10f0 <splitcommand@plt>
mov    %r13,%rdi
mov    %ebx,%esi
mov    0x4(%rsp),%edx
call   1060 <execcmd@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    1230 <clienthandler+0x60>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1080 <logcommand@plt>
jmp    1230 <clienthandler+0x60>
lea    0xd37(%rip),%rdi
call   1050 <puts@plt>
mov    %r15d,%edi
call   10d0 <close@plt>
add    $0x3018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@stdout = global i32 0
@.str = [13 x i8] c"disconnect.\0A\00"
define void @clienthandler(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.tm
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [2048 x i8]
%8 = alloca [1024 x i8*]
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2048 x i8]
%12 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 0
store i32 %2, i32* %12
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 0, i32* %10
%13 = load i32, i32* %5
%14 = load i32, i32* @STDOUT_FILENO
%15 = call i32 @dup2(i32 %13, i32 %14)
%16 = load i32, i32* %5
%17 = load i32, i32* @STDERR_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
br label %19
19: ; preds = %55, %3
%20 = call i32 (...) @prompt()
%21 = load i32, i32* %5
%22 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%23 = call i32 @readline(i32 %21, i8* %22, i32 2048)
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %54
28: ; preds = %19
%29 = bitcast [2048 x i8]* %11 to i8*
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%31 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @strncpy(i8* %30, i8* %31, i32 %33)
%35 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%36 = call i32 @isBG(i8* %35)
store i32 %36, i32* %10
%37 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%38 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %8, i64 0, i64 0
%39 = call i32 @splitcommand(i8* %37, i8** %38)
%40 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %8, i64 0, i64 0
%41 = load i32, i32* %10
%42 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @execcmd(i8** %40, i32 %41, i32 %43)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %28
%48 = load i8*, i8** %6
%49 = getelementptr inbounds [2048 x i8], [2048 x i8]* %11, i64 0, i64 0
%50 = call i32 @logcommand(i8* %48, i8* %49)
br label %51
51: ; preds = %47, %28
%52 = load i32, i32* @stdout
%53 = call i32 @fflush(i32 %52)
br label %54
54: ; preds = %51, %19
br label %55
55: ; preds = %54
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, -1
br i1 %57, label %19, label %58
58: ; preds = %55
%59 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%60 = load i32, i32* %5
%61 = call i32 @close(i32 %60)
ret void
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@stdout = global i32 0
@str = [12 x i8] c"disconnect.\00"
define void @clienthandler(i32 %0, i8* %1, i32 %2) {
%4 = alloca [2048 x i8]
%5 = alloca [1024 x i8*]
%6 = alloca [2048 x i8]
%7 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%8 = bitcast [1024 x i8*]* %5 to i8*
%9 = load i32, i32* @STDOUT_FILENO
%10 = tail call i32 @dup2(i32 %0, i32 %9)
%11 = load i32, i32* @STDERR_FILENO
%12 = tail call i32 @dup2(i32 %0, i32 %11)
%13 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%14 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %5, i64 0, i64 0
br label %15
15: ; preds = %33, %3
%16 = phi i32 [ undef, %3 ], [ %34, %33 ]
%17 = call i32 (...) @prompt()
%18 = call i32 @readline(i32 %0, i8* %7, i32 2048)
%19 = load i8, i8* %7
%20 = icmp eq i8 %19, 0
br i1 %20, label %33, label %21
21: ; preds = %15
%22 = call i32 @strlen(i8* %7)
%23 = call i32 @strncpy(i8* %13, i8* %7, i32 %22)
%24 = call i32 @isBG(i8* %7)
%25 = call i32 @splitcommand(i8* %7, i8** %14)
%26 = call i32 @execcmd(i8** %14, i32 %24, i32 %2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @logcommand(i8* %1, i8* %13)
br label %30
30: ; preds = %28, %21
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
br label %33
33: ; preds = %15, %30
%34 = phi i32 [ %26, %30 ], [ %16, %15 ]
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %15
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%38 = call i32 @close(i32 %0)
ret void
}