#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int snprintf(char *, int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  struct sockaddr_un address;
  int socket_fd, nbytes;
  char buffer[256];
  socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (socket_fd < 0) {
    printf("socket() failed\n");
    return 1;
  }
  memset(&address, 0, sizeof(struct sockaddr_un));
  address.sun_family = AF_UNIX;
  snprintf(address.sun_path, 108, "./demo_socket");
  if (connect(socket_fd, (struct sockaddr *)&address,
              sizeof(struct sockaddr_un)) != 0) {
    printf("connect() failed\n");
    return 1;
  }
  nbytes = snprintf(buffer, 256, "hello from a client");
  write(socket_fd, buffer, nbytes);
  nbytes = read(socket_fd, buffer, 256);
  buffer[nbytes] = 0;
  printf("MESSAGE FROM SERVER: %s\n", buffer);
  close(socket_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d2 <main+0x52>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ac <main+0x12c>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x6c,%esi
lea    0xe13(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     1238 <main+0xb8>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ac <main+0x12c>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
lea    0xde6(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     120d <main+0x7d>
mov    %eax,%ebp
mov    %rsp,%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rdi
lea    0xe1a(%rip),%rdx
mov    $0x6c,%esi
call   1060 <snprintf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   10a0 <connect@plt>
test   %rax,%rax
je     1220 <main+0x90>
lea    0xe30(%rip),%rdi
jmp    1214 <main+0x84>
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1273 <main+0xe3>
lea    0xde7(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   1060 <snprintf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1090 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdc0(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"socket() failed\0A\00"
@AF_UNIX = global i32 0
@.str.1 = [14 x i8] c"./demo_socket\00"
@.str.2 = [18 x i8] c"connect() failed\0A\00"
@.str.3 = [20 x i8] c"hello from a client\00"
@.str.4 = [25 x i8] c"MESSAGE FROM SERVER: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @PF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %43
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @snprintf(i8* %18, i32 108, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%22 = call i64 @connect(i32 %20, %struct.sockaddr* %21, i32 16)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %43
26: ; preds = %13
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%28 = call i32 @snprintf(i8* %27, i32 256, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 %28, i32* %4
%29 = load i32, i32* %3
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%31 = load i32, i32* %4
%32 = call i32 @write(i32 %29, i8* %30, i32 %31)
%33 = load i32, i32* %3
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 256)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
store i32 0, i32* %1
br label %43
43: ; preds = %26, %24, %11
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str.1 = [14 x i8] c"./demo_socket\00"
@.str.3 = [20 x i8] c"hello from a client\00"
@.str.4 = [25 x i8] c"MESSAGE FROM SERVER: %s\0A\00"
@str = [17 x i8] c"connect() failed\00"
@str.5 = [16 x i8] c"socket() failed\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [256 x i8]
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
br label %31
11: ; preds = %0
%12 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 16)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @snprintf(i8* %16, i32 108, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i64 @connect(i32 %7, %struct.sockaddr* %18, i32 16)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %11
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %31
23: ; preds = %11
%24 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @write(i32 %7, i8* %4, i32 %24)
%26 = call i32 @read(i32 %7, i8* %4, i32 256)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%30 = call i32 @close(i32 %7)
br label %31
31: ; preds = %23, %21, %9
%32 = phi i32 [ 1, %9 ], [ 1, %21 ], [ 0, %23 ]
ret i32 %32
}