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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ send(int, void *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int client_send_to_listener(char *app_name, void *buf, int buf_size) {
  int fd, ret = -1;
  struct sockaddr_un saddr;
  if (buf_size > (512 * 1024)) {
    printf("The max message size is %d\n", (512 * 1024));
    goto fun_exit;
  }
  if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    printf("socket failed\n");
    goto fun_exit;
  }
  bzero(&saddr, sizeof(saddr));
  saddr.sun_family = AF_UNIX;
  strcpy(saddr.sun_path, app_name);
  if (connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    printf("connect failed\n");
    goto fun_exit;
  }
  if (send(fd, buf, buf_size, 0) < 0) {
    printf("send failed\n");
    goto fun_exit;
  }
  ret = 0;
fun_exit:
  if (fd > 0)
    close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<client_send_to_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x80000,-0x14(%rbp)
jle    11af <client_send_to_listener+0x3f>
lea    0xe62(%rip),%rdi
mov    $0x80000,%esi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1270 <client_send_to_listener+0x100>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11e7 <client_send_to_listener+0x77>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1270 <client_send_to_listener+0x100>
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    123b <client_send_to_listener+0xcb>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1270 <client_send_to_listener+0x100>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0x0,%rax
jge    1269 <client_send_to_listener+0xf9>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1270 <client_send_to_listener+0x100>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1282 <client_send_to_listener+0x112>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_send_to_listener>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x80001,%edx
jl     11b0 <client_send_to_listener+0x30>
lea    0xe66(%rip),%rdi
mov    $0x80000,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
jmp    125e <client_send_to_listener+0xde>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e29(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     122d <client_send_to_listener+0xad>
mov    %eax,%r12d
mov    %rsp,%r13
mov    %r13,%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1240 <client_send_to_listener+0xc0>
xor    %ebx,%ebx
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %rax,%rax
jns    1251 <client_send_to_listener+0xd1>
lea    0xdf1(%rip),%rdi
jmp    1247 <client_send_to_listener+0xc7>
lea    0xe03(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
jmp    125e <client_send_to_listener+0xde>
lea    0xde1(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
test   %r12d,%r12d
je     125e <client_send_to_listener+0xde>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x8,%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 }
@.str = [28 x i8] c"The max message size is %d\0A\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"socket failed\0A\00"
@.str.2 = [16 x i8] c"connect failed\0A\00"
@.str.3 = [13 x i8] c"send failed\0A\00"
define i32 @client_send_to_listener(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 -1, i32* %8
%10 = load i32, i32* %6
%11 = icmp sgt i32 %10, 524288
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 524288)
br label %44
14: ; preds = %3
%15 = load i32, i32* @AF_UNIX
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %44
21: ; preds = %14
%22 = call i32 @bzero(%struct.sockaddr_un* %9, i32 8)
%23 = load i32, i32* @AF_UNIX
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i8*, i8** %4
%28 = call i32 @strcpy(i32 %26, i8* %27)
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%31 = call i64 @connect(i32 %29, %struct.sockaddr* %30, i32 8)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %44
35: ; preds = %21
%36 = load i32, i32* %7
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%39 = call i64 @send(i32 %36, i8* %37, i32 %38, i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %44
43: ; preds = %35
store i32 0, i32* %8
br label %44
44: ; preds = %43, %41, %33, %19, %12
%45 = load i32, i32* %7
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* %8
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [28 x i8] c"The max message size is %d\0A\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@str = [12 x i8] c"send failed\00"
@str.4 = [15 x i8] c"connect failed\00"
@str.5 = [14 x i8] c"socket failed\00"
define i32 @client_send_to_listener(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = bitcast %struct.sockaddr_un* %4 to i8*
%6 = icmp sgt i32 %2, 524288
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 524288)
br label %37
9: ; preds = %3
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
br label %37
16: ; preds = %9
%17 = call i32 @bzero(%struct.sockaddr_un* %4, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i32 @strcpy(i32 %21, i8* %0)
%23 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%24 = call i64 @connect(i32 %12, %struct.sockaddr* %23, i32 8)
%25 = icmp slt i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %16
%27 = call i64 @send(i32 %12, i8* %1, i32 %2, i32 0)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26, %16
%30 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.4, i64 0, i64 0), %16 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0), %26 ]
%31 = call i32 @puts(i8* dereferenceable(1) %30)
br label %32
32: ; preds = %29, %26
%33 = phi i32 [ 0, %26 ], [ -1, %29 ]
%34 = icmp eq i32 %12, 0
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 @close(i32 %12)
br label %37
37: ; preds = %14, %7, %35, %32
%38 = phi i32 [ %33, %35 ], [ %33, %32 ], [ -1, %7 ], [ -1, %14 ]
ret i32 %38
}