#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*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int create_socket(char *name) {
  struct sockaddr_un addr;
  int len, sock;
  int tries;
  sock = socket(PF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("function creating socket");
    return -1;
  }
  strcpy(addr.sun_path, name);
  addr.sun_family = AF_UNIX;
  len = strlen(addr.sun_path) + sizeof(addr.sun_family);
  for (tries = 0; tries < 5; tries++) {
    if (connect(sock, (struct sockaddr *)&addr, len) < 0) {
      perror("External Function Error: function connect failed");
    } else {
      return sock;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    119a <create_socket+0x4a>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <create_socket+0xd3>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x5,-0x24(%rbp)
jge    121c <create_socket+0xcc>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    11fe <create_socket+0xae>
lea    0xe25(%rip),%rdi
call   1050 <perror@plt>
jmp    1209 <create_socket+0xb9>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1223 <create_socket+0xd3>
jmp    120e <create_socket+0xbe>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ca <create_socket+0x7a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11ae <create_socket+0x5e>
mov    %eax,%ebx
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
call   1040 <strlen@plt>
mov    %eax,%ebp
add    $0x4,%ebp
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     11c1 <create_socket+0x71>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11a5 <create_socket+0x55>
lea    0xe51(%rip),%rdi
call   1050 <perror@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11a5 <create_socket+0x55>
lea    0xe34(%rip),%rdi
call   1050 <perror@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11a5 <create_socket+0x55>
lea    0xe17(%rip),%rdi
call   1050 <perror@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11a5 <create_socket+0x55>
lea    0xdfa(%rip),%rdi
call   1050 <perror@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11a5 <create_socket+0x55>
lea    0xdd9(%rip),%rdi
jmp    11b5 <create_socket+0x65>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [25 x i8] c"function creating socket\00"
@AF_UNIX = global i32 0
@.str.1 = [49 x i8] c"External Function Error: function connect failed\00"
define i32 @create_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
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* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %46
15: ; preds = %1
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @strcpy(i32 %17, i8* %18)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @strlen(i32 %23)
%25 = sext i32 %24 to i64
%26 = add i64 %25, 4
%27 = trunc i64 %26 to i32
store i32 %27, i32* %5
store i32 0, i32* %7
br label %28
28: ; preds = %42, %15
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 5
br i1 %30, label %31, label %45
31: ; preds = %28
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%34 = load i32, i32* %5
%35 = call i64 @connect(i32 %32, %struct.sockaddr* %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %41
39: ; preds = %31
%40 = load i32, i32* %6
store i32 %40, i32* %2
br label %46
41: ; preds = %37
br label %42
42: ; preds = %41
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
br label %28
45: ; preds = %28
store i32 -1, i32* %2
br label %46
46: ; preds = %45, %39, %13
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [25 x i8] c"function creating socket\00"
@AF_UNIX = global i32 0
@.str.1 = [49 x i8] c"External Function Error: function connect failed\00"
define i32 @create_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @PF_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 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %37
10: ; preds = %1
%11 = tail call i32 @strcpy(i32 undef, i8* %0)
%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 @strlen(i32 undef)
%15 = add i32 %14, 4
%16 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%17 = call i64 @connect(i32 %6, %struct.sockaddr* %16, i32 %15)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%21 = call i64 @connect(i32 %6, %struct.sockaddr* %16, i32 %15)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%25 = call i64 @connect(i32 %6, %struct.sockaddr* %16, i32 %15)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%29 = call i64 @connect(i32 %6, %struct.sockaddr* %16, i32 %15)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%33 = call i64 @connect(i32 %6, %struct.sockaddr* %16, i32 %15)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %10, %19, %23, %27, %31, %35, %8
%38 = phi i32 [ -1, %8 ], [ %6, %10 ], [ %6, %19 ], [ %6, %23 ], [ %6, %27 ], [ %6, %31 ], [ -1, %35 ]
ret i32 %38
}