#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 PATH_MAX;
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 sa;
  memset(&sa, 0, sizeof(struct sockaddr_un));
  sa.sun_family = AF_UNIX;
  snprintf(sa.sun_path, PATH_MAX, "/var/local/gridhost/raygan/local");
  int sfd;
  sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sfd == -1) {
    printf("socket failed\n");
    return -1;
  }
  if (connect(sfd, (struct sockaddr *)&sa, sizeof(struct sockaddr_un)) != 0) {
    printf("connect failed.\n");
    return -1;
  }
  int n;
  char buf[256];
  n = snprintf(buf, 256, "bitch!");
  write(sfd, buf, n);
  n = read(sfd, buf, 256);
  buf[n] = '\0';
  printf("incoming msg: %s\n", buf);
  close(sfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdx
call   1050 <snprintf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1207 <main+0x87>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <main+0x130>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     123c <main+0xbc>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <main+0x130>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
lea    0xdf2(%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    0xdae(%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    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2e2f(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdx
call   1060 <snprintf@plt>
mov    (%rbx),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1206 <main+0x76>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   10a0 <connect@plt>
test   %rax,%rax
je     1219 <main+0x89>
lea    0xe36(%rip),%rdi
jmp    120d <main+0x7d>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    126e <main+0xde>
lea    0xe01(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   1060 <snprintf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1090 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdcd(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@PATH_MAX = global i32 0
@.str = [33 x i8] c"/var/local/gridhost/raygan/local\00"
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"socket failed\0A\00"
@.str.2 = [17 x i8] c"connect failed.\0A\00"
@.str.3 = [7 x i8] c"bitch!\00"
@.str.4 = [18 x i8] c"incoming msg: %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 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%7 = load i32, i32* @AF_UNIX
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%10 = load i8*, i8** %9
%11 = load i32, i32* @PATH_MAX
%12 = call i32 @snprintf(i8* %10, i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
20: ; preds = %0
%21 = load i32, i32* %3
%22 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%23 = call i64 @connect(i32 %21, %struct.sockaddr* %22, i32 16)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
27: ; preds = %20
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%29 = call i32 @snprintf(i8* %28, i32 256, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 %29, i32* %4
%30 = load i32, i32* %3
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%32 = load i32, i32* %4
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
%34 = load i32, i32* %3
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 256)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
store i32 0, i32* %1
br label %44
44: ; preds = %27, %25, %18
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@PATH_MAX = global i32 0
@.str = [33 x i8] c"/var/local/gridhost/raygan/local\00"
@SOCK_STREAM = global i32 0
@.str.3 = [7 x i8] c"bitch!\00"
@.str.4 = [18 x i8] c"incoming msg: %s\0A\00"
@str = [16 x i8] c"connect failed.\00"
@str.5 = [14 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 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 16)
%5 = load i32, i32* @AF_UNIX
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%8 = load i8*, i8** %7
%9 = load i32, i32* @PATH_MAX
%10 = call i32 @snprintf(i8* %8, i32 %9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
br label %32
17: ; preds = %0
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i64 @connect(i32 %13, %struct.sockaddr* %18, i32 16)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %32
23: ; preds = %17
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%25 = call i32 @snprintf(i8* %24, i32 256, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @write(i32 %13, i8* %24, i32 %25)
%27 = call i32 @read(i32 %13, i8* %24, i32 256)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%31 = call i32 @close(i32 %13)
br label %32
32: ; preds = %23, %21, %15
%33 = phi i32 [ -1, %15 ], [ -1, %21 ], [ 0, %23 ]
ret i32 %33
}