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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ dump_unix(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(void *, char *);
int main(int argc, char **argv) {
  int conn_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
  if (conn_sock == -1) {
    perror("socket fail ");
    return -1;
  }
  struct sockaddr_un addr;
  bzero(&addr, sizeof(addr));
  addr.sun_family = AF_LOCAL;
  strcpy((void *)&addr.sun_path, "/tmp/sock_demo");
  if (connect(conn_sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("connect fail ");
    return -1;
  }
  dump_unix(conn_sock);
  close(conn_sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c4 <main+0x54>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <main+0xc8>
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1221 <main+0xb1>
lea    0xe0c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <main+0xc8>
mov    -0x14(%rbp),%edi
call   1040 <dump_unix@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%r15
mov    (%r15),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11f0 <main+0x80>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
lea    0xe53(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     11f9 <main+0x89>
mov    %ebx,%edi
call   1040 <dump_unix@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe09(%rip),%rdi
jmp    1200 <main+0x90>
lea    0xe1c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11e6 <main+0x76>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket fail \00"
@.str.1 = [15 x i8] c"/tmp/sock_demo\00"
@.str.2 = [14 x i8] c"connect fail \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @AF_LOCAL
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %33
15: ; preds = %2
%16 = call i32 @bzero(%struct.sockaddr_un* %7, i32 8)
%17 = load i32, i32* @AF_LOCAL
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%20 = bitcast i32* %19 to i8*
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %6
%23 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%24 = call i64 @connect(i32 %22, %struct.sockaddr* %23, i32 8)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %33
28: ; preds = %15
%29 = load i32, i32* %6
%30 = call i32 @dump_unix(i32 %29)
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
store i32 0, i32* %3
br label %33
33: ; preds = %28, %26, %13
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket fail \00"
@.str.1 = [15 x i8] c"/tmp/sock_demo\00"
@.str.2 = [14 x i8] c"connect fail \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_un
%4 = load i32, i32* @AF_LOCAL
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %2
%11 = bitcast %struct.sockaddr_un* %3 to i8*
%12 = call i32 @bzero(%struct.sockaddr_un* %3, i32 8)
%13 = load i32, i32* @AF_LOCAL
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%16 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%17 = call i64 @connect(i32 %6, %struct.sockaddr* %16, i32 8)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %24
21: ; preds = %10
%22 = call i32 @dump_unix(i32 %6)
%23 = call i32 @close(i32 %6)
br label %24
24: ; preds = %21, %19
%25 = phi i32 [ -1, %19 ], [ 0, %21 ]
br label %26
26: ; preds = %24, %8
%27 = phi i32 [ -1, %8 ], [ %25, %24 ]
ret i32 %27
}