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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int fd;
  struct sockaddr_un addr = {0};
  int r;
  char buf[300];
  printf("pid : %d,fd : %d\n", getpid(), fd);
  fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (fd == -1) {
    printf("create socket error:%m\n");
    exit(-1);
  }
  addr.sun_family = AF_UNIX;
  memcpy(addr.sun_path, "my.sock", strlen("my.sock"));
  r = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (fd == -1) {
    printf("connect socket error:%m\n");
    exit(-1);
  }
  write(fd, "Hi,mizu!!!!!!", strlen("Hi,mizu!!!!!!"));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    121a <main+0x7a>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x154(%rbp)
lea    0xdf4(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x154(%rbp),%edi
mov    %eax,%edx
lea    0xde0(%rip),%rsi
call   1090 <memcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1285 <main+0xe5>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x158(%rbp)
lea    0xdb6(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x158(%rbp),%edi
mov    %eax,%edx
lea    0xda2(%rip),%rsi
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e22(%rip),%r14
mov    (%r14),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     123f <main+0xaf>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    0xe3c(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
xor    %edi,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1080 <memcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
lea    0xe18(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdcc(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"pid : %d,fd : %d\0A\00"
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [24 x i8] c"create socket error:%m\0A\00"
@.str.2 = [8 x i8] c"my.sock\00"
@.str.3 = [25 x i8] c"connect socket error:%m\0A\00"
@.str.4 = [14 x i8] c"Hi,mizu!!!!!!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = alloca [300 x i8]
store i32 0, i32* %1
%6 = bitcast %struct.sockaddr_un* %3 to i8*
%7 = call i32 (...) @getpid()
%8 = load i32, i32* %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %0
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @memcpy(i32 %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%27 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %4
%28 = load i32, i32* %2
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %18
%34 = load i32, i32* %2
%35 = call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @write(i32 %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"pid : %d,fd : %d\0A\00"
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [24 x i8] c"create socket error:%m\0A\00"
@.str.2 = [8 x i8] c"my.sock\00"
@.str.4 = [14 x i8] c"Hi,mizu!!!!!!\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %3, i32 undef)
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%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 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @memcpy(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%18 = call i32 @connect(i32 %7, %struct.sockaddr* %17, i32 12)
%19 = call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @write(i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = call i32 @close(i32 %7)
ret i32 0
}