#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 */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ slice(char **, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int strlen(char *);
char *strrchr(char *, char);
int vfork();
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
void *new_client(void *ptr) {
  char buf[1024];
  int err;
  int *data = (int *)ptr;
  int client_fd = *data;
  pid_t forkStatus;
  char *args[2048];
  char out[10048];
  int out_pipe[2];
  int saved_stdout;
  int tem_e;
  while (1) {
    tem_e = 0;
    err = send(client_fd, "> ", 2, 0);
    if (err < 0) {
      fprintf(stderr, "Client write failed\n");
      fflush(stderr);
      exit(1);
    };
    int readed = recv(client_fd, buf, 1024, 0);
    printf("BUff %s\n", buf);
    if (!readed)
      break;
    if (readed < 0) {
      fprintf(stderr, "Client read failed\n");
      fflush(stderr);
      exit(1);
    };
    if (strcmp(buf, "q") == 0) {
      break;
    } else {
      char *end = strrchr(buf, '&');
      if ('&' == buf[strlen(buf) - 1]) {
        tem_e = 1;
        *end = '\0';
      }
      forkStatus = vfork();
      if (forkStatus == 0) {
        slice(args, buf);
        saved_stdout = dup(STDOUT_FILENO);
        if (pipe(out_pipe) != 0) {
          exit(1);
        }
        dup2(out_pipe[1], STDOUT_FILENO);
        close(out_pipe[1]);
        read(out_pipe[0], out, 10048);
        err = send(client_fd, out, strlen(out), 0);
      } else if (forkStatus != -1) {
        if (!tem_e) {
          wait(NULL);
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<new_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x6ba0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
movl   $0x0,-0x6b80(%rbp)
mov    -0x424(%rbp),%edi
lea    0xdb0(%rip),%rsi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    129c <new_client+0x8c>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
call   10f0 <fprintf@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1100 <fflush@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x424(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x6b84(%rbp)
lea    -0x410(%rbp),%rsi
lea    0xd4f(%rip),%rdi
call   1080 <printf@plt>
cmpl   $0x0,-0x6b84(%rbp)
jne    12e0 <new_client+0xd0>
jmp    14a1 <new_client+0x291>
cmpl   $0x0,-0x6b84(%rbp)
jge    131a <new_client+0x10a>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
lea    0xd24(%rip),%rsi
call   10f0 <fprintf@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1100 <fflush@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    -0x410(%rbp),%rdi
lea    0xd0d(%rip),%rsi
call   10e0 <strcmp@plt>
cmp    $0x0,%rax
jne    133c <new_client+0x12c>
jmp    14a1 <new_client+0x291>
lea    -0x410(%rbp),%rdi
mov    $0x26,%esi
call   1090 <strrchr@plt>
mov    %rax,-0x6b90(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%ecx
ff
mov    $0x26,%eax
cmp    %ecx,%eax
jne    138e <new_client+0x17e>
movl   $0x1,-0x6b80(%rbp)
mov    -0x6b90(%rbp),%rax
movb   $0x0,(%rax)
call   1110 <vfork@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jne    146a <new_client+0x25a>
lea    -0x4430(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1040 <slice@plt>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
call   10a0 <dup@plt>
mov    %eax,-0x6b7c(%rbp)
lea    -0x6b78(%rbp),%rdi
call   10c0 <pipe@plt>
cmp    $0x0,%rax
je     13ed <new_client+0x1dd>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x6b74(%rbp),%edi
mov    0x2bf6(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0x6b74(%rbp),%edi
call   10b0 <close@plt>
mov    -0x6b78(%rbp),%edi
lea    -0x6b70(%rbp),%rsi
mov    $0x2740,%edx
call   10d0 <read@plt>
mov    -0x424(%rbp),%eax
mov    %eax,-0x6b9c(%rbp)
lea    -0x6b70(%rbp),%rax
mov    %rax,-0x6b98(%rbp)
lea    -0x6b70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x6b9c(%rbp),%edi
mov    -0x6b98(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %eax,-0x414(%rbp)
jmp    1497 <new_client+0x287>
cmpl   $0xffffffff,-0x428(%rbp)
je     1492 <new_client+0x282>
cmpl   $0x0,-0x6b80(%rbp)
jne    148d <new_client+0x27d>
xor    %eax,%eax
mov    %eax,%edi
call   1130 <wait@plt>
jmp    1492 <new_client+0x282>
jmp    1497 <new_client+0x287>
jmp    149c <new_client+0x28c>
jmp    1239 <new_client+0x29>
mov    -0x8(%rbp),%rax
add    $0x6ba0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x6b58,%rsp
mov    (%rdi),%r12d
lea    0xdd5(%rip),%rsi
mov    $0x2,%edx
xor    %ecx,%ecx
mov    %r12d,%edi
call   1070 <send@plt>
test   %eax,%eax
js     13ab <new_client+0x19b>
lea    0x10(%rsp),%r13
xor    %r15d,%r15d
mov    0x2d9f(%rip),%r14
jmp    127f <new_client+0x6f>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12d,%edi
lea    0xd96(%rip),%rsi
mov    $0x2,%edx
mov    %r15d,%ecx
call   1070 <send@plt>
test   %eax,%eax
js     13ab <new_client+0x19b>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
mov    %r15d,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
lea    0xd7d(%rip),%rdi
mov    %r13,%rsi
call   1080 <printf@plt>
test   %ebp,%ebp
je     1399 <new_client+0x189>
js     13bd <new_client+0x1ad>
mov    %r13,%rdi
lea    0xd7a(%rip),%rsi
call   10e0 <strcmp@plt>
test   %rax,%rax
je     1399 <new_client+0x189>
mov    %r14,%rbp
mov    %r13,%rdi
mov    $0x26,%esi
call   1090 <strrchr@plt>
mov    %rax,%r14
mov    %r13,%rdi
call   1050 <strlen@plt>
cltq
movzbl -0x1(%rax,%r13,1),%ebx
cmp    $0x26,%bl
jne    12f5 <new_client+0xe5>
movb   $0x0,(%r14)
call   1110 <vfork@plt>
test   %eax,%eax
je     1320 <new_client+0x110>
cmp    $0xffffffff,%eax
mov    %rbp,%r14
je     1260 <new_client+0x50>
cmp    $0x26,%bl
je     1260 <new_client+0x50>
mov    %r15d,%edi
call   1130 <wait@plt>
jmp    1260 <new_client+0x50>
lea    0x2b50(%rsp),%rdi
mov    %r13,%rsi
call   1040 <slice@plt>
mov    %rbp,%r14
mov    0x0(%rbp),%edi
call   10a0 <dup@plt>
lea    0x8(%rsp),%rdi
call   10c0 <pipe@plt>
test   %rax,%rax
jne    13d9 <new_client+0x1c9>
mov    0xc(%rsp),%edi
mov    (%r14),%esi
call   1060 <dup2@plt>
mov    0xc(%rsp),%edi
call   10b0 <close@plt>
mov    0x8(%rsp),%edi
lea    0x410(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x2740,%edx
call   10d0 <read@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %r15d,%ecx
call   1070 <send@plt>
jmp    1260 <new_client+0x50>
add    $0x6b58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c26(%rip),%rbx
mov    (%rbx),%edi
lea    0xc48(%rip),%rsi
jmp    13cd <new_client+0x1bd>
mov    0x2c14(%rip),%rbx
mov    (%rbx),%edi
lea    0xc54(%rip),%rsi
call   10f0 <fprintf@plt>
mov    (%rbx),%edi
call   1100 <fflush@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"> \00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Client write failed\0A\00"
@.str.2 = [9 x i8] c"BUff %s\0A\00"
@.str.3 = [20 x i8] c"Client read failed\0A\00"
@.str.4 = [2 x i8] c"q\00"
@STDOUT_FILENO = global i32 0
define i8* @new_client(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2048 x i8*]
%10 = alloca [10048 x i8]
%11 = alloca [2 x i32]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
store i8* %0, i8** %3
%16 = load i8*, i8** %3
%17 = bitcast i8* %16 to i32*
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = load i32, i32* %18
store i32 %19, i32* %7
br label %20
20: ; preds = %1, %110
store i32 0, i32* %13
%21 = load i32, i32* %7
%22 = call i32 @send(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2, i32 0)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = call i32 @fflush(i32 %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = load i32, i32* %7
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = call i32 @recv(i32 %32, i8* %33, i32 1024, i32 0)
store i32 %34, i32* %14
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%36 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %14
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %31
br label %111
40: ; preds = %31
%41 = load i32, i32* %14
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* @stderr
%47 = call i32 @fflush(i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %40
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %49
br label %111
54: ; preds = %49
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%56 = call i8* @strrchr(i8* %55, i8 signext 38)
store i8* %56, i8** %15
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%58 = call i32 @strlen(i8* %57)
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 38, %63
br i1 %64, label %65, label %67
65: ; preds = %54
store i32 1, i32* %13
%66 = load i8*, i8** %15
store i8 0, i8* %66
br label %67
67: ; preds = %65, %54
%68 = call i32 @vfork()
store i32 %68, i32* %8
%69 = load i32, i32* %8
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %99
71: ; preds = %67
%72 = getelementptr inbounds [2048 x i8*], [2048 x i8*]* %9, i64 0, i64 0
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%74 = call i32 @slice(i8** %72, i8* %73)
%75 = load i32, i32* @STDOUT_FILENO
%76 = call i32 @dup(i32 %75)
store i32 %76, i32* %12
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%78 = call i64 @pipe(i32* %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %71
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %71
%83 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%84 = load i32, i32* %83
%85 = load i32, i32* @STDOUT_FILENO
%86 = call i32 @dup2(i32 %84, i32 %85)
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%88 = load i32, i32* %87
%89 = call i32 @close(i32 %88)
%90 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%91 = load i32, i32* %90
%92 = getelementptr inbounds [10048 x i8], [10048 x i8]* %10, i64 0, i64 0
%93 = call i32 @read(i32 %91, i8* %92, i32 10048)
%94 = load i32, i32* %7
%95 = getelementptr inbounds [10048 x i8], [10048 x i8]* %10, i64 0, i64 0
%96 = getelementptr inbounds [10048 x i8], [10048 x i8]* %10, i64 0, i64 0
%97 = call i32 @strlen(i8* %96)
%98 = call i32 @send(i32 %94, i8* %95, i32 %97, i32 0)
store i32 %98, i32* %5
br label %109
99: ; preds = %67
%100 = load i32, i32* %8
%101 = icmp ne i32 %100, -1
br i1 %101, label %102, label %108
102: ; preds = %99
%103 = load i32, i32* %13
%104 = icmp ne i32 %103, 0
br i1 %104, label %107, label %105
105: ; preds = %102
%106 = call i32 @wait(i32* null)
br label %107
107: ; preds = %105, %102
br label %108
108: ; preds = %107, %99
br label %109
109: ; preds = %108, %82
br label %110
110: ; preds = %109
br label %20
111: ; preds = %53, %39
%112 = load i8*, i8** %2
ret i8* %112
}

LLVM IR (-O2):
@.str = [3 x i8] c"> \00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Client write failed\0A\00"
@.str.2 = [9 x i8] c"BUff %s\0A\00"
@.str.3 = [20 x i8] c"Client read failed\0A\00"
@.str.4 = [2 x i8] c"q\00"
@STDOUT_FILENO = global i32 0
define noalias i8* @new_client(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca [2048 x i8*]
%4 = alloca [10048 x i8]
%5 = alloca [2 x i32]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = bitcast i8* %0 to i32*
%8 = load i32, i32* %7
%9 = bitcast [2048 x i8*]* %3 to i8*
%10 = getelementptr inbounds [10048 x i8], [10048 x i8]* %4, i64 0, i64 0
%11 = bitcast [2 x i32]* %5 to i8*
%12 = call i32 @send(i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %1
%15 = getelementptr inbounds [2048 x i8*], [2048 x i8*]* %3, i64 0, i64 0
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
br label %24
18: ; preds = %74, %1
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = call i32 @fflush(i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14, %74
%25 = call i32 @recv(i32 %8, i8* %6, i32 1024, i32 0)
%26 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%27 = icmp eq i32 %25, 0
br i1 %27, label %77, label %28
28: ; preds = %24
%29 = icmp slt i32 %25, 0
br i1 %29, label %30, label %36
30: ; preds = %28
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @stderr
%34 = call i32 @fflush(i32 %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %77, label %39
39: ; preds = %36
%40 = call i8* @strrchr(i8* %6, i8 signext 38)
%41 = call i32 @strlen(i8* %6)
%42 = add nsw i32 %41, -1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = icmp eq i8 %45, 38
br i1 %46, label %47, label %48
47: ; preds = %39
store i8 0, i8* %40
br label %48
48: ; preds = %47, %39
%49 = call i32 @vfork()
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %69
51: ; preds = %48
%52 = call i32 @slice(i8** %15, i8* %6)
%53 = load i32, i32* @STDOUT_FILENO
%54 = call i32 @dup(i32 %53)
%55 = call i64 @pipe(i32* %16)
%56 = icmp eq i64 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %51
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %51
%60 = load i32, i32* %17
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i32 @dup2(i32 %60, i32 %61)
%63 = load i32, i32* %17
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %16
%66 = call i32 @read(i32 %65, i8* %10, i32 10048)
%67 = call i32 @strlen(i8* %10)
%68 = call i32 @send(i32 %8, i8* %10, i32 %67, i32 0)
br label %74
69: ; preds = %48
%70 = icmp eq i32 %49, -1
%71 = select i1 %70, i1 true, i1 %46
br i1 %71, label %74, label %72
72: ; preds = %69
%73 = call i32 @wait(i32* null)
br label %74
74: ; preds = %59, %72, %69
%75 = call i32 @send(i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2, i32 0)
%76 = icmp slt i32 %75, 0
br i1 %76, label %18, label %24
77: ; preds = %24, %36
ret i8* undef
}