#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 /*<<< orphan*/ trailer;
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ header;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int FD_SETSIZE;
int /*<<< orphan*/ UNLLog(int, char *, int, ...);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int /*<<< orphan*/ send(int, ...);
int strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int ts_accept(fd_set *fd_set, int server_socket, char *xtitle,
              int *client_socket, int sendHeaders) {
  unsigned char header[] = {255, 251, 1,   255, 251,    3,   255, 251,
                            0,   255, 253, 0,   '\033', ']', '0', ';'};
  char trailer[] = {'\007'};
  int newclient_socket;
  int rc = -1;
  struct sockaddr_in6 client_addr;
  memset(&client_addr, 0, sizeof(client_addr));
  socklen_t client_addrlen = sizeof(client_addr);
  if ((newclient_socket = accept(server_socket, (struct sockaddr *)&client_addr,
                                 &client_addrlen)) < 0) {
    rc = 1;
    UNLLog(1, "Failed to accept new TS client (accept). ERR: %s (%i).\n",
           strerror(errno), rc);
    close(newclient_socket);
    return rc;
  } else {
    if (client_socket[0] + 1 > FD_SETSIZE) {
      rc = 2;
      UNLLog(1, "Failed to accept new TS client (MAX FD). ERR: %s (%i).\n",
             strerror(errno), rc);
      close(newclient_socket);
      return rc;
    }
    if (sendHeaders) {
      send(newclient_socket, &header, sizeof(header) / sizeof(header[0]), 0);
      send(newclient_socket, xtitle, strlen(xtitle), 0);
      send(newclient_socket, &trailer, sizeof(trailer) / sizeof(trailer[0]), 0);
    }
    UNLLog(3, "Adding client socket descriptor (%i).\n", newclient_socket);
    FD_SET(newclient_socket, fd_set);
    client_socket[++client_socket[0]] = newclient_socket;
  }
  UNLLog(3, "New client connected (%i), total is %i.\n", newclient_socket,
         client_socket[0]);
  return 0;
}


