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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ STDIN_FILENO;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int main(void) {
  struct sockaddr_un addr;
  int sfd;
  ssize_t numRead;
  char buf[100];
  sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sfd == -1)
    exit(-1);
  memset(&addr, 0, sizeof(struct sockaddr_un));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, "/tmp/us_xfr", sizeof(addr.sun_path) - 1);
  if (connect(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1)
    exit(-2);
  while ((numRead = read(STDIN_FILENO, buf, 100)) > 0)
    if (write(sfd, buf, numRead) != numRead)
      perror("parital/failed write ");
  if (numRead == -1)
    exit(-3);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c2 <main+0x42>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe18(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1216 <main+0x96>
mov    $0xfffffffe,%edi
call   1080 <exit@plt>
jmp    121b <main+0x9b>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1267 <main+0xe7>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
cmp    -0x18(%rbp),%eax
je     1262 <main+0xe2>
lea    0xdaf(%rip),%rdi
call   1070 <perror@plt>
jmp    121b <main+0x9b>
cmpl   $0xffffffff,-0x18(%rbp)
jne    127b <main+0xfb>
mov    $0xfffffffd,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    0x2e52(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1279 <main+0xf9>
mov    %eax,%r15d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xe25(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0x103>
mov    0x2dcb(%rip),%r12
mov    (%r12),%edi
lea    0x10(%rsp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1262 <main+0xe2>
lea    0x10(%rsp),%rbx
lea    0xde2(%rip),%r14
jmp    1247 <main+0xc7>
nopl   0x0(%rax)
mov    (%r12),%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1262 <main+0xe2>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     1230 <main+0xb0>
mov    %r14,%rdi
call   1070 <perror@plt>
jmp    1230 <main+0xb0>
cmp    $0xffffffff,%ebp
je     128d <main+0x10d>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    $0xfffffffe,%edi
call   1080 <exit@plt>
mov    $0xfffffffd,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"/tmp/us_xfr\00"
@STDIN_FILENO = global i32 0
@.str.1 = [22 x i8] c"parital/failed write \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strncpy(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 3)
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%22 = call i32 @connect(i32 %20, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @exit(i32 -2)
unreachable
26: ; preds = %13
br label %27
27: ; preds = %41, %26
%28 = load i32, i32* @STDIN_FILENO
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 100)
store i32 %30, i32* %4
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %27
%33 = load i32, i32* %3
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%35 = load i32, i32* %4
%36 = call i32 @write(i32 %33, i8* %34, i32 %35)
%37 = load i32, i32* %4
%38 = icmp ne i32 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %32
br label %27
42: ; preds = %27
%43 = load i32, i32* %4
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @exit(i32 -3)
unreachable
47: ; preds = %42
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"/tmp/us_xfr\00"
@STDIN_FILENO = global i32 0
@.str.1 = [22 x i8] c"parital/failed write \00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [100 x i8]
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 8)
%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 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strncpy(i32 %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 3)
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i32 @connect(i32 %7, %struct.sockaddr* %18, i32 8)
%20 = icmp eq i32 %19, -1
br i1 %20, label %25, label %21
21: ; preds = %11
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @read(i32 %22, i8* %4, i32 100)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %27, label %37
25: ; preds = %11
%26 = call i32 @exit(i32 -2)
unreachable
27: ; preds = %21, %33
%28 = phi i32 [ %35, %33 ], [ %23, %21 ]
%29 = call i32 @write(i32 %7, i8* %4, i32 %28)
%30 = icmp eq i32 %29, %28
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = load i32, i32* @STDIN_FILENO
%35 = call i32 @read(i32 %34, i8* %4, i32 100)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %27, label %37
37: ; preds = %33, %21
%38 = phi i32 [ %23, %21 ], [ %35, %33 ]
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @exit(i32 -3)
unreachable
42: ; preds = %37
ret i32 0
}