#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 /*<<< orphan*/ scanf(char *, 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";
  printf("Enter message to server: \n");
  scanf("%s", message_to_server);
  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    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11eb <main+0x4b>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2df6(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x18(%rbp)
lea    0xe00(%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   10b0 <SUN_LEN@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   10a0 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    124b <main+0xab>
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0xdf7(%rip),%rax
mov    %rax,-0x26(%rbp)
mov    0xdf4(%rip),%ax
mov    %ax,-0x1e(%rbp)
lea    0xdaf(%rip),%rdi
call   1050 <printf@plt>
lea    -0x26(%rbp),%rsi
lea    0xdba(%rip),%rdi
call   1070 <scanf@plt>
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     12c5 <main+0x125>
lea    0xd7f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd6f(%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    0x2e3a(%rip),%r14
mov    (%r14),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1266 <main+0xc6>
mov    %eax,%ebx
movslq (%r14),%rax
mov    %rax,0x18(%rsp)
lea    0xe26(%rip),%rax
mov    %eax,0x20(%rsp)
lea    0x18(%rsp),%r14
mov    %r14,%rdi
call   10b0 <SUN_LEN@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     126f <main+0xcf>
movabs $0x6f6c6c6568,%rax
mov    %rax,0x8(%rsp)
movw   $0x0,0x10(%rsp)
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf3(%rip),%rdi
lea    0x8(%rsp),%r14
mov    %r14,%rsi
call   1070 <scanf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
jle    1278 <main+0xd8>
lea    0xdea(%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    0xd9b(%rip),%rdi
jmp    127f <main+0xdf>
lea    0xd99(%rip),%rdi
jmp    127f <main+0xdf>
lea    0xd9b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <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 = [27 x i8] c"Enter message to server: \0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [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 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%34 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %33)
%35 = load i32, i32* %2
%36 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%37 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = call i32 @write(i32 %35, i8* %36, i32 %38)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = icmp sle i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %30
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30
%46 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%47 = load i32, i32* %2
%48 = call i32 @close(i32 %47)
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.4 = [3 x i8] c"%s\00"
@.str.5 = [6 x i8] c"write\00"
@str = [26 x i8] c"Enter message to server: \00"
@str.7 = [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 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%27 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%28 = call i32 @strlen(i8* %25)
%29 = call i32 @write(i32 %5, i8* %25, i32 %28)
%30 = icmp slt i32 %29, 1
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %24
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0))
%36 = call i32 @close(i32 %5)
ret i32 0
}