#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int S_IRUSR;
int S_IWUSR;
int bind(int, struct sockaddr *, int);
scalar_t__ chmod(char *, int);
int /*<<< orphan*/ close(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int setup_clnt() {
  int s;
  struct sockaddr_un local;
  int len;
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    return (-1);
  }
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, "/dev/sccd");
  unlink(local.sun_path);
  len = strlen(local.sun_path) + sizeof(local.sun_family);
  if (bind(s, (struct sockaddr *)&local, len) == -1) {
    perror("bind");
    close(s);
    return (-1);
  }
  if (listen(s, 5) == -1) {
    perror("listen");
    close(s);
    return (-1);
  }
  if (chmod("/dev/sccd", (S_IWUSR | S_IRUSR)) < 0) {
    perror("chmod socket");
  }
  return (s);
}


x86 assembly (clang, -O0):
<setup_clnt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11d5 <setup_clnt+0x45>
lea    0xe58(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <setup_clnt+0x121>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe15(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1241 <setup_clnt+0xb1>
lea    0xde2(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <setup_clnt+0x121>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1277 <setup_clnt+0xe7>
lea    0xdb1(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <setup_clnt+0x121>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
mov    0x2d41(%rip),%rax
or     (%rax),%esi
lea    0xd70(%rip),%rdi
call   1080 <chmod@plt>
cmp    $0x0,%rax
jge    12ab <setup_clnt+0x11b>
lea    0xd70(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_clnt>:
push   %r14
push   %rbx
push   %rax
mov    0x2e4d(%rip),%r14
mov    (%r14),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1222 <setup_clnt+0x92>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xe3d(%rip),%rsi
call   1040 <strcpy@plt>
call   1030 <unlink@plt>
call   1050 <strlen@plt>
lea    0x4(%rax),%edx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1235 <setup_clnt+0xa5>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     123e <setup_clnt+0xae>
mov    0x2de4(%rip),%rax
mov    0x2dc5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdf2(%rip),%rdi
call   1080 <chmod@plt>
test   %rax,%rax
js     1258 <setup_clnt+0xc8>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1218 <setup_clnt+0x88>
lea    0xdce(%rip),%rdi
jmp    1245 <setup_clnt+0xb5>
lea    0xdca(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    1218 <setup_clnt+0x88>
lea    0xdb7(%rip),%rdi
call   10a0 <perror@plt>
jmp    1218 <setup_clnt+0x88>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [10 x i8] c"/dev/sccd\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@.str.4 = [13 x i8] c"chmod socket\00"
define i32 @setup_clnt() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %53
11: ; preds = %0
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @unlink(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strlen(i32 %21)
%23 = sext i32 %22 to i64
%24 = add i64 %23, 4
%25 = trunc i64 %24 to i32
store i32 %25, i32* %4
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%28 = load i32, i32* %4
%29 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %11
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %1
br label %53
35: ; preds = %11
%36 = load i32, i32* %2
%37 = call i32 @listen(i32 %36, i32 5)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %1
br label %53
43: ; preds = %35
%44 = load i32, i32* @S_IWUSR
%45 = load i32, i32* @S_IRUSR
%46 = or i32 %44, %45
%47 = call i64 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %43
%52 = load i32, i32* %2
store i32 %52, i32* %1
br label %53
53: ; preds = %51, %39, %31, %9
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [10 x i8] c"/dev/sccd\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@.str.4 = [13 x i8] c"chmod socket\00"
define i32 @setup_clnt() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %36
9: ; preds = %0
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @unlink(i32 undef)
%14 = tail call i32 @strlen(i32 undef)
%15 = add i32 %14, 4
%16 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%17 = call i32 @bind(i32 %5, %struct.sockaddr* %16, i32 %15)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @close(i32 %5)
br label %36
22: ; preds = %9
%23 = call i32 @listen(i32 %5, i32 5)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @close(i32 %5)
br label %36
28: ; preds = %22
%29 = load i32, i32* @S_IWUSR
%30 = load i32, i32* @S_IRUSR
%31 = or i32 %30, %29
%32 = call i64 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %28, %34, %25, %19, %7
%37 = phi i32 [ -1, %7 ], [ -1, %19 ], [ -1, %25 ], [ %5, %34 ], [ %5, %28 ]
ret i32 %37
}