#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;
};
struct reader {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
struct reader *reader_init(int, int);
int reader_readline(struct reader *, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ unlink(char *);
int main() {
  int lsock;
  struct sockaddr_un addr;
  int conn;
  lsock = socket(AF_UNIX, SOCK_STREAM, 0);
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, "test.sock", sizeof(addr.sun_path) - 1);
  if (bind(lsock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
    perror("bind");
    return 1;
  }
  if (listen(lsock, 0) == -1) {
    perror("listen");
    return 1;
  }
  conn = accept(lsock, NULL, NULL);
  struct reader *reader = reader_init(conn, 1024);
  char buf[8];
  int rc;
  int lineno = 0;
  fd_set rfds;
  for (;;) {
    FD_ZERO(&rfds);
    FD_SET(conn, &rfds);
    rc = select(conn + 1, &rfds, NULL, NULL, NULL);
    if (rc == -1) {
      perror("select");
      break;
    }
    for (;;) {
      rc = reader_readline(reader, buf, sizeof(buf));
      if (rc == -1 || rc == -3) {
        fprintf(stderr, "readline status=%d\n", rc);
        goto stop;
      } else if (rc == 0) {
        fprintf(stderr, "client disconnected\n");
        goto stop;
      } else if (rc == -2) {
        break;
      }
      fprintf(stderr, "%04d (rc=%2d): %s", ++lineno, rc, buf);
    }
  }
stop:
  close(conn);
  close(lsock);
  unlink("test.sock");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xddf(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    125d <main+0x7d>
lea    0xdbe(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13dd <main+0x1fd>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1288 <main+0xa8>
lea    0xd98(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13dd <main+0x1fd>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10e0 <accept@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
call   10b0 <reader_init@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
lea    -0x34(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12ff <main+0x11f>
lea    0xd21(%rip),%rdi
call   10d0 <perror@plt>
jmp    13bc <main+0x1dc>
jmp    1304 <main+0x124>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1060 <reader_readline@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     132d <main+0x14d>
cmpl   $0xfffffffd,-0x2c(%rbp)
jne    134c <main+0x16c>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xcdd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13c1 <main+0x1e1>
cmpl   $0x0,-0x2c(%rbp)
jne    1372 <main+0x192>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
lea    0xccb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13c1 <main+0x1e1>
cmpl   $0xfffffffe,-0x2c(%rbp)
jne    1381 <main+0x1a1>
jmp    13b7 <main+0x1d7>
jmp    1386 <main+0x1a6>
jmp    138b <main+0x1ab>
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%edx
add    $0x1,%edx
mov    %edx,-0x30(%rbp)
mov    -0x2c(%rbp),%ecx
lea    -0x28(%rbp),%r8
lea    0xc9b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1304 <main+0x124>
jmp    12b2 <main+0xd2>
jmp    13c1 <main+0x1e1>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    0xc28(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2df3(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x1c(%rsp)
lea    0xdea(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
je     1396 <main+0x1b6>
mov    %ebp,%edi
xor    %esi,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
je     139f <main+0x1bf>
mov    %ebp,0xc(%rsp)
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
mov    %eax,%r12d
mov    %eax,%edi
mov    $0x400,%esi
call   10b0 <reader_init@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   10f0 <FD_SET@plt>
mov    %r12d,0x14(%rsp)
add    $0x1,%r12d
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
cmp    $0xffffffff,%eax
je     1331 <main+0x151>
lea    0x20(%rsp),%rbp
mov    0x2d23(%rip),%r15
lea    0xd8a(%rip),%r13
xor    %r14d,%r14d
nop
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1060 <reader_readline@plt>
mov    %eax,%ecx
not    %ecx
test   $0xfffffffd,%ecx
je     133f <main+0x15f>
cmp    $0xfffffffe,%eax
je     1300 <main+0x120>
test   %eax,%eax
je     1354 <main+0x174>
mov    (%r15),%edi
add    $0x1,%r14d
mov    %r13,%rsi
mov    %r14d,%edx
mov    %eax,%ecx
mov    %rbp,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12c0 <main+0xe0>
lea    0x10(%rsp),%rdi
call   1080 <FD_ZERO@plt>
mov    0x14(%rsp),%edi
lea    0x10(%rsp),%rsi
call   10f0 <FD_SET@plt>
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
cmp    $0xffffffff,%eax
jne    12c0 <main+0xe0>
lea    0xcde(%rip),%rdi
call   10d0 <perror@plt>
jmp    1365 <main+0x185>
mov    (%r15),%edi
lea    0xcd4(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1365 <main+0x185>
mov    (%r15),%edi
lea    0xcd3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0xc(%rsp),%ebx
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xc80(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc6d(%rip),%rdi
jmp    13a6 <main+0x1c6>
lea    0xc69(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%eax
jmp    1387 <main+0x1a7>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.reader = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"test.sock\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"select\00"
@stderr = global i32 0
@.str.4 = [20 x i8] c"readline status=%d\0A\00"
@.str.5 = [21 x i8] c"client disconnected\0A\00"
@.str.6 = [18 x i8] c"%04d (rc=%2d): %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = alloca %struct.reader*
%6 = alloca [8 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
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 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strncpy(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 3)
%18 = load i32, i32* %2
%19 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%20 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %87
24: ; preds = %0
%25 = load i32, i32* %2
%26 = call i32 @listen(i32 %25, i32 0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %87
30: ; preds = %24
%31 = load i32, i32* %2
%32 = call i32 @accept(i32 %31, i32* null, i32* null)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = call %struct.reader* @reader_init(i32 %33, i32 1024)
store %struct.reader* %34, %struct.reader** %5
store i32 0, i32* %8
br label %35
35: ; preds = %79, %30
%36 = call i32 @FD_ZERO(i32* %9)
%37 = load i32, i32* %4
%38 = call i32 @FD_SET(i32 %37, i32* %9)
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
%41 = call i32 @select(i32 %40, i32* %9, i32* null, i32* null, i32* null)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %35
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %80
46: ; preds = %35
br label %47
47: ; preds = %72, %46
%48 = load %struct.reader*, %struct.reader** %5
%49 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%50 = call i32 @reader_readline(%struct.reader* %48, i8* %49, i32 8)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, -1
br i1 %52, label %56, label %53
53: ; preds = %47
%54 = load i32, i32* %7
%55 = icmp eq i32 %54, -3
br i1 %55, label %56, label %60
56: ; preds = %53, %47
%57 = load i32, i32* @stderr
%58 = load i32, i32* %7
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %58)
br label %81
60: ; preds = %53
%61 = load i32, i32* %7
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* @stderr
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %81
66: ; preds = %60
%67 = load i32, i32* %7
%68 = icmp eq i32 %67, -2
br i1 %68, label %69, label %70
69: ; preds = %66
br label %79
70: ; preds = %66
br label %71
71: ; preds = %70
br label %72
72: ; preds = %71
%73 = load i32, i32* @stderr
%74 = load i32, i32* %8
%75 = add nsw i32 %74, 1
store i32 %75, i32* %8
%76 = load i32, i32* %7
%77 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %75, i32 %76, i8* %77)
br label %47
79: ; preds = %69
br label %35
80: ; preds = %44
br label %81
81: ; preds = %80, %63, %56
%82 = load i32, i32* %4
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* %2
%85 = call i32 @close(i32 %84)
%86 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %87
87: ; preds = %81, %28, %22
%88 = load i32, i32* %1
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
%struct.reader = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"test.sock\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"select\00"
@stderr = global i32 0
@.str.4 = [20 x i8] c"readline status=%d\0A\00"
@.str.5 = [21 x i8] c"client disconnected\0A\00"
@.str.6 = [18 x i8] c"%04d (rc=%2d): %s\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [8 x i8]
%3 = alloca i32
%4 = bitcast %struct.sockaddr_un* %1 to i8*
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 3)
%11 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%12 = call i32 @bind(i32 %7, %struct.sockaddr* %11, i32 8)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %64
16: ; preds = %0
%17 = call i32 @listen(i32 %7, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %64
21: ; preds = %16
%22 = call i32 @accept(i32 %7, i32* null, i32* null)
%23 = call %struct.reader* @reader_init(i32 %22, i32 1024)
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%25 = bitcast i32* %3 to i8*
%26 = call i32 @FD_ZERO(i32* %3)
%27 = call i32 @FD_SET(i32 %22, i32* %3)
%28 = add nsw i32 %22, 1
%29 = call i32 @select(i32 %28, i32* %3, i32* null, i32* null, i32* null)
%30 = icmp eq i32 %29, -1
br i1 %30, label %41, label %36
31: ; preds = %47
%32 = call i32 @FD_ZERO(i32* %3)
%33 = call i32 @FD_SET(i32 %22, i32* %3)
%34 = call i32 @select(i32 %28, i32* %3, i32* null, i32* null, i32* null)
%35 = icmp eq i32 %34, -1
br i1 %35, label %41, label %36
36: ; preds = %21, %31
%37 = phi i32 [ %49, %31 ], [ 0, %21 ]
%38 = call i32 @reader_readline(%struct.reader* %23, i8* %24, i32 8)
%39 = and i32 %38, -3
%40 = icmp eq i32 %39, -3
br i1 %40, label %43, label %47
41: ; preds = %31, %21
%42 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %60
43: ; preds = %36, %53
%44 = phi i32 [ %57, %53 ], [ %38, %36 ]
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %60
47: ; preds = %36, %53
%48 = phi i32 [ %57, %53 ], [ %38, %36 ]
%49 = phi i32 [ %55, %53 ], [ %37, %36 ]
switch i32 %48, label %53 [
i32 0, label %50
i32 -2, label %31
]
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %60
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = add nsw i32 %49, 1
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %55, i32 %48, i8* %24)
%57 = call i32 @reader_readline(%struct.reader* %23, i8* %24, i32 8)
%58 = and i32 %57, -3
%59 = icmp eq i32 %58, -3
br i1 %59, label %43, label %47
60: ; preds = %41, %50, %43
%61 = call i32 @close(i32 %22)
%62 = call i32 @close(i32 %7)
%63 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %64
64: ; preds = %60, %19, %14
%65 = phi i32 [ 1, %14 ], [ 1, %19 ], [ 0, %60 ]
ret i32 %65
}