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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ unlink(char const *);
int connect_server(const char *domain_name) {
  int connect_fd;
  struct sockaddr_un srv_addr;
  int ret;
  connect_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (connect_fd < 0) {
    perror("client create socket failed");
    return -1;
  }
  srv_addr.sun_family = AF_UNIX;
  strcpy(srv_addr.sun_path, domain_name);
  ret = connect(connect_fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
  if (ret == -1) {
    char buf[128];
    sprintf(buf, "pid %d, %s", getpid(), "connect to server failed!");
    perror(buf);
    close(connect_fd);
    unlink(domain_name);
    return -1;
  }
  return connect_fd;
}


x86 assembly (clang, -O0):
<connect_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11cd <connect_server+0x4d>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <connect_server+0xe3>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    125d <connect_server+0xdd>
lea    -0xb0(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0xb8(%rbp),%rdi
mov    %eax,%edx
lea    0xdf4(%rip),%rsi
lea    0xdf8(%rip),%rcx
call   1080 <sprintf@plt>
lea    -0xb0(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <connect_server+0xe3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %rdi,%rbx
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     11e6 <connect_server+0x66>
mov    %eax,%ebp
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     11f4 <connect_server+0x74>
mov    %ebp,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
jmp    122f <connect_server+0xaf>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe1a(%rip),%rsi
lea    0xe1e(%rip),%rcx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebp
jmp    11d8 <connect_server+0x58>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"client create socket failed\00"
@AF_UNIX = global i32 0
@.str.1 = [11 x i8] c"pid %d, %s\00"
@.str.2 = [26 x i8] c"connect to server failed!\00"
define i32 @connect_server(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
%7 = alloca [128 x i8]
store i8* %0, i8** %3
%8 = load i32, i32* @PF_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 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
15: ; preds = %1
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i32 %19, i8* %20)
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 8)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %37
27: ; preds = %15
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%29 = call i32 (...) @getpid()
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%32 = call i32 @perror(i8* %31)
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %3
%36 = call i32 @unlink(i8* %35)
store i32 -1, i32* %2
br label %39
37: ; preds = %15
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %27, %13
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"client create socket failed\00"
@AF_UNIX = global i32 0
@.str.1 = [11 x i8] c"pid %d, %s\00"
@.str.2 = [26 x i8] c"connect to server failed!\00"
define i32 @connect_server(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca [128 x i8]
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %25
11: ; preds = %1
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = tail call i32 @strcpy(i32 undef, i8* %0)
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i32 @connect(i32 %7, %struct.sockaddr* %15, i32 8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%20 = call i32 (...) @getpid()
%21 = call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @perror(i8* %19)
%23 = call i32 @close(i32 %7)
%24 = call i32 @unlink(i8* %0)
br label %25
25: ; preds = %11, %18, %9
%26 = phi i32 [ -1, %9 ], [ -1, %18 ], [ %7, %11 ]
ret i32 %26
}