#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 {
  scalar_t__ *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ ENOENT;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ chmod(char const *, int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int listen(int, int const);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int strlen(char const *);
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
int unlink(char const *);
char start_server(const char *path, int *socket_fd) {
  assert(path != 0);
  assert(socket_fd != 0);
  *socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (*socket_fd == -1) {
    perror("socket");
    return 0;
  }
  struct sockaddr_un local;
  local.sun_family = AF_UNIX;
  if (strlen(path) >= 108) {
    fprintf(stderr, "Shim: Path %s is too long!", path);
    return 0;
  }
  strncpy(local.sun_path, path, 108 - 1);
  local.sun_path[108 - 1] = 0;
  if (unlink(path) == -1 && errno != ENOENT) {
    perror("unlink");
    return 0;
  }
  socklen_t local_len = strlen(path) + sizeof(local.sun_family);
  if (bind(*socket_fd, (struct sockaddr *)&local, local_len) == -1) {
    perror("bind");
    return 0;
  }
  const int backlog = 5;
  if (listen(*socket_fd, backlog) == -1) {
    perror("listen");
    return 0;
  }
  chmod(path, 0660);
  return 1;
}


x86 assembly (clang, -O0):
<start_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    1217 <start_server+0x77>
lea    0xdf7(%rip),%rdi
call   10b0 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    1342 <start_server+0x1a2>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x6c,%eax
jl     1257 <start_server+0xb7>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdbe(%rip),%rsi
call   1070 <fprintf@plt>
movb   $0x0,-0x1(%rbp)
jmp    1342 <start_server+0x1a2>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x6b,%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
movq   $0x0,0x358(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    12b9 <start_server+0x119>
mov    0x2d37(%rip),%rax
mov    (%rax),%rax
mov    0x2d1d(%rip),%rcx
cmp    (%rcx),%rax
je     12b9 <start_server+0x119>
lea    0xd77(%rip),%rdi
call   10b0 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    1342 <start_server+0x1a2>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12fb <start_server+0x15b>
lea    0xd3c(%rip),%rdi
call   10b0 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    1342 <start_server+0x1a2>
movl   $0x5,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    1330 <start_server+0x190>
lea    0xd0c(%rip),%rdi
call   10b0 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    1342 <start_server+0x1a2>
mov    -0x10(%rbp),%rdi
mov    $0x1b0,%esi
call   1090 <chmod@plt>
movb   $0x1,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_server>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     11d5 <start_server+0x75>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0x6c,%eax
jl     11e3 <start_server+0x83>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
jmp    11cb <start_server+0x6b>
mov    %r14,%rsi
mov    $0x6b,%edx
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Shim: Path %s is too long!\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [7 x i8] c"unlink\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
define signext i8 @start_server(i8* %0, i32* %1) {
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i32, i32* @AF_UNIX
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = load i32*, i32** %5
store i32 %19, i32* %20
%21 = load i32*, i32** %5
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8 0, i8* %3
br label %77
26: ; preds = %2
%27 = load i32, i32* @AF_UNIX
%28 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i8*, i8** %4
%30 = call i32 @strlen(i8* %29)
%31 = icmp sge i32 %30, 108
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %4
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i8 0, i8* %3
br label %77
36: ; preds = %26
%37 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%38 = load i64*, i64** %37
%39 = load i8*, i8** %4
%40 = call i32 @strncpy(i64* %38, i8* %39, i32 107)
%41 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%42 = load i64*, i64** %41
%43 = getelementptr inbounds i64, i64* %42, i64 107
store i64 0, i64* %43
%44 = load i8*, i8** %4
%45 = call i32 @unlink(i8* %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %36
%48 = load i64, i64* @errno
%49 = load i64, i64* @ENOENT
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8 0, i8* %3
br label %77
53: ; preds = %47, %36
%54 = load i8*, i8** %4
%55 = call i32 @strlen(i8* %54)
%56 = sext i32 %55 to i64
%57 = add i64 %56, 4
%58 = trunc i64 %57 to i32
store i32 %58, i32* %7
%59 = load i32*, i32** %5
%60 = load i32, i32* %59
%61 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%62 = load i32, i32* %7
%63 = call i32 @bind(i32 %60, %struct.sockaddr* %61, i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %53
%66 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8 0, i8* %3
br label %77
67: ; preds = %53
store i32 5, i32* %8
%68 = load i32*, i32** %5
%69 = load i32, i32* %68
%70 = call i32 @listen(i32 %69, i32 5)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i8 0, i8* %3
br label %77
74: ; preds = %67
%75 = load i8*, i8** %4
%76 = call i32 @chmod(i8* %75, i32 432)
store i8 1, i8* %3
br label %77
77: ; preds = %74, %72, %65, %51, %32, %24
%78 = load i8, i8* %3
ret i8 %78
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Shim: Path %s is too long!\00"
@errno = global i64 0
@ENOENT = global i64 0
define signext i8 @start_server(i8* %0, i32* %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i32* %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %1
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %23
15: ; preds = %2
%16 = tail call i32 @strlen(i8* %0)
%17 = icmp sgt i32 %16, 107
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
21: ; preds = %15
%22 = tail call i32 @strncpy(i64* undef, i8* %0, i32 107)
unreachable
23: ; preds = %18, %13
ret i8 0
}