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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int connect_fd;
  struct sockaddr_un srv_addr;
  char snd_buf[1024];
  char rcv_buf[1024];
  int ret;
  int i;
  connect_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (connect_fd < 0) {
    perror("client create socket failed");
    return 1;
  }
  srv_addr.sun_family = AF_UNIX;
  strcpy(srv_addr.sun_path, "/tmp/UNIX.domain");
  ret = connect(connect_fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
  if (ret == -1) {
    perror("connect to server failed!");
    close(connect_fd);
    unlink("/tmp/UNIX.domain");
    return 1;
  }
  memset(rcv_buf, 0, 1024);
  int rcv_num = read(connect_fd, rcv_buf, sizeof(rcv_buf));
  printf("receive message from server (%d) :%s\n", rcv_num, rcv_buf);
  memset(snd_buf, 0, 1024);
  strcpy(snd_buf, "message from client");
  printf("sizeof(snd_buf): %d\n", sizeof(snd_buf));
  sleep(2000);
  for (i = 0; i < 4; i++) {
    write(connect_fd, snd_buf, sizeof(snd_buf));
  }
  close(connect_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1200 <main+0x50>
lea    0xe11(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x195>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <connect@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0xffffffff,-0x824(%rbp)
jne    126c <main+0xbc>
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
lea    0xdc1(%rip),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x195>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x82c(%rbp)
mov    -0x82c(%rbp),%esi
lea    -0x820(%rbp),%rdx
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x420(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xda0(%rip),%rdi
mov    $0x400,%esi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x7d0,%edi
call   10c0 <sleep@plt>
movl   $0x0,-0x828(%rbp)
cmpl   $0x4,-0x828(%rbp)
jge    1336 <main+0x186>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <write@plt>
mov    -0x828(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x828(%rbp)
jmp    1301 <main+0x151>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x810,%rsp
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12dd <main+0x12d>
mov    %eax,%ebx
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xe2a(%rip),%rsi
call   1040 <strcpy@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     12f1 <main+0x141>
lea    0x10(%rsp),%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
lea    0xe0b(%rip),%rdi
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x410(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    0xe07(%rip),%rsi
mov    %rbp,%rdi
call   1040 <strcpy@plt>
lea    0xe0c(%rip),%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x7d0,%edi
call   10c0 <sleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x810,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd1c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r14d
jmp    12ce <main+0x11e>
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xd11(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%r14d
jmp    12ce <main+0x11e>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"client create socket failed\00"
@AF_UNIX = global i32 0
@.str.1 = [17 x i8] c"/tmp/UNIX.domain\00"
@.str.2 = [26 x i8] c"connect to server failed!\00"
@.str.3 = [38 x i8] c"receive message from server (%d) :%s\0A\00"
@.str.4 = [20 x i8] c"message from client\00"
@.str.5 = [21 x i8] c"sizeof(snd_buf): %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @PF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %60
16: ; preds = %0
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 16)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
%31 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %60
32: ; preds = %16
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 0, i32 1024)
%35 = load i32, i32* %2
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 1024)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %38, i8* %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%42 = call i32 @memset(i8* %41, i32 0, i32 1024)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%44 = call i32 @strcpy(i8* %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 1024)
%46 = call i32 @sleep(i32 2000)
store i32 0, i32* %7
br label %47
47: ; preds = %54, %32
%48 = load i32, i32* %7
%49 = icmp slt i32 %48, 4
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i32, i32* %2
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%53 = call i32 @write(i32 %51, i8* %52, i32 1024)
br label %54
54: ; preds = %50
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %47
57: ; preds = %47
%58 = load i32, i32* %2
%59 = call i32 @close(i32 %58)
store i32 0, i32* %1
br label %60
60: ; preds = %57, %27, %14
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"client create socket failed\00"
@AF_UNIX = global i32 0
@.str.1 = [17 x i8] c"/tmp/UNIX.domain\00"
@.str.2 = [26 x i8] c"connect to server failed!\00"
@.str.3 = [38 x i8] c"receive message from server (%d) :%s\0A\00"
@.str.4 = [20 x i8] c"message from client\00"
@.str.5 = [21 x i8] c"sizeof(snd_buf): %d\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = bitcast %struct.sockaddr_un* %1 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %37
13: ; preds = %0
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = tail call i32 @strcpy(i8* undef, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%17 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%18 = call i32 @connect(i32 %9, %struct.sockaddr* %17, i32 16)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @close(i32 %9)
%23 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %37
24: ; preds = %13
%25 = call i32 @memset(i8* %6, i32 0, i32 1024)
%26 = call i32 @read(i32 %9, i8* %6, i32 1024)
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %26, i8* %6)
%28 = call i32 @memset(i8* %5, i32 0, i32 1024)
%29 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 1024)
%31 = call i32 @sleep(i32 2000)
%32 = call i32 @write(i32 %9, i8* %5, i32 1024)
%33 = call i32 @write(i32 %9, i8* %5, i32 1024)
%34 = call i32 @write(i32 %9, i8* %5, i32 1024)
%35 = call i32 @write(i32 %9, i8* %5, i32 1024)
%36 = call i32 @close(i32 %9)
br label %37
37: ; preds = %24, %20, %11
%38 = phi i32 [ 1, %11 ], [ 1, %20 ], [ 0, %24 ]
ret i32 %38
}