#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;
typedef int /*<<< orphan*/ local;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ report_error();
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
void stub1(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
void server_socket(char *address, void (*functionPtr)(int)) {
  struct sockaddr_un local, remote;
  int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd == -1) {
    syslog(LOG_ERR, "Could not create socket.");
    report_error();
    exit(EXIT_FAILURE);
  }
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, address);
  unlink(local.sun_path);
  int local_len = sizeof(local);
  int status = bind(sockfd, (struct sockaddr *)&local, local_len);
  if (status == -1) {
    syslog(LOG_ERR, "Could not bind socket to address.");
    report_error();
    exit(EXIT_FAILURE);
  }
  status = listen(sockfd, 5);
  if (status == -1) {
    syslog(LOG_ERR, "Could not listen at socket.");
    report_error();
    exit(EXIT_FAILURE);
  }
  while (true) {
    int remote_len = sizeof(remote);
    int connectedfd = accept(sockfd, (struct sockaddr *)&remote, &remote_len);
    if (connectedfd == -1) {
      syslog(LOG_WARNING, "Failed to connect to client.");
      report_error();
    } else {
      (*functionPtr)(connectedfd);
    }
    status = close(connectedfd);
    if (status == -1) {
      syslog(LOG_WARNING, "Could not close connection with client.");
      report_error();
    }
  }
  close(sockfd);
  unlink(local.sun_path);
}


