#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;
};
typedef int /*<<< orphan*/ serv;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int read_stream(int, char *, int);
scalar_t__ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  struct sockaddr_un serv;
  char request[400], reply[400];
  int sockfd, n;
  if (argc < 2)
    err_quit("usage: unixcli <pathname of server>");
  if ((sockfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
    err_sys("socket error");
  memset(&serv, 0, sizeof(serv));
  serv.sun_family = AF_UNIX;
  strncpy(serv.sun_path, argv[1], sizeof(serv.sun_path));
  if (connect(sockfd, (struct sockaddr *)&serv, sizeof(serv)) < 0)
    err_sys("connect error");
  strncpy(request, "Hello, Unix domain!\n", 400);
  if (write(sockfd, request, 400) != 400)
    err_sys("write error");
  if (shutdown(sockfd, 1) < 0)
    err_sys("shutdown error");
  if ((n = read_stream(sockfd, reply, 400)) < 0)
    err_sys("read_stream error");
  reply[n] = '\0';
  printf("%s\n", reply);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11cf <main+0x2f>
lea    0xe36(%rip),%rdi
call   1090 <err_quit@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x344(%rbp)
cmp    $0x0,%eax
jge    1203 <main+0x63>
lea    0xe26(%rip),%rdi
call   1040 <err_sys@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x344(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    125f <main+0xbf>
lea    0xdd7(%rip),%rdi
call   1040 <err_sys@plt>
lea    -0x1b0(%rbp),%rdi
lea    0xdd2(%rip),%rsi
mov    $0x190,%edx
call   1030 <strncpy@plt>
mov    -0x344(%rbp),%edi
lea    -0x1b0(%rbp),%rsi
mov    $0x190,%edx
call   1050 <write@plt>
cmp    $0x190,%eax
je     12a5 <main+0x105>
lea    0xdb4(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x344(%rbp),%edi
mov    $0x1,%esi
call   1060 <shutdown@plt>
cmp    $0x0,%rax
jge    12cb <main+0x12b>
lea    0xd9a(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x344(%rbp),%edi
lea    -0x340(%rbp),%rsi
mov    $0x190,%edx
call   10a0 <read_stream@plt>
mov    %eax,-0x348(%rbp)
cmp    $0x0,%eax
jge    12fd <main+0x15d>
lea    0xd77(%rip),%rdi
call   1040 <err_sys@plt>
movslq -0x348(%rbp),%rax
movb   $0x0,-0x340(%rbp,%rax,1)
lea    -0x340(%rbp),%rsi
lea    0xd67(%rip),%rdi
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x330,%rsp
mov    %rsi,%r14
cmp    $0x1,%edi
jg     11c0 <main+0x20>
lea    0xe45(%rip),%rdi
call   1090 <err_quit@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11eb <main+0x4b>
lea    0xe3e(%rip),%rdi
call   1040 <err_sys@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rdi
mov    0x8(%r14),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   10b0 <connect@plt>
test   %rax,%rax
jns    123c <main+0x9c>
lea    0xdfa(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdfc(%rip),%rsi
lea    0x1a0(%rsp),%r14
mov    %r14,%rdi
mov    $0x190,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x190,%edx
call   1050 <write@plt>
cmp    $0x190,%eax
je     127a <main+0xda>
lea    0xddf(%rip),%rdi
call   1040 <err_sys@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <shutdown@plt>
test   %rax,%rax
jns    1297 <main+0xf7>
lea    0xdce(%rip),%rdi
call   1040 <err_sys@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x190,%edx
call   10a0 <read_stream@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12ba <main+0x11a>
lea    0xdba(%rip),%rdi
call   1040 <err_sys@plt>
movslq %ebx,%rax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdb8(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x330,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [36 x i8] c"usage: unixcli <pathname of server>\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@AF_UNIX = global i32 0
@.str.2 = [14 x i8] c"connect error\00"
@.str.3 = [21 x i8] c"Hello, Unix domain!\0A\00"
@.str.4 = [12 x i8] c"write error\00"
@.str.5 = [15 x i8] c"shutdown error\00"
@.str.6 = [18 x i8] c"read_stream error\00"
@.str.7 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_un
%7 = alloca [400 x i8]
%8 = alloca [400 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @err_quit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i32, i32* @PF_UNIX
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 16)
%24 = load i32, i32* @AF_UNIX
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @strncpy(i8* %27, i8* %30, i32 8)
%32 = load i32, i32* %9
%33 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%34 = call i64 @connect(i32 %32, %struct.sockaddr* %33, i32 16)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %22
%37 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %22
%39 = getelementptr inbounds [400 x i8], [400 x i8]* %7, i64 0, i64 0
%40 = call i32 @strncpy(i8* %39, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 400)
%41 = load i32, i32* %9
%42 = getelementptr inbounds [400 x i8], [400 x i8]* %7, i64 0, i64 0
%43 = call i32 @write(i32 %41, i8* %42, i32 400)
%44 = icmp ne i32 %43, 400
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %38
%48 = load i32, i32* %9
%49 = call i64 @shutdown(i32 %48, i32 1)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = load i32, i32* %9
%55 = getelementptr inbounds [400 x i8], [400 x i8]* %8, i64 0, i64 0
%56 = call i32 @read_stream(i32 %54, i8* %55, i32 400)
store i32 %56, i32* %10
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %53
%61 = load i32, i32* %10
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [400 x i8], [400 x i8]* %8, i64 0, i64 %62
store i8 0, i8* %63
%64 = getelementptr inbounds [400 x i8], [400 x i8]* %8, i64 0, i64 0
%65 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %64)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [36 x i8] c"usage: unixcli <pathname of server>\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@AF_UNIX = global i32 0
@.str.2 = [14 x i8] c"connect error\00"
@.str.3 = [21 x i8] c"Hello, Unix domain!\0A\00"
@.str.4 = [12 x i8] c"write error\00"
@.str.5 = [15 x i8] c"shutdown error\00"
@.str.6 = [18 x i8] c"read_stream error\00"
@.str.7 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [400 x i8]
%5 = alloca [400 x i8]
%6 = bitcast %struct.sockaddr_un* %3 to i8*
%7 = getelementptr inbounds [400 x i8], [400 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [400 x i8], [400 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @err_quit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* @PF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%21 = load i32, i32* @AF_UNIX
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strncpy(i8* %24, i8* %26, i32 8)
%28 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%29 = call i64 @connect(i32 %15, %struct.sockaddr* %28, i32 16)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %19
%32 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %19
%34 = call i32 @strncpy(i8* %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 400)
%35 = call i32 @write(i32 %15, i8* %7, i32 400)
%36 = icmp eq i32 %35, 400
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = call i64 @shutdown(i32 %15, i32 1)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = call i32 @read_stream(i32 %15, i8* %8, i32 400)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
%50 = sext i32 %45 to i64
%51 = getelementptr inbounds [400 x i8], [400 x i8]* %5, i64 0, i64 %50
store i8 0, i8* %51
%52 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %8)
ret i32 0
}