#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, size_t);
int /*<<< orphan*/ copyData(int /*<<< orphan*/, int);
int /*<<< orphan*/ die(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int main(void) {
  struct sockaddr_un address;
  int sock;
  size_t addrLength;
  if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
    die("socket");
  address.sun_family = AF_UNIX;
  strcpy(address.sun_path, "./sample-socket");
  addrLength = sizeof(address.sun_family) + strlen(address.sun_path);
  if (connect(sock, (struct sockaddr *)&address, addrLength))
    die("connect");
  copyData(0, sock);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11b0 <main+0x40>
lea    0xe5e(%rip),%rdi
call   1060 <die@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe3a(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%edi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <connect@plt>
cmp    $0x0,%rax
je     1203 <main+0x93>
lea    0xe12(%rip),%rdi
call   1060 <die@plt>
mov    -0x14(%rbp),%esi
xor    %edi,%edi
call   1070 <copyData@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a0 <main+0x30>
lea    0xe6e(%rip),%rdi
call   1060 <die@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0xe4c(%rip),%rsi
call   1030 <strcpy@plt>
call   1040 <strlen@plt>
movslq %eax,%rdx
add    $0x4,%rdx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1080 <connect@plt>
test   %rax,%rax
je     11e2 <main+0x72>
lea    0xe33(%rip),%rdi
call   1060 <die@plt>
xor    %edi,%edi
mov    %ebx,%esi
call   1070 <copyData@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_UNIX = global i32 0
@.str.1 = [16 x i8] c"./sample-socket\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @PF_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 %9
7: ; preds = %0
%8 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @strlen(i32 undef)
%14 = sext i32 %13 to i64
%15 = add nsw i64 %14, 4
%16 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%17 = call i64 @connect(i32 %5, %struct.sockaddr* %16, i64 %15)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %9
%20 = call i32 @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %9
%22 = call i32 @copyData(i32 0, i32 %5)
%23 = call i32 @close(i32 %5)
ret i32 0
}