#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*/ yes;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int *calloc(int, int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ skclose(int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int *skbind(short port) {
  int *sd = NULL;
  int yes = 1;
  struct sockaddr_in addr;
  sd = calloc(sizeof(int), 1);
  if (sd == NULL) {
    ;
    return NULL;
  }
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if ((*sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    ;
    free(sd);
    return NULL;
  }
  if (setsockopt(*sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {

    ;
    skclose(sd);
    return NULL;
  }
  if (bind(*sd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1) {

    ;
    skclose(sd);
    return NULL;
  }
  return sd;
}


x86 assembly (clang, -O0):
<skbind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0xa(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    $0x4,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <skbind+0x49>
movq   $0x0,-0x8(%rbp)
jmp    12b0 <skbind+0x130>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movswl -0xa(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    -0x18(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    122f <skbind+0xaf>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b0 <skbind+0x130>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1275 <skbind+0xf5>
mov    -0x18(%rbp),%rdi
call   1080 <skclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b0 <skbind+0x130>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a8 <skbind+0x128>
mov    -0x18(%rbp),%rdi
call   1080 <skclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b0 <skbind+0x130>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skbind>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
movl   $0x1,0xc(%rsp)
mov    $0x4,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     1240 <skbind+0xc0>
mov    %rax,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e1d(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
movswl %bp,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    (%r14),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     1238 <skbind+0xb8>
mov    %eax,%ebp
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     122e <skbind+0xae>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1242 <skbind+0xc2>
mov    %rbx,%rdi
call   1080 <skclose@plt>
jmp    1240 <skbind+0xc0>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32* @skbind(i16 signext %0) {
%2 = alloca i32*
%3 = alloca i16
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i16 %0, i16* %3
store i32* null, i32** %4
store i32 1, i32* %5
%7 = call i32* @calloc(i32 4, i32 1)
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32* null, i32** %2
br label %47
11: ; preds = %1
%12 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 8)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i16, i16* %3
%16 = call i32 @htons(i16 signext %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = load i32*, i32** %4
store i32 %20, i32* %21
%22 = icmp eq i32 %20, -1
br i1 %22, label %23, label %26
23: ; preds = %11
%24 = load i32*, i32** %4
%25 = call i32 @free(i32* %24)
store i32* null, i32** %2
br label %47
26: ; preds = %11
%27 = load i32*, i32** %4
%28 = load i32, i32* %27
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEADDR
%31 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %5, i32 4)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32*, i32** %4
%35 = call i32 @skclose(i32* %34)
store i32* null, i32** %2
br label %47
36: ; preds = %26
%37 = load i32*, i32** %4
%38 = load i32, i32* %37
%39 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%40 = call i32 @bind(i32 %38, %struct.sockaddr* %39, i32 8)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = load i32*, i32** %4
%44 = call i32 @skclose(i32* %43)
store i32* null, i32** %2
br label %47
45: ; preds = %36
%46 = load i32*, i32** %4
store i32* %46, i32** %2
br label %47
47: ; preds = %45, %42, %33, %23, %10
%48 = load i32*, i32** %2
ret i32* %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32* @skbind(i16 signext %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = tail call i32* @calloc(i32 4, i32 1)
%7 = icmp eq i32* %6, null
br i1 %7, label %33, label %8
8: ; preds = %1
%9 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 8)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = call i32 @htons(i16 signext %0)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %8
%19 = call i32 @free(i32* %6)
br label %33
20: ; preds = %8
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %16, i32 %21, i32 %22, i32* %2, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @skclose(i32* %6)
br label %33
27: ; preds = %20
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @bind(i32 %16, %struct.sockaddr* %28, i32 8)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @skclose(i32* %6)
br label %33
33: ; preds = %27, %1, %31, %25, %18
%34 = phi i32* [ null, %18 ], [ null, %25 ], [ null, %31 ], [ null, %1 ], [ %6, %27 ]
ret i32* %34
}