#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 *member_1;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SUN_LEN(struct sockaddr_un *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main() {
  int client_socket = socket(AF_UNIX, SOCK_STREAM, 0);
  if (client_socket < 0) {
    perror("socket");
    exit(1);
  }
  struct sockaddr_un server_address = {AF_UNIX, "/tmp/my_socket"};
  int ret = connect(client_socket, (struct sockaddr *)&server_address,
                    SUN_LEN(&server_address));
  if (ret < 0) {
    perror("connect");
    exit(1);
  }
  char message_to_server[10] = "hello";
  int written_bytes =
      write(client_socket, message_to_server, strlen(message_to_server));
  if (written_bytes <= 0) {
    perror("write");
    exit(1);
  }
  printf("Message sent\n");
  close(client_socket);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11db <main+0x4b>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e06(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x18(%rbp)
lea    0xe10(%rip),%rax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x18(%rbp),%rdi
call   10a0 <SUN_LEN@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1090 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    123b <main+0xab>
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0xde9(%rip),%rax
mov    %rax,-0x26(%rbp)
mov    0xde6(%rip),%ax
mov    %ax,-0x1e(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x26(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x26(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     1299 <main+0x109>
lea    0xd8d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd7d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e4a(%rip),%r14
mov    (%r14),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     123b <main+0xab>
mov    %eax,%ebx
movslq (%r14),%rax
mov    %rax,0x18(%rsp)
lea    0xe36(%rip),%rax
mov    %eax,0x20(%rsp)
lea    0x18(%rsp),%r14
mov    %r14,%rdi
call   10a0 <SUN_LEN@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1090 <connect@plt>
test   %eax,%eax
js     1244 <main+0xb4>
movabs $0x6f6c6c6568,%rax
mov    %rax,0x8(%rsp)
movw   $0x0,0x10(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
jle    124d <main+0xbd>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdc6(%rip),%rdi
jmp    1254 <main+0xc4>
lea    0xdc4(%rip),%rdi
jmp    1254 <main+0xc4>
lea    0xdc3(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [15 x i8] c"/tmp/my_socket\00"
@.str.2 = [8 x i8] c"connect\00"
@__const.main.message_to_server = [10 x i8] c"hello\00\00\00\00\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [14 x i8] c"Message sent\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = alloca [10 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%17 = load i32, i32* @AF_UNIX
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %16
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 ptrtoint ([15 x i8]* @.str.1 to i32), i32* %20
%21 = load i32, i32* %2
%22 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%23 = call i32 @SUN_LEN(%struct.sockaddr_un* %3)
%24 = call i32 @connect(i32 %21, %struct.sockaddr* %22, i32 %23)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %15
%31 = bitcast [10 x i8]* %5 to i8*
%32 = load i32, i32* %2
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%34 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = call i32 @write(i32 %32, i8* %33, i32 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp sle i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %30
%43 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32, i32* %2
%45 = call i32 @close(i32 %44)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [15 x i8] c"/tmp/my_socket\00"
@.str.2 = [8 x i8] c"connect\00"
@__const.main.message_to_server = [10 x i8] c"hello\00\00\00\00\00"
@.str.3 = [6 x i8] c"write\00"
@str = [13 x i8] c"Message sent\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [10 x i8]
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = bitcast %struct.sockaddr_un* %1 to i8*
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%13 = load i32, i32* @AF_UNIX
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %12
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 ptrtoint ([15 x i8]* @.str.1 to i32), i32* %16
%17 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%18 = call i32 @SUN_LEN(%struct.sockaddr_un* %1)
%19 = call i32 @connect(i32 %5, %struct.sockaddr* %17, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %10
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %10
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = call i32 @write(i32 %5, i8* %25, i32 %26)
%28 = icmp slt i32 %27, 1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%34 = call i32 @close(i32 %5)
ret i32 0
}