#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 create_unix_server_socket(char *, int, int /*<<< orphan*/);
int destroy_unix_socket(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int recvfrom_unix_dgram_socket(int, char *, int, char *, int,
                               int /*<<< orphan*/);
int sendto_unix_dgram_socket(int, char *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int sfd, bytes, ret;
  char buf[128];
  char from[128];
  memset(buf, 0, 128);
  memset(from, 0, 128);
  ret = sfd = create_unix_server_socket("/tmp/echosock", 2, 0);
  if (ret < 0) {
    perror(0);
    exit(1);
  }
  while (0 < (ret = bytes =
                  recvfrom_unix_dgram_socket(sfd, buf, 127, from, 127, 0))) {
    if (ret < 0) {
      perror(0);
      exit(1);
    }
    write(1, buf, bytes);
    ret = sendto_unix_dgram_socket(sfd, buf, bytes, from, 0);
    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    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe41(%rip),%rdi
mov    $0x2,%esi
xor    %edx,%edx
call   1060 <create_unix_server_socket@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ec <main+0x6c>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11f1 <main+0x71>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
lea    -0x110(%rbp),%rcx
mov    $0x7f,%r8d
xor    %r9d,%r9d
mov    %r8d,%edx
call   1040 <recvfrom_unix_dgram_socket@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    1293 <main+0x113>
cmpl   $0x0,-0x10(%rbp)
jge    1240 <main+0xc0>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x90(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x110(%rbp),%rcx
xor    %r8d,%r8d
call   1080 <sendto_unix_dgram_socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    128e <main+0x10e>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11f1 <main+0x71>
mov    -0x8(%rbp),%edi
call   1070 <destroy_unix_socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12b9 <main+0x139>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
lea    0x80(%rsp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe49(%rip),%rdi
mov    $0x2,%esi
xor    %edx,%edx
call   1060 <create_unix_server_socket@plt>
test   %eax,%eax
js     1226 <main+0xa6>
mov    %eax,%ebx
lea    0x80(%rsp),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x7f,%edx
mov    %r15,%rcx
mov    $0x7f,%r8d
xor    %r9d,%r9d
call   1040 <recvfrom_unix_dgram_socket@plt>
test   %eax,%eax
jle    1237 <main+0xb7>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %r15,%rcx
xor    %r8d,%r8d
call   1080 <sendto_unix_dgram_socket@plt>
test   %eax,%eax
jns    11e0 <main+0x60>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %ebx,%edi
call   1070 <destroy_unix_socket@plt>
test   %eax,%eax
js     1226 <main+0xa6>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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 [128 x i8]
%6 = alloca [128 x i8]
store i32 0, i32* %1
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 128)
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 128)
%11 = call i32 @create_unix_server_socket(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 2, i32 0)
store i32 %11, i32* %2
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i32 0)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
br label %18
18: ; preds = %44, %17
%19 = load i32, i32* %2
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%22 = call i32 @recvfrom_unix_dgram_socket(i32 %19, i8* %20, i32 127, i8* %21, i32 127, i32 0)
store i32 %22, i32* %3
store i32 %22, i32* %4
%23 = icmp slt i32 0, %22
br i1 %23, label %24, label %45
24: ; preds = %18
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = call i32 @perror(i32 0)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %24
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%32 = load i32, i32* %3
%33 = call i32 @write(i32 1, i8* %31, i32 %32)
%34 = load i32, i32* %2
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%36 = load i32, i32* %3
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%38 = call i32 @sendto_unix_dgram_socket(i32 %34, i8* %35, i32 %36, i8* %37, i32 0)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = call i32 @perror(i32 0)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %30
br label %18
45: ; preds = %18
%46 = load i32, i32* %2
%47 = call i32 @destroy_unix_socket(i32 %46)
store i32 %47, i32* %4
%48 = load i32, i32* %4
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = call i32 @perror(i32 0)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %45
ret i32 0
}

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