#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 */
typedef int /*<<< orphan*/ unix_addr;
struct sockaddr_un {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TRUE;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ cbreak();
int connect(int, struct sockaddr *, int);
char getch();
int /*<<< orphan*/ initscr();
int /*<<< orphan*/ keypad(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ nonl();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdscr;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main(void) {
  int s, ret;
  char buf[1024];
  int len;
  struct sockaddr_un unix_addr;
  s = socket(AF_UNIX, SOCK_STREAM, 0);
  perror("socket: ");
  printf("socket returned %d\n", s);
  bzero((char *)&unix_addr, sizeof(unix_addr));
  unix_addr.sun_family = AF_UNIX;
  strcpy(unix_addr.sun_path, "/tmp/KCemu-in");
  len = strlen(unix_addr.sun_path) + sizeof(unix_addr.sun_family);
  ret = connect(s, (struct sockaddr *)&unix_addr, len);
  perror("connect: ");
  printf("connect returned %d\n", ret);
  initscr();
  cbreak();
  nonl();
  keypad(stdscr, TRUE);
  while (242) {
    buf[0] = getch();
    buf[1] = '\0';
    ret = send(s, buf, strlen(buf), 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    0xdfb(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%esi
lea    0xdf5(%rip),%rdi
call   1070 <printf@plt>
lea    -0x428(%rbp),%rdi
mov    $0x10,%esi
call   10d0 <bzero@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x420(%rbp)
mov    -0x428(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x428(%rbp),%rdi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x428(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   10e0 <connect@plt>
mov    %eax,-0xc(%rbp)
lea    0xda8(%rip),%rdi
call   10c0 <perror@plt>
mov    -0xc(%rbp),%esi
lea    0xda3(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1090 <initscr@plt>
mov    $0x0,%al
call   1040 <cbreak@plt>
mov    $0x0,%al
call   10a0 <nonl@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
call   1080 <keypad@plt>
mov    $0x0,%al
call   10b0 <getch@plt>
mov    %al,-0x410(%rbp)
movb   $0x0,-0x40f(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x434(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0xc(%rbp)
jmp    12c3 <main+0xf3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    0x2e06(%rip),%rbx
mov    (%rbx),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
lea    0xe03(%rip),%rdi
call   10c0 <perror@plt>
lea    0xe00(%rip),%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x10,%esi
call   10d0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rbx
lea    0xdec(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x4(%rax),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   10e0 <connect@plt>
mov    %eax,%ebx
lea    0xdd4(%rip),%rdi
call   10c0 <perror@plt>
lea    0xdd2(%rip),%rdi
mov    %ebx,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1090 <initscr@plt>
xor    %eax,%eax
call   1040 <cbreak@plt>
xor    %eax,%eax
call   10a0 <nonl@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
call   1080 <keypad@plt>
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <getch@plt>
mov    %al,0x10(%rsp)
movb   $0x0,0x11(%rsp)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    12a0 <main+0xd0>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket: \00"
@.str.1 = [20 x i8] c"socket returned %d\0A\00"
@.str.2 = [14 x i8] c"/tmp/KCemu-in\00"
@.str.3 = [10 x i8] c"connect: \00"
@.str.4 = [21 x i8] c"connect returned %d\0A\00"
@stdscr = global i32 0
@TRUE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i32 0, i32* %1
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %2
%12 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = bitcast %struct.sockaddr_un* %6 to i8*
%14 = call i32 @bzero(i8* %13, i32 16)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strlen(i8* %21)
%23 = sext i32 %22 to i64
%24 = add i64 %23, 4
%25 = trunc i64 %24 to i32
store i32 %25, i32* %5
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%28 = load i32, i32* %5
%29 = call i32 @connect(i32 %26, %struct.sockaddr* %27, i32 %28)
store i32 %29, i32* %3
%30 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* %3
%32 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = call i32 (...) @initscr()
%34 = call i32 (...) @cbreak()
%35 = call i32 (...) @nonl()
%36 = load i32, i32* @stdscr
%37 = load i32, i32* @TRUE
%38 = call i32 @keypad(i32 %36, i32 %37)
br label %39
39: ; preds = %0, %39
%40 = call signext i8 (...) @getch()
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8 %40, i8* %41
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 1
store i8 0, i8* %42
%43 = load i32, i32* %2
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @send(i32 %43, i8* %44, i32 %46, i32 0)
store i32 %47, i32* %3
br label %39
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket: \00"
@.str.1 = [20 x i8] c"socket returned %d\0A\00"
@.str.2 = [14 x i8] c"/tmp/KCemu-in\00"
@.str.3 = [10 x i8] c"connect: \00"
@.str.4 = [21 x i8] c"connect returned %d\0A\00"
@stdscr = global i32 0
@TRUE = global i32 0
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.sockaddr_un
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%10 = call i32 @bzero(i8* %4, i32 16)
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = tail call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @strlen(i8* %14)
%17 = add i32 %16, 4
%18 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%19 = call i32 @connect(i32 %7, %struct.sockaddr* %18, i32 %17)
%20 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%22 = call i32 (...) @initscr()
%23 = call i32 (...) @cbreak()
%24 = call i32 (...) @nonl()
%25 = load i32, i32* @stdscr
%26 = load i32, i32* @TRUE
%27 = call i32 @keypad(i32 %25, i32 %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 1
br label %29
29: ; preds = %29, %0
%30 = call signext i8 (...) @getch()
store i8 %30, i8* %3
store i8 0, i8* %28
%31 = call i32 @strlen(i8* %3)
%32 = call i32 @send(i32 %7, i8* %3, i32 %31, i32 0)
br label %29
}