#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 socklen_t;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int /*<<< orphan*/);
int main(void) {
  struct sockaddr_un address;
  int socket_fd, connection_fd;
  socklen_t address_length;
  pid_t child;
  char buffer[256];
  int n;
  socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (socket_fd < 0) {
    printf("socket() failed\n");
    return 1;
  }
  unlink("/tmp/demo_socket");
  memset(&address, 0, sizeof(struct sockaddr_un));
  address.sun_family = AF_UNIX;
  snprintf(address.sun_path, 108, "/tmp/demo_socket");
  if (bind(socket_fd, (struct sockaddr *)&address,
           sizeof(struct sockaddr_un)) != 0) {
    printf("bind() failed\n");
    return 1;
  }
  if (listen(socket_fd, 5) != 0) {
    printf("listen() failed\n");
    return 1;
  }
  address_length = sizeof(address);
  while ((connection_fd = accept(socket_fd, (struct sockaddr *)&address,
                                 &address_length)) > -1) {
    printf("successfully received data\n");
    bzero(buffer, 256);
    n = read(connection_fd, buffer, 255);
    if (n < 0)
      printf("ERROR reading from socket");
    printf("Here is the message with %d bytes: %s\n\n", n, buffer);
    strcpy(buffer, "Hi back from the C world");
    n = write(connection_fd, buffer, strlen(buffer));
    printf("Sent %d bytes \n", n);
    if (n < 0)
      printf("ERROR writing to socket\n");
    break;
  }
  close(socket_fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1232 <main+0x52>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f9 <main+0x219>
lea    0xdd8(%rip),%rdi
call   1030 <unlink@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
mov    $0x6c,%esi
lea    0xda8(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
je     12a3 <main+0xc3>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f9 <main+0x219>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
cmp    $0x0,%rax
je     12d4 <main+0xf4>
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f9 <main+0x219>
movl   $0x8,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jle    13ea <main+0x20a>
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   10b0 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    134b <main+0x16b>
lea    0xd1a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x124(%rbp),%esi
lea    -0x120(%rbp),%rdx
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x120(%rbp),%rdi
lea    0xd2c(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x134(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%esi
lea    0xcf6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x124(%rbp)
jge    13e5 <main+0x205>
lea    0xceb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13ea <main+0x20a>
mov    -0x14(%rbp),%edi
call   10a0 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    0x2de4(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
js     1272 <main+0x82>
mov    %eax,%r15d
lea    0xddc(%rip),%rbp
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10a0 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    $0x6c,%esi
mov    %rbp,%rdx
call   1090 <snprintf@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   10e0 <bind@plt>
test   %rax,%rax
je     127b <main+0x8b>
lea    0xe4f(%rip),%rdi
jmp    1294 <main+0xa4>
lea    0xe54(%rip),%rdi
jmp    1294 <main+0xa4>
mov    %r15d,%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
test   %rax,%rax
je     12ac <main+0xbc>
lea    0xe1b(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movl   $0x8,0xc(%rsp)
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   1100 <accept@plt>
test   %eax,%eax
js     136f <main+0x17f>
mov    %eax,%r14d
lea    0xda6(%rip),%rdi
call   1050 <puts@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10f0 <bzero@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   10c0 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1311 <main+0x121>
lea    0xd07(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xd13(%rip),%rdi
lea    0x10(%rsp),%rbx
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xd23(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    %eax,%ebx
lea    0xd16(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
test   %ebx,%ebx
jns    136f <main+0x17f>
lea    0xd2d(%rip),%rdi
call   1050 <puts@plt>
mov    %r15d,%edi
call   10b0 <close@plt>
xor    %eax,%eax
jmp    129e <main+0xae>

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"socket() failed\0A\00"
@.str.1 = [17 x i8] c"/tmp/demo_socket\00"
@.str.2 = [15 x i8] c"bind() failed\0A\00"
@.str.3 = [17 x i8] c"listen() failed\0A\00"
@.str.4 = [28 x i8] c"successfully received data\0A\00"
@.str.5 = [26 x i8] c"ERROR reading from socket\00"
@.str.6 = [40 x i8] c"Here is the message with %d bytes: %s\0A\0A\00"
@.str.7 = [25 x i8] c"Hi back from the C world\00"
@.str.8 = [16 x i8] c"Sent %d bytes \0A\00"
@.str.9 = [25 x i8] c"ERROR writing to socket\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%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 %74
16: ; preds = %0
%17 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @snprintf(i32 %22, i32 108, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = 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 %74
30: ; preds = %16
%31 = load i32, i32* %3
%32 = call i64 @listen(i32 %31, i32 5)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = 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 %74
36: ; preds = %30
store i32 8, i32* %5
br label %37
37: ; preds = %36
%38 = load i32, i32* %3
%39 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%40 = call i32 @accept(i32 %38, %struct.sockaddr* %39, i32* %5)
store i32 %40, i32* %4
%41 = icmp sgt i32 %40, -1
br i1 %41, label %42, label %71
42: ; preds = %37
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%45 = call i32 @bzero(i8* %44, i32 256)
%46 = load i32, i32* %4
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 255)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %42
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %51, %42
%54 = load i32, i32* %8
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %54, i8* %55)
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%58 = call i32 @strcpy(i8* %57, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%59 = load i32, i32* %4
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%62 = call i32 @strlen(i8* %61)
%63 = call i32 @write(i32 %59, i8* %60, i32 %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %8
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %53
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
br label %70
70: ; preds = %68, %53
br label %71
71: ; preds = %70, %37
%72 = load i32, i32* %3
%73 = call i32 @close(i32 %72)
store i32 0, i32* %1
br label %74
74: ; preds = %71, %34, %28, %14
%75 = load i32, i32* %1
ret i32 %75
}

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 = [17 x i8] c"/tmp/demo_socket\00"
@.str.5 = [26 x i8] c"ERROR reading from socket\00"
@.str.6 = [40 x i8] c"Here is the message with %d bytes: %s\0A\0A\00"
@.str.7 = [25 x i8] c"Hi back from the C world\00"
@.str.8 = [16 x i8] c"Sent %d bytes \0A\00"
@str = [27 x i8] c"successfully received data\00"
@str.10 = [24 x i8] c"ERROR writing to socket\00"
@str.11 = [16 x i8] c"listen() failed\00"
@str.12 = [14 x i8] c"bind() failed\00"
@str.13 = [16 x i8] c"socket() failed\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = bitcast %struct.sockaddr_un* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.13, i64 0, i64 0))
br label %52
13: ; preds = %0
%14 = tail call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 8)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @snprintf(i32 %19, i32 108, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%21 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%22 = call i64 @bind(i32 %9, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %13
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.12, i64 0, i64 0))
br label %52
26: ; preds = %13
%27 = call i64 @listen(i32 %9, i32 5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.11, i64 0, i64 0))
br label %52
31: ; preds = %26
store i32 8, i32* %2
%32 = call i32 @accept(i32 %9, %struct.sockaddr* %21, i32* %2)
%33 = icmp sgt i32 %32, -1
br i1 %33, label %34, label %50
34: ; preds = %31
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%36 = call i32 @bzero(i8* %6, i32 256)
%37 = call i32 @read(i32 %32, i8* %6, i32 255)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %37, i8* %6)
%43 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%44 = call i32 @strlen(i8* %6)
%45 = call i32 @write(i32 %32, i8* %6, i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i32 %45)
%47 = icmp slt i32 %45, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
br label %50
50: ; preds = %41, %48, %31
%51 = call i32 @close(i32 %9)
br label %52
52: ; preds = %50, %29, %24, %11
%53 = phi i32 [ 1, %11 ], [ 1, %24 ], [ 1, %29 ], [ 0, %50 ]
ret i32 %53
}