#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*/ buf;

/* Variables and functions */
int create_unix_stream_socket(char *, int /*<<< orphan*/);
int destroy_unix_socket(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int read(int, char *, int);
int shutdown_unix_stream_socket(int, int);
int write(int, char const *, int);
int main(void) {
  int sfd, bytes = 1, ret;
  const char *string = "abcdefghijklmnopqrstuvwxyz";
  char buf[16];
  memset(buf, 0, sizeof(buf));
  ret = sfd = create_unix_stream_socket("/tmp/echosock", 0);
  if (ret < 0) {
    perror(0);
    exit(1);
  }
  ret = write(sfd, string, 26);
  if (ret < 0) {
    perror(0);
    exit(1);
  }
  ret = shutdown_unix_stream_socket(sfd, 2);
  if (ret < 0) {
    perror(0);
    exit(1);
  }
  while (bytes > 0) {
    ret = bytes = read(sfd, buf, 15);
    if (ret < 0) {
      perror(0);
      exit(1);
    }
    write(1, buf, bytes);
  }
  ret = destroy_unix_socket(sfd);
  if (ret < 0) {
    perror(0);
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    0xe63(%rip),%rdi
xor    %esi,%esi
call   1080 <create_unix_stream_socket@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e0 <main+0x60>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1a,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    120f <main+0x8f>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1070 <shutdown_unix_stream_socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    123a <main+0xba>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    123f <main+0xbf>
cmpl   $0x0,-0xc(%rbp)
jle    1291 <main+0x111>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xf,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    127b <main+0xfb>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    123f <main+0xbf>
mov    -0x8(%rbp),%edi
call   1060 <destroy_unix_socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12b7 <main+0x137>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    0xe7d(%rip),%rdi
xor    %esi,%esi
call   1080 <create_unix_stream_socket@plt>
test   %eax,%eax
js     1220 <main+0xa0>
mov    %eax,%r14d
lea    0xe4d(%rip),%rsi
mov    %eax,%edi
mov    $0x1a,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1220 <main+0xa0>
mov    %r14d,%edi
mov    $0x2,%esi
call   1070 <shutdown_unix_stream_socket@plt>
test   %eax,%eax
js     1220 <main+0xa0>
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xf,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1220 <main+0xa0>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %ebp,%ebp
jne    11e0 <main+0x60>
mov    %r14d,%edi
call   1060 <destroy_unix_socket@plt>
test   %eax,%eax
js     1220 <main+0xa0>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"abcdefghijklmnopqrstuvwxyz\00"
@.str.1 = [14 x i8] c"/tmp/echosock\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [16 x i8]
store i32 0, i32* %1
store i32 1, i32* %3
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 16)
%9 = call i32 @create_unix_stream_socket(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %9, i32* %2
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i32 0)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %2
%17 = load i8*, i8** %5
%18 = call i32 @write(i32 %16, i8* %17, i32 26)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i32 0)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = load i32, i32* %2
%26 = call i32 @shutdown_unix_stream_socket(i32 %25, i32 2)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i32 0)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
br label %33
33: ; preds = %45, %32
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %49
36: ; preds = %33
%37 = load i32, i32* %2
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 15)
store i32 %39, i32* %3
store i32 %39, i32* %4
%40 = load i32, i32* %4
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = call i32 @perror(i32 0)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%47 = load i32, i32* %3
%48 = call i32 @write(i32 1, i8* %46, i32 %47)
br label %33
49: ; preds = %33
%50 = load i32, i32* %2
%51 = call i32 @destroy_unix_socket(i32 %50)
store i32 %51, i32* %4
%52 = load i32, i32* %4
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = call i32 @perror(i32 0)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %49
ret i32 0
}

LLVM IR (-O2):
@.str = [27 x i8] c"abcdefghijklmnopqrstuvwxyz\00"
@.str.1 = [14 x i8] c"/tmp/echosock\00"
define i32 @main() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 16)
%4 = call i32 @create_unix_stream_socket(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i32 0)
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = call i32 @write(i32 %4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 26)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @perror(i32 0)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = call i32 @shutdown_unix_stream_socket(i32 %4, i32 2)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i32 0)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15, %27
%22 = call i32 @read(i32 %4, i8* %2, i32 15)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 @perror(i32 0)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = call i32 @write(i32 1, i8* %2, i32 %22)
%29 = icmp eq i32 %22, 0
br i1 %29, label %30, label %21
30: ; preds = %27
%31 = call i32 @destroy_unix_socket(i32 %4)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i32 0)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
ret i32 0
}