#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_family;
  int /*<<< orphan*/ sun_path;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ sockBufSize;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int EINTR;
int ENAMETOOLONG;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDBUF;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ connect(int, struct sockaddr *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char const *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
scalar_t__ write(int /*<<< orphan*/, char *, size_t);
int runClientMode(const char *sockPath) {
  char buf[16 * 1024];
  struct sockaddr_un addr;
  int sock;
  int sockBufSize = 1024 * 512;
  if (strlen(sockPath) >= sizeof(addr.sun_path)) {
    errno = ENAMETOOLONG;
    perror(sockPath);
    exit(errno);
  }
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1) {
    perror("socket(AF_UNIX, SOCK_STREAM, 0)");
    exit(errno);
  }
  strncpy(addr.sun_path, sockPath, sizeof(addr.sun_path));
  addr.sun_family = AF_UNIX;
  if (connect(sock, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) != 0) {
    perror(sockPath);
    return errno;
  }
  setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sockBufSize,
             (socklen_t)sizeof(sockBufSize));
  while (1) {
    ssize_t written = 0;
    ssize_t r = read(sock, buf, sizeof(buf));
    if (r <= 0) {
      if (r < 0 && errno == EINTR)
        continue;
      return r ? errno : 0;
    }
    while (written < r) {
      ssize_t w = write(STDOUT_FILENO, buf + written, (size_t)(r - written));
      if (w <= 0) {
        perror("read(STDIN_FILENO)");
        return errno;
      } else {
        written += w;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<runClientMode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x80000,-0x4020(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     11e7 <runClientMode+0x57>
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
mov    0x2dea(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x401c(%rbp)
cmpl   $0xffffffff,-0x401c(%rbp)
jne    122d <runClientMode+0x9d>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x4014(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4018(%rbp)
mov    -0x401c(%rbp),%edi
lea    -0x4018(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
je     128b <runClientMode+0xfb>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e8 <runClientMode+0x258>
mov    -0x401c(%rbp),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
lea    -0x4020(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
movq   $0x0,-0x4028(%rbp)
mov    -0x401c(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    $0x4000,%edx
call   1070 <read@plt>
mov    %rax,-0x4030(%rbp)
cmpq   $0x0,-0x4030(%rbp)
jg     1354 <runClientMode+0x1c4>
cmpq   $0x0,-0x4030(%rbp)
jge    1317 <runClientMode+0x187>
mov    0x2cb7(%rip),%rax
mov    (%rax),%eax
mov    0x2ca6(%rip),%rcx
cmp    (%rcx),%eax
jne    1317 <runClientMode+0x187>
jmp    12b5 <runClientMode+0x125>
cmpq   $0x0,-0x4030(%rbp)
je     1339 <runClientMode+0x1a9>
mov    0x2c8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x403c(%rbp)
jmp    1346 <runClientMode+0x1b6>
xor    %eax,%eax
mov    %eax,-0x403c(%rbp)
jmp    1346 <runClientMode+0x1b6>
mov    -0x403c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13e8 <runClientMode+0x258>
jmp    1359 <runClientMode+0x1c9>
mov    -0x4028(%rbp),%rax
cmp    -0x4030(%rbp),%rax
jge    13e3 <runClientMode+0x253>
mov    0x2c7c(%rip),%rax
mov    (%rax),%edi
lea    -0x4010(%rbp),%rsi
add    -0x4028(%rbp),%rsi
mov    -0x4030(%rbp),%rdx
sub    -0x4028(%rbp),%rdx
call   1050 <write@plt>
mov    %rax,-0x4038(%rbp)
cmpq   $0x0,-0x4038(%rbp)
jg     13c9 <runClientMode+0x239>
lea    0xc6d(%rip),%rdi
call   1080 <perror@plt>
mov    0x2bf9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13e8 <runClientMode+0x258>
mov    -0x4038(%rbp),%rax
add    -0x4028(%rbp),%rax
mov    %rax,-0x4028(%rbp)
jmp    1359 <runClientMode+0x1c9>
jmp    12b5 <runClientMode+0x125>
mov    -0x4(%rbp),%eax
add    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runClientMode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4018,%rsp
mov    %rdi,%r14
movl   $0x80000,0x4(%rsp)
call   1060 <strlen@plt>
cmp    $0x4,%eax
jae    12f0 <runClientMode+0x160>
mov    0x2e0f(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1311 <runClientMode+0x181>
mov    %eax,%r13d
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0x8,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jne    12cd <runClientMode+0x13d>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x10(%rsp),%r14
mov    0x2d7a(%rip),%r15
mov    0x2dab(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x4000,%edx
call   1070 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    12a0 <runClientMode+0x110>
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
lea    (%rsp,%r12,1),%rsi
add    $0x10,%rsi
mov    %rbx,%rdx
sub    %r12,%rdx
call   1050 <write@plt>
test   %rax,%rax
jle    12b2 <runClientMode+0x122>
add    %rax,%r12
cmp    %r12,%rbx
jg     1270 <runClientMode+0xe0>
jmp    1250 <runClientMode+0xc0>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%eax
jns    12c3 <runClientMode+0x133>
mov    0x2d04(%rip),%rcx
cmp    (%rcx),%eax
je     1250 <runClientMode+0xc0>
jmp    12de <runClientMode+0x14e>
lea    0xd67(%rip),%rdi
call   1080 <perror@plt>
mov    (%r15),%eax
jmp    12de <runClientMode+0x14e>
xor    %ecx,%ecx
test   %rbx,%rbx
cmove  %ecx,%eax
jmp    12de <runClientMode+0x14e>
mov    %r14,%rdi
call   1080 <perror@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
add    $0x4018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cb1(%rip),%rax
mov    (%rax),%eax
mov    0x2cb8(%rip),%rbx
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1080 <perror@plt>
mov    (%rbx),%edi
call   1090 <exit@plt>
lea    0xce8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"socket(AF_UNIX, SOCK_STREAM, 0)\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@EINTR = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [19 x i8] c"read(STDIN_FILENO)\00"
define i32 @runClientMode(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [16384 x i8]
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %3
store i32 524288, i32* %7
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = icmp uge i64 %13, 4
br i1 %14, label %15, label %21
15: ; preds = %1
%16 = load i32, i32* @ENAMETOOLONG
store i32 %16, i32* @errno
%17 = load i8*, i8** %3
%18 = call i32 @perror(i8* %17)
%19 = load i32, i32* @errno
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %1
%22 = load i32, i32* @AF_UNIX
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* @errno
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%32 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load i8*, i8** %3
%35 = call i32 @strncpy(i32 %33, i8* %34, i32 4)
%36 = load i32, i32* @AF_UNIX
%37 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%40 = call i64 @connect(i32 %38, %struct.sockaddr* %39, i32 8)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %31
%43 = load i8*, i8** %3
%44 = call i32 @perror(i8* %43)
%45 = load i32, i32* @errno
store i32 %45, i32* %2
br label %98
46: ; preds = %31
%47 = load i32, i32* %6
%48 = load i32, i32* @SOL_SOCKET
%49 = load i32, i32* @SO_SNDBUF
%50 = call i32 @setsockopt(i32 %47, i32 %48, i32 %49, i32* %7, i32 4)
br label %51
51: ; preds = %46, %64, %97
store i64 0, i64* %8
%52 = load i32, i32* %6
%53 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%54 = call i64 @read(i32 %52, i8* %53, i32 16384)
store i64 %54, i64* %9
%55 = load i64, i64* %9
%56 = icmp sle i64 %55, 0
br i1 %56, label %57, label %73
57: ; preds = %51
%58 = load i64, i64* %9
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %57
%61 = load i32, i32* @errno
%62 = load i32, i32* @EINTR
%63 = icmp eq i32 %61, %62
br i1 %63, label %64, label %65
64: ; preds = %60
br label %51
65: ; preds = %60, %57
%66 = load i64, i64* %9
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = load i32, i32* @errno
br label %71
70: ; preds = %65
br label %71
71: ; preds = %70, %68
%72 = phi i32 [ %69, %68 ], [ 0, %70 ]
store i32 %72, i32* %2
br label %98
73: ; preds = %51
br label %74
74: ; preds = %96, %73
%75 = load i64, i64* %8
%76 = load i64, i64* %9
%77 = icmp slt i64 %75, %76
br i1 %77, label %78, label %97
78: ; preds = %74
%79 = load i32, i32* @STDOUT_FILENO
%80 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%81 = load i64, i64* %8
%82 = getelementptr inbounds i8, i8* %80, i64 %81
%83 = load i64, i64* %9
%84 = load i64, i64* %8
%85 = sub nsw i64 %83, %84
%86 = call i64 @write(i32 %79, i8* %82, i64 %85)
store i64 %86, i64* %10
%87 = load i64, i64* %10
%88 = icmp sle i64 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %78
%90 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%91 = load i32, i32* @errno
store i32 %91, i32* %2
br label %98
92: ; preds = %78
%93 = load i64, i64* %10
%94 = load i64, i64* %8
%95 = add nsw i64 %94, %93
store i64 %95, i64* %8
br label %96
96: ; preds = %92
br label %74
97: ; preds = %74
br label %51
98: ; preds = %89, %71, %42
%99 = load i32, i32* %2
ret i32 %99
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"socket(AF_UNIX, SOCK_STREAM, 0)\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@EINTR = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [19 x i8] c"read(STDIN_FILENO)\00"
define i32 @runClientMode(i8* %0) {
%2 = alloca [16384 x i8]
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = getelementptr inbounds [16384 x i8], [16384 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_un* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 524288, i32* %4
%8 = tail call i32 @strlen(i8* %0)
%9 = icmp ugt i32 %8, 3
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @ENAMETOOLONG
store i32 %11, i32* @errno
%12 = tail call i32 @perror(i8* %0)
%13 = load i32, i32* @errno
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32, i32* @AF_UNIX
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @errno
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%26 = load i32, i32* @AF_UNIX
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%29 = call i64 @connect(i32 %18, %struct.sockaddr* %28, i32 8)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %24
%32 = call i32 @perror(i8* %0)
%33 = load i32, i32* @errno
br label %64
34: ; preds = %24
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_SNDBUF
%37 = call i32 @setsockopt(i32 %18, i32 %35, i32 %36, i32* %4, i32 4)
br label %38
38: ; preds = %50, %34
%39 = call i64 @read(i32 %18, i8* %5, i32 16384)
%40 = icmp slt i64 %39, 1
br i1 %40, label %41, label %53
41: ; preds = %38
%42 = icmp slt i64 %39, 0
%43 = load i32, i32* @errno
br i1 %42, label %47, label %44
44: ; preds = %41
%45 = icmp eq i64 %39, 0
%46 = select i1 %45, i32 0, i32 %43
br label %64
47: ; preds = %41
%48 = load i32, i32* @EINTR
%49 = icmp eq i32 %43, %48
br i1 %49, label %50, label %64
50: ; preds = %51, %47
br label %38
51: ; preds = %53
%52 = icmp sgt i64 %39, %60
br i1 %52, label %53, label %50
53: ; preds = %38, %51
%54 = phi i64 [ %60, %51 ], [ 0, %38 ]
%55 = load i32, i32* @STDOUT_FILENO
%56 = getelementptr inbounds [16384 x i8], [16384 x i8]* %2, i64 0, i64 %54
%57 = sub nsw i64 %39, %54
%58 = call i64 @write(i32 %55, i8* %56, i64 %57)
%59 = icmp slt i64 %58, 1
%60 = add nsw i64 %58, %54
br i1 %59, label %61, label %51
61: ; preds = %53
%62 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%63 = load i32, i32* @errno
br label %64
64: ; preds = %47, %44, %61, %31
%65 = phi i32 [ %33, %31 ], [ %63, %61 ], [ %46, %44 ], [ %43, %47 ]
ret i32 %65
}