#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;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ bind(int, struct sockaddr *, int);
int get_sockaddr_un_size(struct sockaddr_un *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
bool bind_unix_stream_socket(int sock, struct sockaddr_un *sock_st) {
  int len;
  if (sock < 0 || !sock_st) {
    printf("Error - invalid parameters to bind_unix_stream_socket\n");
    return false;
  }
  len = get_sockaddr_un_size(sock_st);
  unlink(sock_st->sun_path);
  if (bind(sock, (struct sockaddr *)sock_st, len) < 0) {
    perror("Error - bind_unix_stream_socket() - bind::");
    return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<bind_unix_stream_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1174 <bind_unix_stream_socket+0x24>
cmpq   $0x0,-0x10(%rbp)
jne    118c <bind_unix_stream_socket+0x3c>
lea    0xe85(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11db <bind_unix_stream_socket+0x8b>
mov    -0x10(%rbp),%rdi
call   1070 <get_sockaddr_un_size@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11d4 <bind_unix_stream_socket+0x84>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11db <bind_unix_stream_socket+0x8b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_unix_stream_socket>:
push   %rbp
push   %r14
push   %rbx
test   %edi,%edi
js     119c <bind_unix_stream_socket+0x4c>
mov    %rsi,%rbx
test   %rsi,%rsi
je     119c <bind_unix_stream_socket+0x4c>
mov    %edi,%ebp
mov    %rbx,%rdi
call   1070 <get_sockaddr_un_size@plt>
mov    %eax,%r14d
mov    (%rbx),%edi
call   1030 <unlink@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <bind@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jns    11aa <bind_unix_stream_socket+0x5a>
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
jmp    11a8 <bind_unix_stream_socket+0x58>
lea    0xe88(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [55 x i8] c"Error - invalid parameters to bind_unix_stream_socket\0A\00"
@.str.1 = [43 x i8] c"Error - bind_unix_stream_socket() - bind::\00"
define i32 @bind_unix_stream_socket(i32 %0, %struct.sockaddr_un* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un*
%6 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_un* %1, %struct.sockaddr_un** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%11 = icmp ne %struct.sockaddr_un* %10, null
br i1 %11, label %14, label %12
12: ; preds = %9, %2
%13 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %30
14: ; preds = %9
%15 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%16 = call i32 @get_sockaddr_un_size(%struct.sockaddr_un* %15)
store i32 %16, i32* %6
%17 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @unlink(i32 %19)
%21 = load i32, i32* %4
%22 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%23 = bitcast %struct.sockaddr_un* %22 to %struct.sockaddr*
%24 = load i32, i32* %6
%25 = call i64 @bind(i32 %21, %struct.sockaddr* %23, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %30
29: ; preds = %14
store i32 1, i32* %3
br label %30
30: ; preds = %29, %27, %12
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [43 x i8] c"Error - bind_unix_stream_socket() - bind::\00"
@str = [54 x i8] c"Error - invalid parameters to bind_unix_stream_socket\00"
define i32 @bind_unix_stream_socket(i32 %0, %struct.sockaddr_un* %1) {
%3 = icmp sgt i32 %0, -1
%4 = icmp ne %struct.sockaddr_un* %1, null
%5 = and i1 %3, %4
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
br label %18
8: ; preds = %2
%9 = tail call i32 @get_sockaddr_un_size(%struct.sockaddr_un* %1)
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @unlink(i32 %11)
%13 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%14 = tail call i64 @bind(i32 %0, %struct.sockaddr* %13, i32 %9)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %8, %16, %6
%19 = phi i32 [ 0, %16 ], [ 0, %6 ], [ 1, %8 ]
ret i32 %19
}