#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 uint;
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;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
uint create_socket() {
  int s, len;
  struct sockaddr_un remote;
  s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s == -1) {
    perror("Failed to create socket");
    exit(-1);
  }
  remote.sun_family = AF_UNIX;
  strcpy(remote.sun_path, "/tmp/chat_socket");
  len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  if (connect(s, (struct sockaddr *)&remote, len) == -1) {
    perror("Connect failed");
    exit(-2);
  }
  return s;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11a4 <create_socket+0x44>
lea    0xe6b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe5e(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%edi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    11fe <create_socket+0x9e>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xfffffffe,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%r14
mov    (%r14),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11b9 <create_socket+0x59>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xe85(%rip),%rsi
call   1030 <strcpy@plt>
call   1040 <strlen@plt>
lea    0x4(%rax),%edx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11cf <create_socket+0x6f>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe40(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
lea    0xe53(%rip),%rdi
call   1050 <perror@plt>
mov    $0xfffffffe,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@.str.1 = [17 x i8] c"/tmp/chat_socket\00"
@.str.2 = [15 x i8] c"Connect failed\00"
define i32 @create_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strlen(i32 %19)
%21 = sext i32 %20 to i64
%22 = add i64 %21, 4
%23 = trunc i64 %22 to i32
store i32 %23, i32* %2
%24 = load i32, i32* %1
%25 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%26 = load i32, i32* %2
%27 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %12
%30 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 -2)
unreachable
32: ; preds = %12
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@.str.1 = [17 x i8] c"/tmp/chat_socket\00"
@.str.2 = [15 x i8] c"Connect failed\00"
define i32 @create_socket() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %11, i32* %12
%13 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @strlen(i32 undef)
%15 = add i32 %14, 4
%16 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%17 = call i32 @connect(i32 %5, %struct.sockaddr* %16, i32 %15)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 -2)
unreachable
22: ; preds = %10
ret i32 %5
}