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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int main(void) {
  int server, client, t, len, n;
  struct sockaddr_un local, remote;
  char str[100];
  if ((server = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    printf("Error: Socket()\n");
    return -1;
  }
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, "echo_socket");
  unlink(local.sun_path);
  len = strlen(local.sun_path) + sizeof(local.sun_family);
  if (bind(server, (struct sockaddr *)&local, len) == -1) {
    printf("Error: Bind()\n");
    return -1;
  }
  if (listen(server, 5) < 0) {
    printf("Error: Listen()\n");
    return -1;
  }
  while (1) {
    printf("Esperando una conexion...\n");
    t = sizeof(remote);
    if ((client = accept(server, (struct sockaddr *)&remote, &t)) < 0) {
      perror("accept");
      exit(1);
    }
    printf("Conectado\n");
    n = recv(client, str, 100, 0);
    if (n < 0) {
      printf("Error: Recv()\n");
      return -1;
    } else if (n > 0) {
      printf("Recibido %d bytes\n", n);
      printf("Mensaje: %s\n", str);
    }
    close(client);
  }
  close(server);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1211 <main+0x51>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <main+0x1c1>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    0xdea(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%edi
call   1040 <unlink@plt>
mov    -0x20(%rbp),%edi
call   1060 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1277 <main+0xb7>
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <main+0x1c1>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12a8 <main+0xe8>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <main+0x1c1>
jmp    12ad <main+0xed>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x8,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    12f4 <main+0x134>
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    133f <main+0x17f>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <main+0x1c1>
cmpl   $0x0,-0x18(%rbp)
jle    136f <main+0x1af>
mov    -0x18(%rbp),%esi
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x90(%rbp),%rsi
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1374 <main+0x1b4>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
jmp    12ad <main+0xed>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2e00(%rip),%rbx
mov    (%rbx),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1317 <main+0x147>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x1c(%rsp)
lea    0xdef(%rip),%rsi
call   1050 <strcpy@plt>
call   1040 <unlink@plt>
call   1070 <strlen@plt>
lea    0x4(%rax),%edx
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     1329 <main+0x159>
mov    %ebp,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
test   %rax,%rax
js     1332 <main+0x162>
lea    0xddf(%rip),%rdi
call   1060 <puts@plt>
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10d0 <accept@plt>
test   %eax,%eax
js     1355 <main+0x185>
mov    %eax,%r14d
lea    0x20(%rsp),%rbx
lea    0xdaa(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
jmp    12d0 <main+0x100>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
call   1090 <close@plt>
mov    %r15,%rdi
call   1060 <puts@plt>
movl   $0x8,0xc(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10d0 <accept@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1355 <main+0x185>
lea    0xd76(%rip),%rdi
call   1060 <puts@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1320 <main+0x150>
je     12a0 <main+0xd0>
lea    0xd18(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xd1b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    12a0 <main+0xd0>
lea    0xd65(%rip),%rdi
jmp    1339 <main+0x169>
lea    0xd30(%rip),%rdi
jmp    1339 <main+0x169>
lea    0xd45(%rip),%rdi
jmp    1339 <main+0x169>
lea    0xd2c(%rip),%rdi
call   1060 <puts@plt>
mov    $0xffffffff,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb0(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"Error: Socket()\0A\00"
@.str.1 = [12 x i8] c"echo_socket\00"
@.str.2 = [15 x i8] c"Error: Bind()\0A\00"
@.str.3 = [17 x i8] c"Error: Listen()\0A\00"
@.str.4 = [27 x i8] c"Esperando una conexion...\0A\00"
@.str.5 = [7 x i8] c"accept\00"
@.str.6 = [11 x i8] c"Conectado\0A\00"
@.str.7 = [15 x i8] c"Error: Recv()\0A\00"
@.str.8 = [19 x i8] c"Recibido %d bytes\0A\00"
@.str.9 = [13 x i8] c"Mensaje: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
%8 = alloca %struct.sockaddr_un
%9 = alloca [100 x i8]
store i32 0, i32* %1
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %75
16: ; preds = %0
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @strcpy(i32 %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @unlink(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @strlen(i32 %26)
%28 = sext i32 %27 to i64
%29 = add i64 %28, 4
%30 = trunc i64 %29 to i32
store i32 %30, i32* %5
%31 = load i32, i32* %2
%32 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%33 = load i32, i32* %5
%34 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %16
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %75
38: ; preds = %16
%39 = load i32, i32* %2
%40 = call i64 @listen(i32 %39, i32 5)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %75
44: ; preds = %38
br label %45
45: ; preds = %44, %72
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
store i32 8, i32* %4
%47 = load i32, i32* %2
%48 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%49 = call i32 @accept(i32 %47, %struct.sockaddr* %48, i32* %4)
store i32 %49, i32* %3
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %45
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%56 = load i32, i32* %3
%57 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%58 = call i32 @recv(i32 %56, i8* %57, i32 100, i32 0)
store i32 %58, i32* %6
%59 = load i32, i32* %6
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %54
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %1
br label %75
63: ; preds = %54
%64 = load i32, i32* %6
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %63
%67 = load i32, i32* %6
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i32 %67)
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %66, %63
br label %72
72: ; preds = %71
%73 = load i32, i32* %3
%74 = call i32 @close(i32 %73)
br label %45
75: ; preds = %61, %42, %36, %14
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [12 x i8] c"echo_socket\00"
@.str.5 = [7 x i8] c"accept\00"
@.str.8 = [19 x i8] c"Recibido %d bytes\0A\00"
@.str.9 = [13 x i8] c"Mensaje: %s\0A\00"
@str = [26 x i8] c"Esperando una conexion...\00"
@str.10 = [10 x i8] c"Conectado\00"
@str.11 = [14 x i8] c"Error: Recv()\00"
@str.12 = [16 x i8] c"Error: Listen()\00"
@str.13 = [14 x i8] c"Error: Bind()\00"
@str.14 = [16 x i8] c"Error: Socket()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.sockaddr_un
%4 = alloca [100 x i8]
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.sockaddr_un* %2 to i8*
%7 = bitcast %struct.sockaddr_un* %3 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.14, i64 0, i64 0))
br label %57
15: ; preds = %0
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @unlink(i32 undef)
%20 = tail call i32 @strlen(i32 undef)
%21 = add i32 %20, 4
%22 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %11, %struct.sockaddr* %22, i32 %21)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0))
br label %57
27: ; preds = %15
%28 = call i64 @listen(i32 %11, i32 5)
%29 = icmp slt i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
store i32 8, i32* %1
%32 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%33 = call i32 @accept(i32 %11, %struct.sockaddr* %32, i32* %1)
%34 = icmp slt i32 %33, 0
br i1 %34, label %37, label %40
35: ; preds = %27
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.12, i64 0, i64 0))
br label %57
37: ; preds = %52, %30
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30, %52
%41 = phi i32 [ %55, %52 ], [ %33, %30 ]
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%43 = call i32 @recv(i32 %41, i8* %8, i32 100, i32 0)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
br label %57
47: ; preds = %40
%48 = icmp eq i32 %43, 0
br i1 %48, label %52, label %49
49: ; preds = %47
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i32 %43)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0), i8* %8)
br label %52
52: ; preds = %47, %49
%53 = call i32 @close(i32 %41)
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
store i32 8, i32* %1
%55 = call i32 @accept(i32 %11, %struct.sockaddr* %32, i32* %1)
%56 = icmp slt i32 %55, 0
br i1 %56, label %37, label %40
57: ; preds = %45, %35, %25, %13
ret i32 -1
}