#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*/ sun;
struct sockaddr_un {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_NETBEUI;
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int connect(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int main() {
  int sockfd;
  int err;
  char buf[2048];
  struct sockaddr_un sun = {
      .sun_family = AF_NETBEUI,
      .sun_path = "/tmp/sockipn",
  };
  sockfd = socket(AF_NETBEUI, SOCK_RAW, 1);
  if (sockfd < 0) {
    perror("socket");
    exit(1);
  };
  err = bind(sockfd, (struct sockaddr *)&sun, sizeof(sun));
  if (err < 0) {
    perror("bind");
    exit(1);
  };
  err = connect(sockfd, NULL, 0);
  if (err < 0) {
    perror("connect");
    exit(1);
  };
  err = read(sockfd, buf, 2048);
  printf("I read... %s", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x820(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x818(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11de <main+0x6e>
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x10,%edx
call   1050 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1215 <main+0xa5>
lea    0xe0e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <connect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1246 <main+0xd6>
lea    0xde2(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
lea    -0x810(%rbp),%rsi
lea    0xdb6(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x818,%rsp
lea    0xe7f(%rip),%rax
mov    %rax,(%rsp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11fd <main+0x8d>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1050 <bind@plt>
test   %eax,%eax
js     1206 <main+0x96>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <connect@plt>
test   %eax,%eax
js     120f <main+0x9f>
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x800,%edx
call   1040 <read@plt>
lea    0xe39(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe09(%rip),%rdi
jmp    1216 <main+0xa6>
lea    0xe07(%rip),%rdi
jmp    1216 <main+0xa6>
lea    0xe03(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"/tmp/sockipn\00"
@AF_NETBEUI = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [13 x i8] c"I read... %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2048 x i8]
%5 = alloca %struct.sockaddr_un
store i32 0, i32* %1
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
%8 = load i32, i32* @AF_NETBEUI
store i32 %8, i32* %7
%9 = load i32, i32* @AF_NETBEUI
%10 = load i32, i32* @SOCK_RAW
%11 = call i32 @socket(i32 %9, i32 %10, i32 1)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* %2
%19 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%20 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 16)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32, i32* %2
%28 = call i32 @connect(i32 %27, i32* null, i32 0)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i32, i32* %2
%36 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 2048)
store i32 %37, i32* %3
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%39 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %38)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"/tmp/sockipn\00"
@AF_NETBEUI = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [13 x i8] c"I read... %s\00"
define i32 @main() {
%1 = alloca [2048 x i8]
%2 = alloca %struct.sockaddr_un
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
%7 = load i32, i32* @AF_NETBEUI
store i32 %7, i32* %6
%8 = load i32, i32* @SOCK_RAW
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 1)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i32 @bind(i32 %9, %struct.sockaddr* %15, i32 16)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32 @connect(i32 %9, i32* null, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = call i32 @read(i32 %9, i8* %3, i32 2048)
%29 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %3)
ret i32 0
}