#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*/ buf;
typedef int /*<<< orphan*/ addr;

/* 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*/ fprintf(int /*<<< orphan*/, char *);
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*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  struct sockaddr_un addr;
  char buf[100];
  int fd, rc;
  if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket error");
    exit(-1);
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, ".mglMovieSocket", sizeof(addr.sun_path) - 1);
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    perror("connect error");
    exit(-1);
  }
  if ((rc = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
    if (write(fd, buf, rc) != rc) {
      if (rc > 0)
        fprintf(stderr, "partial write");
      else {
        perror("write error");
        exit(-1);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x84(%rbp)
cmp    $0xffffffff,%eax
jne    11e7 <main+0x57>
lea    0xe28(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    0xe00(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x84(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    124a <main+0xba>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x88(%rbp)
cmp    $0x0,%eax
jle    12d8 <main+0x148>
mov    -0x84(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x88(%rbp),%edx
call   1040 <write@plt>
cmp    -0x88(%rbp),%eax
je     12d3 <main+0x143>
cmpl   $0x0,-0x88(%rbp)
jle    12b8 <main+0x128>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12ce <main+0x13e>
lea    0xd7a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    12d3 <main+0x143>
jmp    12d8 <main+0x148>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    0x2e46(%rip),%r14
mov    (%r14),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1257 <main+0xc7>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xe27(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     1260 <main+0xd0>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1249 <main+0xb9>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     1249 <main+0xb9>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xda2(%rip),%rdi
jmp    1267 <main+0xd7>
lea    0xdb6(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <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 = [13 x i8] c"socket error\00"
@.str.1 = [16 x i8] c".mglMovieSocket\00"
@.str.2 = [14 x i8] c"connect error\00"
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str.3 = [14 x i8] c"partial write\00"
@.str.4 = [12 x i8] c"write error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_un
%7 = alloca [100 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strncpy(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 3)
%24 = load i32, i32* %8
%25 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%26 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %17
%32 = load i32, i32* @STDIN_FILENO
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 100)
store i32 %34, i32* %9
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %54
36: ; preds = %31
%37 = load i32, i32* %8
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%39 = load i32, i32* %9
%40 = call i32 @write(i32 %37, i8* %38, i32 %39)
%41 = load i32, i32* %9
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %53
43: ; preds = %36
%44 = load i32, i32* %9
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %52
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %46
br label %53
53: ; preds = %52, %36
br label %54
54: ; preds = %53, %31
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 = [13 x i8] c"socket error\00"
@.str.1 = [16 x i8] c".mglMovieSocket\00"
@.str.2 = [14 x i8] c"connect error\00"
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str.3 = [14 x i8] c"partial write\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strncpy(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 3)
%21 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%22 = call i32 @connect(i32 %9, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %14
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @read(i32 %28, i8* %6, i32 100)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = call i32 @write(i32 %9, i8* %6, i32 %29)
%33 = icmp eq i32 %32, %29
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %31, %34, %27
ret i32 0
}