#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 /*<<< orphan*/ tmpaddr;
struct sockaddr_storage {
  int ss_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int bind_socket(int af, int sk, struct sockaddr_storage *src) {
  if (!src)
    return -1;
  struct sockaddr_storage tmpaddr;
  if (!src->ss_family) {
    memset(&tmpaddr, 0, sizeof(tmpaddr));
    tmpaddr.ss_family = af;
    src = &tmpaddr;
  }
  if (bind(sk, (struct sockaddr *)src, sizeof(*src)) == -1)
    do {
      perror("bind");
      return -1;
    } while (0);
  return 0;
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1159 <bind_socket+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <bind_socket+0x92>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1184 <bind_socket+0x54>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <bind@plt>
cmp    $0xffffffff,%eax
jne    11bb <bind_socket+0x8b>
jmp    11a3 <bind_socket+0x73>
lea    0xe56(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <bind_socket+0x92>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
test   %rdx,%rdx
je     1184 <bind_socket+0x54>
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
cmpl   $0x0,(%rdx)
jne    1163 <bind_socket+0x33>
mov    %edi,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %r14d,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <bind@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     118a <bind_socket+0x5a>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%eax
ret
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    117b <bind_socket+0x4b>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"bind\00"
define i32 @bind_socket(i32 %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.sockaddr_storage
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
%9 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%10 = icmp ne %struct.sockaddr_storage* %9, null
br i1 %10, label %12, label %11
11: ; preds = %3
store i32 -1, i32* %4
br label %32
12: ; preds = %3
%13 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%14 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = call i32 @memset(%struct.sockaddr_storage* %8, i32 0, i32 4)
%19 = load i32, i32* %5
%20 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %8, i32 0, i32 0
store i32 %19, i32* %20
store %struct.sockaddr_storage* %8, %struct.sockaddr_storage** %7
br label %21
21: ; preds = %17, %12
%22 = load i32, i32* %6
%23 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%24 = bitcast %struct.sockaddr_storage* %23 to %struct.sockaddr*
%25 = call i32 @bind(i32 %22, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %21
br label %28
28: ; preds = %27
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %32
30: ; No predecessors!
br label %31
31: ; preds = %30, %21
store i32 0, i32* %4
br label %32
32: ; preds = %31, %28, %11
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"bind\00"
define i32 @bind_socket(i32 %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca %struct.sockaddr_storage
%5 = icmp eq %struct.sockaddr_storage* %2, null
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = bitcast %struct.sockaddr_storage* %4 to i8*
%8 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = call i32 @memset(%struct.sockaddr_storage* %4, i32 0, i32 4)
%13 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
store i32 %0, i32* %13
br label %14
14: ; preds = %11, %6
%15 = phi %struct.sockaddr_storage* [ %2, %6 ], [ %4, %11 ]
%16 = bitcast %struct.sockaddr_storage* %15 to %struct.sockaddr*
%17 = call i32 @bind(i32 %1, %struct.sockaddr* %16, i32 4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %14, %19
%22 = phi i32 [ -1, %19 ], [ 0, %14 ]
br label %23
23: ; preds = %3, %21
%24 = phi i32 [ %22, %21 ], [ -1, %3 ]
ret i32 %24
}