#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*/ srv_addr;
typedef int /*<<< orphan*/ snd_buf;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
scalar_t__ strncasecmp(char *, char *, int);
int write(int, char *, int);
int main(void) {
  int connect_fd;
  int ret;
  char snd_buf[1024];
  int i;
  static struct sockaddr_un srv_addr;
  connect_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (connect_fd < 0) {
    perror("cannot create communication socket");
    return 1;
  }
  srv_addr.sun_family = AF_UNIX;
  strcpy(srv_addr.sun_path, "/tmp/UNIX.domain");
  ret = connect(connect_fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
  if (ret == -1) {
    perror("cannot connect to the server");
    close(connect_fd);
    return 1;
  }
  int bytes_write = 0;
  while (strncasecmp("exit", snd_buf, 4) != 0) {
    memset(snd_buf, 0, 1024);
    fgets(snd_buf, sizeof(snd_buf), stdin);
    snd_buf[strlen(snd_buf) - 1] = 0;
    bytes_write = write(connect_fd, snd_buf, sizeof(snd_buf));
    printf("Sending %d bytes to server\n", bytes_write);
  }
  close(connect_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1200 <main+0x50>
lea    0xe11(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1314 <main+0x164>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2e71(%rip)
mov    0x2e67(%rip),%edi
lea    0xe07(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    0x2e51(%rip),%rsi
mov    $0x8,%edx
call   10c0 <connect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1262 <main+0xb2>
lea    0xdeb(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1314 <main+0x164>
movl   $0x0,-0x418(%rbp)
lea    -0x410(%rbp),%rsi
lea    0xdd7(%rip),%rdi
mov    $0x4,%edx
call   10a0 <strncasecmp@plt>
cmp    $0x0,%rax
je     1305 <main+0x155>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1090 <fgets@plt>
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    %eax,-0x418(%rbp)
mov    -0x418(%rbp),%esi
lea    0xd5b(%rip),%rdi
call   1060 <printf@plt>
jmp    126c <main+0xbc>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12d6 <main+0x126>
mov    %eax,%ebp
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2e8f(%rip)
lea    0x2e84(%rip),%rbx
mov    0x2e7e(%rip),%edi
lea    0xe1e(%rip),%rsi
call   1030 <strcpy@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
je     12e9 <main+0x139>
lea    0xe28(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
mov    $0x4,%edx
call   10a0 <strncasecmp@plt>
test   %rax,%rax
je     12bd <main+0x10d>
mov    0x2d93(%rip),%r12
lea    0xe0a(%rip),%r14
lea    0xdfe(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10a0 <strncasecmp@plt>
test   %rax,%rax
jne    1260 <main+0xb0>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd23(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
jmp    12c6 <main+0x116>
lea    0xd44(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0x1,%eax
jmp    12c6 <main+0x116>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@main.srv_addr = internal global %struct.sockaddr_un zeroinitializer
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [35 x i8] c"cannot create communication socket\00"
@AF_UNIX = global i32 0
@.str.1 = [17 x i8] c"/tmp/UNIX.domain\00"
@.str.2 = [29 x i8] c"cannot connect to the server\00"
@.str.3 = [5 x i8] c"exit\00"
@stdin = global i32 0
@.str.4 = [28 x i8] c"Sending %d bytes to server\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %50
14: ; preds = %0
%15 = load i32, i32* @AF_UNIX
store i32 %15, i32* getelementptr inbounds (%struct.sockaddr_un, %struct.sockaddr_un* @main.srv_addr, i32 0, i32 1)
%16 = load i32, i32* getelementptr inbounds (%struct.sockaddr_un, %struct.sockaddr_un* @main.srv_addr, i32 0, i32 0)
%17 = call i32 @strcpy(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %2
%19 = call i32 @connect(i32 %18, %struct.sockaddr* bitcast (%struct.sockaddr_un* @main.srv_addr to %struct.sockaddr*), i32 8)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
store i32 1, i32* %1
br label %50
26: ; preds = %14
store i32 0, i32* %6
br label %27
27: ; preds = %31, %26
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = call i64 @strncasecmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %28, i32 4)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %47
31: ; preds = %27
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 1024)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%35 = load i32, i32* @stdin
%36 = call i32 @fgets(i8* %34, i32 1024, i32 %35)
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* %2
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%44 = call i32 @write(i32 %42, i8* %43, i32 1024)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %45)
br label %27
47: ; preds = %27
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
store i32 0, i32* %1
br label %50
50: ; preds = %47, %22, %12
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@main.srv_addr = internal global %struct.sockaddr_un zeroinitializer
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [35 x i8] c"cannot create communication socket\00"
@AF_UNIX = global i32 0
@.str.1 = [17 x i8] c"/tmp/UNIX.domain\00"
@.str.2 = [29 x i8] c"cannot connect to the server\00"
@.str.3 = [5 x i8] c"exit\00"
@stdin = global i32 0
@.str.4 = [28 x i8] c"Sending %d bytes to server\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%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 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %35
9: ; preds = %0
%10 = load i32, i32* @AF_UNIX
store i32 %10, i32* getelementptr inbounds (%struct.sockaddr_un, %struct.sockaddr_un* @main.srv_addr, i64 0, i32 1)
%11 = load i32, i32* getelementptr inbounds (%struct.sockaddr_un, %struct.sockaddr_un* @main.srv_addr, i64 0, i32 0)
%12 = tail call i32 @strcpy(i32 %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @connect(i32 %5, %struct.sockaddr* bitcast (%struct.sockaddr_un* @main.srv_addr to %struct.sockaddr*), i32 8)
%14 = icmp eq i32 %13, -1
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = call i64 @strncasecmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %2, i32 4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %33, label %21
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @close(i32 %5)
br label %35
21: ; preds = %15, %21
%22 = call i32 @memset(i8* %2, i32 0, i32 1024)
%23 = load i32, i32* @stdin
%24 = call i32 @fgets(i8* %2, i32 1024, i32 %23)
%25 = call i32 @strlen(i8* %2)
%26 = add nsw i32 %25, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 @write(i32 %5, i8* %2, i32 1024)
%30 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = call i64 @strncasecmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %2, i32 4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %21
33: ; preds = %21, %15
%34 = call i32 @close(i32 %5)
br label %35
35: ; preds = %33, %18, %7
%36 = phi i32 [ 1, %7 ], [ 1, %18 ], [ 0, %33 ]
ret i32 %36
}