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

/* Variables and functions */
int accept_unix_stream_socket(int, int /*<<< orphan*/);
int create_unix_server_socket(char *, int, int /*<<< orphan*/);
int destroy_unix_socket(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int sfd, cfd, bytes, ret;
  char buf[16];
  buf[15] = 0;
  ret = sfd = create_unix_server_socket("/tmp/echosock", 1, 0);
  if (ret < 0) {
    perror(0);
    exit(1);
  }
  for (;;) {
    ret = cfd = accept_unix_stream_socket(sfd, 0);
    if (ret < 0) {
      perror(0);
      exit(1);
    }
    while (0 < (bytes = read(cfd, buf, 15))) {
      write(cfd, buf, bytes);
      write(1, buf, bytes);
    }
    ret = destroy_unix_socket(cfd);
    if (ret < 0) {
      perror(0);
      exit(1);
    }
  }
  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)
movb   $0x0,-0x21(%rbp)
lea    0xe76(%rip),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1060 <create_unix_server_socket@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b7 <main+0x47>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11bc <main+0x4c>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <accept_unix_stream_socket@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e7 <main+0x77>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11ec <main+0x7c>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xf,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    1231 <main+0xc1>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <write@plt>
lea    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
jmp    11ec <main+0x7c>
mov    -0xc(%rbp),%edi
call   1070 <destroy_unix_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1257 <main+0xe7>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11bc <main+0x4c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movb   $0x0,0xf(%rsp)
lea    0xe7a(%rip),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1060 <create_unix_server_socket@plt>
test   %eax,%eax
jns    11a7 <main+0x37>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%r14d
mov    %rsp,%r15
jmp    11bb <main+0x4b>
nop
mov    %ebp,%edi
call   1070 <destroy_unix_socket@plt>
test   %eax,%eax
js     1196 <main+0x26>
mov    %r14d,%edi
xor    %esi,%esi
call   1030 <accept_unix_stream_socket@plt>
test   %eax,%eax
js     1196 <main+0x26>
mov    %eax,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xf,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11b0 <main+0x40>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    $0x1,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
jmp    11d0 <main+0x60>

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

LLVM IR (-O2):
@.str = [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 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 15
store i8 0, i8* %3
%4 = tail call i32 @create_unix_server_socket(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 1, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i32 0)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0, %24
%10 = tail call i32 @accept_unix_stream_socket(i32 %4, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = call i32 @read(i32 %10, i8* %2, i32 15)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %18, label %24
15: ; preds = %9
%16 = tail call i32 @perror(i32 0)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %12, %18
%19 = phi i32 [ %22, %18 ], [ %13, %12 ]
%20 = call i32 @write(i32 %10, i8* %2, i32 %19)
%21 = call i32 @write(i32 1, i8* %2, i32 %19)
%22 = call i32 @read(i32 %10, i8* %2, i32 15)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %18, label %24
24: ; preds = %18, %12
%25 = tail call i32 @destroy_unix_socket(i32 %10)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %9
27: ; preds = %24
%28 = tail call i32 @perror(i32 0)
%29 = tail call i32 @exit(i32 1)
unreachable
}