#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;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cookie;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int read(int, char *, int);
scalar_t__ strncmp(char *, char *, int);
int write(int, char *, int);
int bme_handle_new_client(int fd) {
  struct sockaddr_un addr;
  socklen_t addr_len;
  int size, client;
  char cookie[8];
  addr_len = sizeof(addr);
  client = accept(fd, (struct sockaddr *)&addr, &addr_len);
  if (client < 0)
    return -1;
  size = sizeof(cookie);
  if ((read(client, cookie, size) < size) ||
      (strncmp(cookie, "BMentity", size)) || (write(client, "\n", 1) < 1)) {
    close(client);
    return -1;
  }
  return client;
}


x86 assembly (clang, -O0):
<bme_handle_new_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118b <bme_handle_new_client+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <bme_handle_new_client+0xae>
movl   $0x8,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <read@plt>
cmp    -0x18(%rbp),%eax
jl     11e4 <bme_handle_new_client+0x94>
lea    -0x24(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xe48(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11e4 <bme_handle_new_client+0x94>
mov    -0x1c(%rbp),%edi
lea    0xe38(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jge    11f8 <bme_handle_new_client+0xa8>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <bme_handle_new_client+0xae>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bme_handle_new_client>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1070 <accept@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11b0 <bme_handle_new_client+0x60>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <read@plt>
cmp    $0x8,%eax
jl     11a9 <bme_handle_new_client+0x59>
lea    0xe6b(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    $0x8,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11b9 <bme_handle_new_client+0x69>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe49(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
test   %eax,%eax
jle    11a9 <bme_handle_new_client+0x59>
mov    %ebp,%ebx
jmp    11b0 <bme_handle_new_client+0x60>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"BMentity\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @bme_handle_new_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8 x i8]
store i32 %0, i32* %3
store i32 4, i32* %5
%9 = load i32, i32* %3
%10 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %5)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %36
15: ; preds = %1
store i32 8, i32* %6
%16 = load i32, i32* %7
%17 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%18 = load i32, i32* %6
%19 = call i32 @read(i32 %16, i8* %17, i32 %18)
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %31, label %22
22: ; preds = %15
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%24 = load i32, i32* %6
%25 = call i64 @strncmp(i8* %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = load i32, i32* %7
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%30 = icmp slt i32 %29, 1
br i1 %30, label %31, label %34
31: ; preds = %27, %22, %15
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %2
br label %36
34: ; preds = %27
%35 = load i32, i32* %7
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %31, %14
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"BMentity\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @bme_handle_new_client(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca [8 x i8]
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
store i32 4, i32* %3
%8 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%9 = call i32 @accept(i32 %0, %struct.sockaddr* %8, i32* %3)
%10 = icmp slt i32 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %1
%12 = call i32 @read(i32 %9, i8* %7, i32 8)
%13 = icmp slt i32 %12, 8
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @write(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %17, %14, %11
%21 = call i32 @close(i32 %9)
br label %22
22: ; preds = %17, %1, %20
%23 = phi i32 [ -1, %20 ], [ -1, %1 ], [ %9, %17 ]
ret i32 %23
}