#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 /*<<< orphan*/ from;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int first_handler(int fd, char *buf) {
  int connected_fd;
  struct sockaddr_un from;
  int lenfrom = sizeof(from);
  bzero(&from, sizeof(from));
  if ((connected_fd = accept(fd, (struct sockaddr *)&from, &lenfrom)) < 0) {
    printf("accept failed\n");
    return -1;
  }
  int nRecvSize = 0;
  nRecvSize = recv(connected_fd, buf, (512 * 1024), 0);
  if (nRecvSize < 0) {
    printf("recv failed\n");
    return -1;
  }
  close(connected_fd);
  return nRecvSize;
}


x86 assembly (clang, -O0):
<first_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x4,%esi
call   1060 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11a8 <first_handler+0x58>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <first_handler+0xa5>
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x80000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e7 <first_handler+0x97>
lea    0xe39(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <first_handler+0xa5>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<first_handler>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1060 <bzero@plt>
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <accept@plt>
test   %eax,%eax
js     11ac <first_handler+0x5c>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x80000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11b5 <first_handler+0x65>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11c6 <first_handler+0x76>
lea    0xe59(%rip),%rdi
jmp    11bc <first_handler+0x6c>
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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