#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 /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serveraddr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SUN_LEN(struct sockaddr_un *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
scalar_t__ write(int, char *, int);
int send_result(char *socket_path, char *buf, int buf_len, char *recv_message,
                int recv_message_len) {
  struct sockaddr_un serveraddr;
  int sd;
  int status = 0;
  do {
    sd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sd < 0) {
      break;
    }
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, socket_path);
    if (connect(sd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)) < 0) {
      break;
    }
    if (write(sd, buf, buf_len) < 0) {
      break;
    }
    if (recv_message_len) {
      int readLen = read(sd, recv_message, recv_message_len);
      if (readLen < 0) {
        break;
      }
      recv_message[readLen] = '\0';
    }
    status = 1;
    close(sd);
  } while (0);
  return status;
}


x86 assembly (clang, -O0):
<send_result>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x38(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11cd <send_result+0x4d>
jmp    128d <send_result+0x10d>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
call   1090 <SUN_LEN@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1229 <send_result+0xa9>
jmp    128d <send_result+0x10d>
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1247 <send_result+0xc7>
jmp    128d <send_result+0x10d>
cmpl   $0x0,-0x24(%rbp)
je     127e <send_result+0xfe>
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1272 <send_result+0xf2>
jmp    128d <send_result+0x10d>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x38(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_result>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r15d
mov    %rcx,%rbp
mov    %edx,%ebx
mov    %rsi,0x10(%rsp)
mov    %rdi,%r14
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %r13d,%r13d
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1256 <send_result+0xd6>
mov    %eax,%r12d
mov    %ebx,0x4(%rsp)
mov    %r15d,%ebx
lea    0x8(%rsp),%r15
xor    %r13d,%r13d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1090 <SUN_LEN@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1256 <send_result+0xd6>
mov    %r12d,%edi
mov    0x10(%rsp),%rsi
mov    0x4(%rsp),%edx
call   1040 <write@plt>
test   %rax,%rax
js     1256 <send_result+0xd6>
test   %ebx,%ebx
je     1248 <send_result+0xc8>
mov    %ebx,%edx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1070 <read@plt>
test   %eax,%eax
js     1256 <send_result+0xd6>
mov    %eax,%eax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %r12d,%edi
call   1060 <close@plt>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @send_result(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.sockaddr_un
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
store i32 0, i32* %13
br label %15
15: ; preds = %5
%16 = load i32, i32* @AF_UNIX
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %62
22: ; preds = %15
%23 = call i32 @memset(%struct.sockaddr_un* %11, i32 0, i32 8)
%24 = load i32, i32* @AF_UNIX
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i8*, i8** %6
%29 = call i32 @strcpy(i32 %27, i8* %28)
%30 = load i32, i32* %12
%31 = bitcast %struct.sockaddr_un* %11 to %struct.sockaddr*
%32 = call i32 @SUN_LEN(%struct.sockaddr_un* %11)
%33 = call i64 @connect(i32 %30, %struct.sockaddr* %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %22
br label %62
36: ; preds = %22
%37 = load i32, i32* %12
%38 = load i8*, i8** %7
%39 = load i32, i32* %8
%40 = call i64 @write(i32 %37, i8* %38, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
br label %62
43: ; preds = %36
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %59
46: ; preds = %43
%47 = load i32, i32* %12
%48 = load i8*, i8** %9
%49 = load i32, i32* %10
%50 = call i32 @read(i32 %47, i8* %48, i32 %49)
store i32 %50, i32* %14
%51 = load i32, i32* %14
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %46
br label %62
54: ; preds = %46
%55 = load i8*, i8** %9
%56 = load i32, i32* %14
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
br label %59
59: ; preds = %54, %43
store i32 1, i32* %13
%60 = load i32, i32* %12
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %59, %53, %42, %35, %21
%63 = load i32, i32* %13
ret i32 %63
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @send_result(i8* %0, i8* nocapture %1, i32 %2, i8* nocapture %3, i32 %4) {
%6 = alloca %struct.sockaddr_un
%7 = bitcast %struct.sockaddr_un* %6 to i8*
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %36, label %12
12: ; preds = %5
%13 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strcpy(i32 %17, i8* %0)
%19 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%20 = call i32 @SUN_LEN(%struct.sockaddr_un* %6)
%21 = call i64 @connect(i32 %10, %struct.sockaddr* %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %36, label %23
23: ; preds = %12
%24 = call i64 @write(i32 %10, i8* %1, i32 %2)
%25 = icmp slt i64 %24, 0
br i1 %25, label %36, label %26
26: ; preds = %23
%27 = icmp eq i32 %4, 0
br i1 %27, label %34, label %28
28: ; preds = %26
%29 = call i32 @read(i32 %10, i8* %3, i32 %4)
%30 = icmp slt i32 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %28
%32 = zext i32 %29 to i64
%33 = getelementptr inbounds i8, i8* %3, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %31, %26
%35 = call i32 @close(i32 %10)
br label %36
36: ; preds = %28, %23, %12, %5, %34
%37 = phi i32 [ 0, %5 ], [ 0, %12 ], [ 0, %23 ], [ 1, %34 ], [ 0, %28 ]
ret i32 %37
}