#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(int /*<<< orphan*/, char *, char const *);
int ud_connect(const char *name) {
  int fd;
  int r;
  struct sockaddr_un addr;
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    return fd;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  sprintf(addr.sun_path, "%s", name);
  r = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (r < 0) {
    close(fd);
    return r;
  }
  return fd;
}


x86 assembly (clang, -O0):
<ud_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118d <ud_connect+0x3d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <ud_connect+0xa3>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    0xe49(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ed <ud_connect+0x9d>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <ud_connect+0xa3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ud_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e87(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c3 <ud_connect+0x73>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
lea    0xe63(%rip),%rsi
mov    %r14,%rdx
call   1050 <sprintf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
test   %eax,%eax
jns    11c3 <ud_connect+0x73>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
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 = [3 x i8] c"%s\00"
define i32 @ud_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i32, i32* %4
store i32 %13, i32* %2
br label %33
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i8*, i8** %3
%21 = call i32 @sprintf(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 8)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %14
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %33
31: ; preds = %14
%32 = load i32, i32* %4
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %27, %12
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @ud_connect(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 %20, label %8
8: ; preds = %1
%9 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @sprintf(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i32 @connect(i32 %6, %struct.sockaddr* %15, i32 8)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %8
%19 = call i32 @close(i32 %6)
br label %20
20: ; preds = %8, %1, %18
%21 = phi i32 [ %16, %18 ], [ %6, %1 ], [ %6, %8 ]
ret i32 %21
}