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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SUN_LEN(struct sockaddr_un *);
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(char *);
int open_configure_unix_socket() {
  struct sockaddr_un sock_server_add;
  memset(&sock_server_add, 0x00, sizeof(sock_server_add));
  int unix_sock = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (unix_sock < 0) {
    perror("DB_USB: Error opening datagram socket");
    exit(-1);
  }
  sock_server_add.sun_family = AF_UNIX;
  strcpy(sock_server_add.sun_path, "/tmp/db_video_out");
  unlink("/tmp/db_video_out");
  if (bind(unix_sock, (struct sockaddr *)&sock_server_add,
           SUN_LEN(&sock_server_add)) < 0) {
    perror("DB_USB: Error binding name to datagram socket");
    close(unix_sock);
    exit(-1);
  }
  printf("Opened UNIX socket\n");
  return unix_sock;
}


x86 assembly (clang, -O0):
<open_configure_unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11f4 <open_configure_unix_socket+0x54>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe1c(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xe10(%rip),%rdi
call   1030 <unlink@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x8(%rbp),%rdi
call   10b0 <SUN_LEN@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1268 <open_configure_unix_socket+0xc8>
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xdf7(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_configure_unix_socket>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e2e(%rip),%r14
mov    (%r14),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1226 <open_configure_unix_socket+0x86>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
lea    0xe42(%rip),%r14
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   10b0 <SUN_LEN@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     123c <open_configure_unix_socket+0x9c>
lea    0xe4f(%rip),%rdi
call   1050 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd3(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [38 x i8] c"DB_USB: Error opening datagram socket\00"
@.str.1 = [18 x i8] c"/tmp/db_video_out\00"
@.str.2 = [46 x i8] c"DB_USB: Error binding name to datagram socket\00"
@.str.3 = [20 x i8] c"Opened UNIX socket\0A\00"
define i32 @open_configure_unix_socket() {
%1 = alloca %struct.sockaddr_un
%2 = alloca i32
%3 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 8)
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_DGRAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 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* %1, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%21 = call i32 @SUN_LEN(%struct.sockaddr_un* %1)
%22 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %12
%25 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %12
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [38 x i8] c"DB_USB: Error opening datagram socket\00"
@.str.1 = [18 x i8] c"/tmp/db_video_out\00"
@.str.2 = [46 x i8] c"DB_USB: Error binding name to datagram socket\00"
@str = [19 x i8] c"Opened UNIX socket\00"
define i32 @open_configure_unix_socket() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 8)
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_DGRAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i32 @SUN_LEN(%struct.sockaddr_un* %1)
%20 = call i64 @bind(i32 %6, %struct.sockaddr* %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %11
%23 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @close(i32 %6)
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %11
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
ret i32 %6
}