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

/* Variables and functions */
void *AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ S_IRWXU;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ chmod(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int getpid();
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(int /*<<< orphan*/, char *, char *, int, char);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int cli_conn(const char *name, char project) {
  int fd, len;
  struct sockaddr_un unix_addr;
  if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
    return (-1);
  memset(&unix_addr, 0, sizeof(unix_addr));
  unix_addr.sun_family = AF_UNIX;
  sprintf(unix_addr.sun_path, "%s%05d-%c", "/var/tmp/", getpid(), project);
  len = sizeof(unix_addr.sun_family) + strlen(unix_addr.sun_path);
  unlink(unix_addr.sun_path);
  if (bind(fd, (struct sockaddr *)&unix_addr, len) < 0)
    goto error;
  if (chmod(unix_addr.sun_path, S_IRWXU) < 0)
    goto error;
  memset(&unix_addr, 0, sizeof(unix_addr));
  unix_addr.sun_family = AF_UNIX;
  strcpy(unix_addr.sun_path, name);
  len = sizeof(unix_addr.sun_family) + strlen(unix_addr.sun_path);
  if (connect(fd, (struct sockaddr *)&unix_addr, len) < 0)
    goto error;
  return (fd);
error:
  close(fd);
  return (-1);
}


x86 assembly (clang, -O0):
<cli_conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11f4 <cli_conn+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <cli_conn+0x156>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%ecx
lea    0xdd5(%rip),%rsi
lea    0xdd8(%rip),%rdx
movsbl -0x11(%rbp),%r8d
call   10b0 <sprintf@plt>
mov    -0x30(%rbp),%edi
call   1060 <strlen@plt>
cltq
add    $0x8,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1273 <cli_conn+0xc3>
jmp    12f7 <cli_conn+0x147>
mov    -0x30(%rbp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
call   1090 <chmod@plt>
cmp    $0x0,%rax
jge    1293 <cli_conn+0xe3>
jmp    12f7 <cli_conn+0x147>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%edi
call   1060 <strlen@plt>
cltq
add    $0x8,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    12ec <cli_conn+0x13c>
jmp    12f7 <cli_conn+0x147>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1306 <cli_conn+0x156>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_conn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r13d
mov    %rdi,%rbx
mov    0x2e15(%rip),%r14
mov    (%r14),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     12b9 <cli_conn+0x109>
mov    %eax,%ebp
mov    %rbx,0x10(%rsp)
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    (%r14),%rax
mov    %rax,0x8(%rsp)
mov    (%rsp),%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
movsbl %r13b,%r8d
lea    0xdde(%rip),%rsi
lea    0xde1(%rip),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   10b0 <sprintf@plt>
mov    (%rsp),%edi
call   1060 <strlen@plt>
lea    0x8(%rax),%ebx
mov    (%rsp),%edi
call   1030 <unlink@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     12b2 <cli_conn+0x102>
mov    (%rsp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
call   1090 <chmod@plt>
test   %rax,%rax
js     12b2 <cli_conn+0x102>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    (%r14),%rax
mov    %rax,0x8(%rsp)
mov    (%rsp),%edi
mov    0x10(%rsp),%rsi
call   1040 <strcpy@plt>
mov    (%rsp),%edi
call   1060 <strlen@plt>
lea    0x8(%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   10c0 <connect@plt>
test   %rax,%rax
js     12b2 <cli_conn+0x102>
mov    %ebp,%r12d
jmp    12b9 <cli_conn+0x109>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i8* }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i8* null
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"%s%05d-%c\00"
@.str.1 = [10 x i8] c"/var/tmp/\00"
@S_IRWXU = global i32 0
define i32 @cli_conn(i8* %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i8 %1, i8* %5
%9 = load i8*, i8** @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i8* %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %70
14: ; preds = %2
%15 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 16)
%16 = load i8*, i8** @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i8* %16, i8** %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 (...) @getpid()
%21 = load i8, i8* %5
%22 = call i32 @sprintf(i32 %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8 signext %21)
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @strlen(i32 %24)
%26 = sext i32 %25 to i64
%27 = add i64 8, %26
%28 = trunc i64 %27 to i32
store i32 %28, i32* %7
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @unlink(i32 %30)
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%34 = load i32, i32* %7
%35 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %14
br label %67
38: ; preds = %14
%39 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* @S_IRWXU
%42 = call i64 @chmod(i32 %40, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38
br label %67
45: ; preds = %38
%46 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 16)
%47 = load i8*, i8** @AF_UNIX
%48 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i8* %47, i8** %48
%49 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i8*, i8** %4
%52 = call i32 @strcpy(i32 %50, i8* %51)
%53 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @strlen(i32 %54)
%56 = sext i32 %55 to i64
%57 = add i64 8, %56
%58 = trunc i64 %57 to i32
store i32 %58, i32* %7
%59 = load i32, i32* %6
%60 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%61 = load i32, i32* %7
%62 = call i64 @connect(i32 %59, %struct.sockaddr* %60, i32 %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %45
br label %67
65: ; preds = %45
%66 = load i32, i32* %6
store i32 %66, i32* %3
br label %70
67: ; preds = %64, %44, %37
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %3
br label %70
70: ; preds = %67, %65, %13
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i8* }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i8* null
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"%s%05d-%c\00"
@.str.1 = [10 x i8] c"/var/tmp/\00"
@S_IRWXU = global i32 0
define i32 @cli_conn(i8* %0, i8 signext %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = load i8*, i8** @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i8* %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %42, label %9
9: ; preds = %2
%10 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%11 = load i8*, i8** @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i8* %11, i8** %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 (...) @getpid()
%16 = call i32 @sprintf(i32 %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8 signext %1)
%17 = load i32, i32* %13
%18 = call i32 @strlen(i32 %17)
%19 = add i32 %18, 8
%20 = load i32, i32* %13
%21 = call i32 @unlink(i32 %20)
%22 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%23 = call i64 @bind(i32 %7, %struct.sockaddr* %22, i32 %19)
%24 = icmp slt i64 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %9
%26 = load i32, i32* %13
%27 = load i32, i32* @S_IRWXU
%28 = call i64 @chmod(i32 %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %25
%31 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%32 = load i8*, i8** @AF_UNIX
store i8* %32, i8** %12
%33 = load i32, i32* %13
%34 = call i32 @strcpy(i32 %33, i8* %0)
%35 = load i32, i32* %13
%36 = call i32 @strlen(i32 %35)
%37 = add i32 %36, 8
%38 = call i64 @connect(i32 %7, %struct.sockaddr* %22, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %30, %25, %9
%41 = call i32 @close(i32 %7)
br label %42
42: ; preds = %30, %2, %40
%43 = phi i32 [ -1, %40 ], [ -1, %2 ], [ %7, %30 ]
ret i32 %43
}