x86 assembly (clang, -O0):
<ts_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    0xe5e(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe5b(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe59(%rip),%al
mov    %al,-0x41(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x4,-0x54(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
lea    -0x54(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
jge    1234 <ts_accept+0xb4>
movl   $0x1,-0x4c(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edx
mov    -0x4c(%rbp),%ecx
mov    $0x1,%edi
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1070 <UNLLog@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1346 <ts_accept+0x1c6>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%eax
jle    128c <ts_accept+0x10c>
movl   $0x2,-0x4c(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edx
mov    -0x4c(%rbp),%ecx
mov    $0x1,%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1070 <UNLLog@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1346 <ts_accept+0x1c6>
cmpl   $0x0,-0x2c(%rbp)
je     12e9 <ts_accept+0x169>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    -0x48(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    -0x48(%rbp),%edx
mov    $0x3,%edi
lea    0xd89(%rip),%rsi
mov    $0x0,%al
call   1070 <UNLLog@plt>
mov    -0x48(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x48(%rbp),%edx
mov    -0x28(%rbp),%rax
mov    -0x28(%rbp),%rsi
mov    (%rsi),%ecx
add    $0x1,%ecx
mov    %ecx,(%rsi)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x48(%rbp),%edx
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
mov    $0x3,%edi
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1070 <UNLLog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ts_accept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r13d
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%ebx
mov    %rdi,%r15
movaps 0xe5d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x7,0x3(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x4,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1285 <ts_accept+0x105>
mov    (%r14),%eax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%eax
jge    12ad <ts_accept+0x12d>
test   %r13d,%r13d
je     123b <ts_accept+0xbb>
lea    0x10(%rsp),%rsi
mov    $0x10,%edx
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
lea    0xe3e(%rip),%rsi
xor    %ebp,%ebp
mov    $0x3,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <UNLLog@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10a0 <FD_SET@plt>
movslq (%r14),%rax
lea    0x1(%rax),%rcx
mov    %ecx,(%r14)
mov    %ebx,0x4(%r14,%rax,4)
mov    (%r14),%ecx
lea    0xe32(%rip),%rsi
mov    $0x3,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <UNLLog@plt>
jmp    12e1 <ts_accept+0x161>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd76(%rip),%rsi
mov    $0x1,%ebp
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x1,%ecx
jmp    12d3 <ts_accept+0x153>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd86(%rip),%rsi
mov    $0x2,%ebp
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x2,%ecx
xor    %eax,%eax
call   1070 <UNLLog@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@__const.ts_accept.header = [16 x i8] c"\FF\FB\01\FF\FB\03\FF\FB\00\FF\FD\00\1B]0;"
@__const.ts_accept.trailer = [1 x i8] c"\07"
@.str = [56 x i8] c"Failed to accept new TS client (accept). ERR: %s (%i).\0A\00"
@errno = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [56 x i8] c"Failed to accept new TS client (MAX FD). ERR: %s (%i).\0A\00"
@.str.2 = [39 x i8] c"Adding client socket descriptor (%i).\0A\00"
@.str.3 = [41 x i8] c"New client connected (%i), total is %i.\0A\00"
define i32 @ts_accept(i32* %0, i32 %1, i8* %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca [16 x i8]
%13 = alloca [1 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.sockaddr_in6
%17 = alloca i32
store i32* %0, i32** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32* %3, i32** %10
store i32 %4, i32* %11
%18 = bitcast [16 x i8]* %12 to i8*
%19 = bitcast [1 x i8]* %13 to i8*
store i32 -1, i32* %15
%20 = call i32 @memset(%struct.sockaddr_in6* %16, i32 0, i32 4)
store i32 4, i32* %17
%21 = load i32, i32* %8
%22 = bitcast %struct.sockaddr_in6* %16 to %struct.sockaddr*
%23 = call i32 @accept(i32 %21, %struct.sockaddr* %22, i32* %17)
store i32 %23, i32* %14
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %5
store i32 1, i32* %15
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = load i32, i32* %15
%29 = call i32 (i32, i8*, i32, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %28)
%30 = load i32, i32* %14
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %15
store i32 %32, i32* %6
br label %81
33: ; preds = %5
%34 = load i32*, i32** %10
%35 = getelementptr inbounds i32, i32* %34, i64 0
%36 = load i32, i32* %35
%37 = add nsw i32 %36, 1
%38 = load i32, i32* @FD_SETSIZE
%39 = icmp sgt i32 %37, %38
br i1 %39, label %40, label %48
40: ; preds = %33
store i32 2, i32* %15
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = load i32, i32* %15
%44 = call i32 (i32, i8*, i32, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %43)
%45 = load i32, i32* %14
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %15
store i32 %47, i32* %6
br label %81
48: ; preds = %33
%49 = load i32, i32* %11
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %61
51: ; preds = %48
%52 = load i32, i32* %14
%53 = call i32 (i32, ...) @send(i32 %52, [16 x i8]* %12, i64 16, i32 0)
%54 = load i32, i32* %14
%55 = load i8*, i8** %9
%56 = load i8*, i8** %9
%57 = call i32 @strlen(i8* %56)
%58 = call i32 (i32, ...) @send(i32 %54, i8* %55, i32 %57, i32 0)
%59 = load i32, i32* %14
%60 = call i32 (i32, ...) @send(i32 %59, [1 x i8]* %13, i64 1, i32 0)
br label %61
61: ; preds = %51, %48
%62 = load i32, i32* %14
%63 = call i32 (i32, i8*, i32, ...) @UNLLog(i32 3, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %14
%65 = load i32*, i32** %7
%66 = call i32 @FD_SET(i32 %64, i32* %65)
%67 = load i32, i32* %14
%68 = load i32*, i32** %10
%69 = load i32*, i32** %10
%70 = getelementptr inbounds i32, i32* %69, i64 0
%71 = load i32, i32* %70
%72 = add nsw i32 %71, 1
store i32 %72, i32* %70
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %68, i64 %73
store i32 %67, i32* %74
br label %75
75: ; preds = %61
%76 = load i32, i32* %14
%77 = load i32*, i32** %10
%78 = getelementptr inbounds i32, i32* %77, i64 0
%79 = load i32, i32* %78
%80 = call i32 (i32, i8*, i32, ...) @UNLLog(i32 3, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %76, i32 %79)
store i32 0, i32* %6
br label %81
81: ; preds = %75, %40, %25
%82 = load i32, i32* %6
ret i32 %82
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@__const.ts_accept.header = [16 x i8] c"\FF\FB\01\FF\FB\03\FF\FB\00\FF\FD\00\1B]0;"
@.str = [56 x i8] c"Failed to accept new TS client (accept). ERR: %s (%i).\0A\00"
@errno = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [56 x i8] c"Failed to accept new TS client (MAX FD). ERR: %s (%i).\0A\00"
@.str.2 = [39 x i8] c"Adding client socket descriptor (%i).\0A\00"
@.str.3 = [41 x i8] c"New client connected (%i), total is %i.\0A\00"
define i32 @ts_accept(i32* %0, i32 %1, i8* %2, i32* nocapture %3, i32 %4) {
%6 = alloca [16 x i8]
%7 = alloca [1 x i8]
%8 = alloca %struct.sockaddr_in6
%9 = alloca i32
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
store i8 7, i8* %11
%12 = bitcast %struct.sockaddr_in6* %8 to i8*
%13 = call i32 @memset(%struct.sockaddr_in6* %8, i32 0, i32 4)
%14 = bitcast i32* %9 to i8*
store i32 4, i32* %9
%15 = bitcast %struct.sockaddr_in6* %8 to %struct.sockaddr*
%16 = call i32 @accept(i32 %1, %struct.sockaddr* %15, i32* %9)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %5
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 (i32, i8*, i32, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %20, i32 1)
%22 = call i32 @close(i32 %16)
br label %48
23: ; preds = %5
%24 = load i32, i32* %3
%25 = load i32, i32* @FD_SETSIZE
%26 = icmp slt i32 %24, %25
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 (i32, i8*, i32, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 2)
%31 = call i32 @close(i32 %16)
br label %48
32: ; preds = %23
%33 = icmp eq i32 %4, 0
br i1 %33, label %39, label %34
34: ; preds = %32
%35 = call i32 (i32, ...) @send(i32 %16, [16 x i8]* %6, i64 16, i32 0)
%36 = call i32 @strlen(i8* %2)
%37 = call i32 (i32, ...) @send(i32 %16, i8* %2, i32 %36, i32 0)
%38 = call i32 (i32, ...) @send(i32 %16, [1 x i8]* %7, i64 1, i32 0)
br label %39
39: ; preds = %34, %32
%40 = call i32 (i32, i8*, i32, ...) @UNLLog(i32 3, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%41 = call i32 @FD_SET(i32 %16, i32* %0)
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
store i32 %43, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %3, i64 %44
store i32 %16, i32* %45
%46 = load i32, i32* %3
%47 = call i32 (i32, i8*, i32, ...) @UNLLog(i32 3, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 %46)
br label %48
48: ; preds = %39, %27, %18
%49 = phi i32 [ 1, %18 ], [ 2, %27 ], [ 0, %39 ]
ret i32 %49
}