#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 sockaddr_un {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct list {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ remote;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ O_NONBLOCK;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int handle_input(char *, int, int /*<<< orphan*/, char *, int *, struct list *);
int /*<<< orphan*/ memset(char *, char, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int handle_connection(int main_socket, pid_t runn_cmd_pid, char *runn_cmd,
                      int *qstat, struct list *queue) {
  int socket, n, retval;
  struct sockaddr_un remote;
  socklen_t t = sizeof(remote);
  char buf[1024];
  fcntl(main_socket, F_SETFL, O_NONBLOCK);
  memset(buf, '\0', 1024);
  if ((socket = accept(main_socket, (struct sockaddr *)&remote, &t)) == -1)
    return 0;
  n = recv(socket, buf, 1024, 0);
  if (n < 0) {
    send(socket, "NOK\n", strlen("NOK\n"), MSG_NOSIGNAL);
  } else if (n > 0) {
    retval = handle_input(buf, socket, runn_cmd_pid, runn_cmd, qstat, queue);
    if (retval == 1) {
      send(socket, "NOK\n", strlen("NOK\n"), MSG_NOSIGNAL);
    } else {
      send(socket, "OK\n", strlen("OK\n"), MSG_NOSIGNAL);
    }
  }
  close(socket);
  return retval;
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x4,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    11f9 <handle_connection+0x79>
movl   $0x0,-0x4(%rbp)
jmp    1309 <handle_connection+0x189>
mov    -0x2c(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1253 <handle_connection+0xd3>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x444(%rbp)
lea    0xdd4(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    %eax,%edx
mov    0x2d90(%rip),%rax
mov    (%rax),%ecx
lea    0xdb7(%rip),%rsi
call   1070 <send@plt>
jmp    12fb <handle_connection+0x17b>
cmpl   $0x0,-0x30(%rbp)
jle    12f6 <handle_connection+0x176>
lea    -0x440(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
call   1040 <handle_input@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jne    12bf <handle_connection+0x13f>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x448(%rbp)
lea    0xd68(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x448(%rbp),%edi
mov    %eax,%edx
mov    0x2d24(%rip),%rax
mov    (%rax),%ecx
lea    0xd4b(%rip),%rsi
call   1070 <send@plt>
jmp    12f1 <handle_connection+0x171>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x44c(%rbp)
lea    0xd32(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x44c(%rbp),%edi
mov    %eax,%edx
mov    0x2ced(%rip),%rax
mov    (%rax),%ecx
lea    0xd15(%rip),%rsi
call   1070 <send@plt>
jmp    12f6 <handle_connection+0x176>
jmp    12fb <handle_connection+0x17b>
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %r8,%rbp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%r13d
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
lea    0x10(%rsp),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
je     1262 <handle_connection+0xe2>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1235 <handle_connection+0xb5>
je     125b <handle_connection+0xdb>
lea    0x10(%rsp),%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %r12,%rcx
mov    %r15,%r8
mov    %rbp,%r9
call   1040 <handle_input@plt>
mov    %eax,%r14d
cmp    $0x1,%eax
lea    0xdd8(%rip),%rax
lea    0xdd2(%rip),%rbp
cmove  %rax,%rbp
jmp    123c <handle_connection+0xbc>
lea    0xdc4(%rip),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    0x2d85(%rip),%rcx
mov    (%rcx),%ecx
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <send@plt>
jmp    125b <handle_connection+0xdb>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %r14d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.list = type { i32 }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [5 x i8] c"NOK\0A\00"
@MSG_NOSIGNAL = global i32 0
@.str.1 = [4 x i8] c"OK\0A\00"
define i32 @handle_connection(i32 %0, i32 %1, i8* %2, i32* %3, %struct.list* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca %struct.list*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.sockaddr_un
%16 = alloca i32
%17 = alloca [1024 x i8]
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32* %3, i32** %10
store %struct.list* %4, %struct.list** %11
store i32 4, i32* %16
%18 = load i32, i32* %7
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* @O_NONBLOCK
%21 = call i32 @fcntl(i32 %18, i32 %19, i32 %20)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i8 signext 0, i32 1024)
%24 = load i32, i32* %7
%25 = bitcast %struct.sockaddr_un* %15 to %struct.sockaddr*
%26 = call i32 @accept(i32 %24, %struct.sockaddr* %25, i32* %16)
store i32 %26, i32* %12
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %5
store i32 0, i32* %6
br label %69
29: ; preds = %5
%30 = load i32, i32* %12
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 0
%32 = call i32 @recv(i32 %30, i8* %31, i32 1024, i32 0)
store i32 %32, i32* %13
%33 = load i32, i32* %13
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i32, i32* %12
%37 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%38 = load i32, i32* @MSG_NOSIGNAL
%39 = call i32 @send(i32 %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %37, i32 %38)
br label %65
40: ; preds = %29
%41 = load i32, i32* %13
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %64
43: ; preds = %40
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 0
%45 = load i32, i32* %12
%46 = load i32, i32* %8
%47 = load i8*, i8** %9
%48 = load i32*, i32** %10
%49 = load %struct.list*, %struct.list** %11
%50 = call i32 @handle_input(i8* %44, i32 %45, i32 %46, i8* %47, i32* %48, %struct.list* %49)
store i32 %50, i32* %14
%51 = load i32, i32* %14
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %58
53: ; preds = %43
%54 = load i32, i32* %12
%55 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%56 = load i32, i32* @MSG_NOSIGNAL
%57 = call i32 @send(i32 %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %55, i32 %56)
br label %63
58: ; preds = %43
%59 = load i32, i32* %12
%60 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%61 = load i32, i32* @MSG_NOSIGNAL
%62 = call i32 @send(i32 %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %60, i32 %61)
br label %63
63: ; preds = %58, %53
br label %64
64: ; preds = %63, %40
br label %65
65: ; preds = %64, %35
%66 = load i32, i32* %12
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %14
store i32 %68, i32* %6
br label %69
69: ; preds = %65, %28
%70 = load i32, i32* %6
ret i32 %70
}

LLVM IR (-O2):
%struct.list = type { i32 }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [5 x i8] c"NOK\0A\00"
@MSG_NOSIGNAL = global i32 0
@.str.1 = [4 x i8] c"OK\0A\00"
define i32 @handle_connection(i32 %0, i32 %1, i8* %2, i32* %3, %struct.list* %4) {
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = bitcast %struct.sockaddr_un* %6 to i8*
%10 = bitcast i32* %7 to i8*
store i32 4, i32* %7
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @O_NONBLOCK
%14 = tail call i32 @fcntl(i32 %0, i32 %12, i32 %13)
%15 = call i32 @memset(i8* %11, i8 signext 0, i32 1024)
%16 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%17 = call i32 @accept(i32 %0, %struct.sockaddr* %16, i32* %7)
%18 = icmp eq i32 %17, -1
br i1 %18, label %37, label %19
19: ; preds = %5
%20 = call i32 @recv(i32 %17, i8* %11, i32 1024, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %19
%23 = icmp eq i32 %20, 0
br i1 %23, label %34, label %24
24: ; preds = %22
%25 = call i32 @handle_input(i8* %11, i32 %17, i32 %1, i8* %2, i32* %3, %struct.list* %4)
%26 = icmp eq i32 %25, 1
%27 = select i1 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
br label %28
28: ; preds = %24, %19
%29 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %19 ], [ %27, %24 ]
%30 = phi i32 [ undef, %19 ], [ %25, %24 ]
%31 = call i32 @strlen(i8* %29)
%32 = load i32, i32* @MSG_NOSIGNAL
%33 = call i32 @send(i32 %17, i8* %29, i32 %31, i32 %32)
br label %34
34: ; preds = %28, %22
%35 = phi i32 [ undef, %22 ], [ %30, %28 ]
%36 = call i32 @close(i32 %17)
br label %37
37: ; preds = %5, %34
%38 = phi i32 [ %35, %34 ], [ 0, %5 ]
ret i32 %38
}