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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ warnx(char *);
int cli_conn(const char *name) {
  int sockfd, addrlen;
  struct sockaddr_un unix_addr;
  struct sockaddr *sock_addr;
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd < 0) {
    warn("socket");
    return -1;
  }
  addrlen = strlen(name);
  if (addrlen >= sizeof(unix_addr.sun_path)) {
    warnx("serv_listen: socket name too long");
    return -2;
  }
  addrlen += sizeof(unix_addr.sun_family);
  memset(&unix_addr, 0, sizeof(unix_addr));
  unix_addr.sun_family = AF_UNIX;
  strcpy(unix_addr.sun_path, name);
  sock_addr = (struct sockaddr *)&unix_addr;
  if (connect(sockfd, sock_addr, addrlen) < 0) {
    warn("connect");
    return -3;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<cli_conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ba <cli_conn+0x4a>
lea    0xe57(%rip),%rdi
call   1040 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <cli_conn+0xee>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
jb     11ec <cli_conn+0x7c>
lea    0xe2c(%rip),%rdi
call   1060 <warnx@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    125e <cli_conn+0xee>
movslq -0x18(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1258 <cli_conn+0xe8>
lea    0xde2(%rip),%rdi
call   1040 <warn@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    125e <cli_conn+0xee>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_conn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e62(%rip),%r12
mov    (%r12),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11c0 <cli_conn+0x50>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jb     11d3 <cli_conn+0x63>
lea    0xe53(%rip),%rdi
call   1060 <warnx@plt>
mov    $0xfffffffe,%ebx
jmp    1222 <cli_conn+0xb2>
lea    0xe39(%rip),%rdi
call   1040 <warn@plt>
mov    $0xffffffff,%ebx
jmp    1222 <cli_conn+0xb2>
mov    %eax,%ebp
add    $0x4,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    1222 <cli_conn+0xb2>
lea    0xe11(%rip),%rdi
call   1040 <warn@plt>
mov    $0xfffffffd,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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 = [34 x i8] c"serv_listen: socket name too long\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @cli_conn(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca %struct.sockaddr*
store i8* %0, i8** %3
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = icmp uge i64 %19, 4
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @warnx(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %2
br label %45
23: ; preds = %15
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = add i64 %25, 4
%27 = trunc i64 %26 to i32
store i32 %27, i32* %5
%28 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%29 = load i32, i32* @AF_UNIX
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %3
%34 = call i32 @strcpy(i32 %32, i8* %33)
%35 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
store %struct.sockaddr* %35, %struct.sockaddr** %7
%36 = load i32, i32* %4
%37 = load %struct.sockaddr*, %struct.sockaddr** %7
%38 = load i32, i32* %5
%39 = call i64 @connect(i32 %36, %struct.sockaddr* %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %23
%42 = call i32 @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %2
br label %45
43: ; preds = %23
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %41, %21, %13
%46 = load i32, i32* %2
ret i32 %46
}

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 = [34 x i8] c"serv_listen: socket name too long\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @cli_conn(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %28
10: ; preds = %1
%11 = tail call i32 @strlen(i8* %0)
%12 = icmp ugt i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @warnx(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %28
15: ; preds = %10
%16 = add nuw nsw i32 %11, 4
%17 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strcpy(i32 %21, i8* %0)
%23 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%24 = call i64 @connect(i32 %6, %struct.sockaddr* %23, i32 %16)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %15, %26, %13, %8
%29 = phi i32 [ -1, %8 ], [ -2, %13 ], [ -3, %26 ], [ %6, %15 ]
ret i32 %29
}