x86 assembly (clang, -O0):
<server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1200 <server_socket+0x60>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x0,%al
call   1080 <report_error@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%edi
call   1030 <unlink@plt>
movl   $0x8,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1090 <bind@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    126d <server_socket+0xcd>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x0,%al
call   1080 <report_error@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x24(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12b1 <server_socket+0x111>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x0,%al
call   1080 <report_error@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    12b6 <server_socket+0x116>
movl   $0x8,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12fb <server_socket+0x15b>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x0,%al
call   1080 <report_error@plt>
jmp    1304 <server_socket+0x164>
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%edi
call   *%rax
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1335 <server_socket+0x195>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
lea    0xd4b(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x0,%al
call   1080 <report_error@plt>
jmp    12b6 <server_socket+0x116>

x86 assembly (clang, -O2):
<server_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e2d(%rip),%r15
mov    (%r15),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     129d <server_socket+0xfd>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rsi
call   1040 <strcpy@plt>
call   1030 <unlink@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     12af <server_socket+0x10f>
mov    %ebp,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     12c1 <server_socket+0x121>
lea    0x10(%rsp),%r15
lea    0x4(%rsp),%rbx
mov    0x2db3(%rip),%r13
nopl   (%rax)
movl   $0x8,0x4(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10a0 <accept@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     1260 <server_socket+0xc0>
mov    %r12d,%edi
call   *%r14
jmp    1277 <server_socket+0xd7>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%edi
lea    0xdec(%rip),%rsi
call   1060 <syslog@plt>
xor    %eax,%eax
call   1080 <report_error@plt>
mov    %r12d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1230 <server_socket+0x90>
mov    0x0(%r13),%edi
lea    0xde5(%rip),%rsi
call   1060 <syslog@plt>
xor    %eax,%eax
call   1080 <report_error@plt>
jmp    1230 <server_socket+0x90>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd53(%rip),%rsi
jmp    12d1 <server_socket+0x131>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xd5a(%rip),%rsi
jmp    12d1 <server_socket+0x131>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
call   1060 <syslog@plt>
xor    %eax,%eax
call   1080 <report_error@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10b0 <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
@LOG_ERR = global i32 0
@.str = [25 x i8] c"Could not create socket.\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [34 x i8] c"Could not bind socket to address.\00"
@.str.2 = [28 x i8] c"Could not listen at socket.\00"
@LOG_WARNING = global i32 0
@.str.3 = [29 x i8] c"Failed to connect to client.\00"
@.str.4 = [40 x i8] c"Could not close connection with client.\00"
define void @server_socket(i8* %0, void (i32)* %1) {
%3 = alloca i8*
%4 = alloca void (i32)*
%5 = alloca %struct.sockaddr_un
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store void (i32)* %1, void (i32)** %4
%12 = load i32, i32* @AF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @LOG_ERR
%19 = call i32 @syslog(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%20 = call i32 (...) @report_error()
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i32, i32* @AF_UNIX
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i8*, i8** %3
%29 = call i32 @strcpy(i32 %27, i8* %28)
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @unlink(i32 %31)
store i32 8, i32* %8
%33 = load i32, i32* %7
%34 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%35 = load i32, i32* %8
%36 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %23
%40 = load i32, i32* @LOG_ERR
%41 = call i32 @syslog(i32 %40, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%42 = call i32 (...) @report_error()
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %23
%46 = load i32, i32* %7
%47 = call i32 @listen(i32 %46, i32 5)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %56
50: ; preds = %45
%51 = load i32, i32* @LOG_ERR
%52 = call i32 @syslog(i32 %51, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%53 = call i32 (...) @report_error()
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %45
br label %57
57: ; preds = %56, %79
store i32 8, i32* %10
%58 = load i32, i32* %7
%59 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%60 = call i32 @accept(i32 %58, %struct.sockaddr* %59, i32* %10)
store i32 %60, i32* %11
%61 = load i32, i32* %11
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %67
63: ; preds = %57
%64 = load i32, i32* @LOG_WARNING
%65 = call i32 @syslog(i32 %64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%66 = call i32 (...) @report_error()
br label %70
67: ; preds = %57
%68 = load void (i32)*, void (i32)** %4
%69 = load i32, i32* %11
br label %70
70: ; preds = %67, %63
%71 = load i32, i32* %11
%72 = call i32 @close(i32 %71)
store i32 %72, i32* %9
%73 = load i32, i32* %9
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %79
75: ; preds = %70
%76 = load i32, i32* @LOG_WARNING
%77 = call i32 @syslog(i32 %76, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%78 = call i32 (...) @report_error()
br label %79
79: ; preds = %75, %70
br label %57
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@LOG_ERR = global i32 0
@.str = [25 x i8] c"Could not create socket.\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [34 x i8] c"Could not bind socket to address.\00"
@.str.2 = [28 x i8] c"Could not listen at socket.\00"
@LOG_WARNING = global i32 0
@.str.3 = [29 x i8] c"Failed to connect to client.\00"
@.str.4 = [40 x i8] c"Could not close connection with client.\00"
define void @server_socket(i8* %0, void (i32)* nocapture %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = bitcast %struct.sockaddr_un* %3 to i8*
%7 = bitcast %struct.sockaddr_un* %4 to i8*
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @LOG_ERR
%14 = tail call i32 @syslog(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 (...) @report_error()
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %19, i32* %20
%21 = tail call i32 @strcpy(i32 undef, i8* %0)
%22 = tail call i32 @unlink(i32 undef)
%23 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%24 = call i32 @bind(i32 %10, %struct.sockaddr* %23, i32 8)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i32, i32* @LOG_ERR
%28 = call i32 @syslog(i32 %27, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 (...) @report_error()
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %18
%33 = call i32 @listen(i32 %10, i32 5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = bitcast i32* %5 to i8*
%37 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
br label %44
38: ; preds = %32
%39 = load i32, i32* @LOG_ERR
%40 = call i32 @syslog(i32 %39, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 (...) @report_error()
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %35, %59
store i32 8, i32* %5
%45 = call i32 @accept(i32 %10, %struct.sockaddr* %37, i32* %5)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32, i32* @LOG_WARNING
%49 = call i32 @syslog(i32 %48, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 (...) @report_error()
br label %52
51: ; preds = %44
br label %52
52: ; preds = %51, %47
%53 = call i32 @close(i32 %45)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i32, i32* @LOG_WARNING
%57 = call i32 @syslog(i32 %56, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 (...) @report_error()
br label %59
59: ; preds = %55, %52
br label %44
}