#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*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ ip;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socketBind(uint32_t ip, uint16_t port, int backlog) {
  int descriptor = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in address;
  if (descriptor < 0) {
    return -1;
  }
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  memcpy(&address.sin_addr, &ip, sizeof(ip));
  if (bind(descriptor, (struct sockaddr *)&address, sizeof(address)) != 0) {
    close(descriptor);
    return -1;
  }
  if (listen(descriptor, backlog) != 0) {
    close(descriptor);
    return -1;
  }
  return descriptor;
}


x86 assembly (clang, -O0):
<socketBind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a3 <socketBind+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <socketBind+0xca>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     11fb <socketBind+0x9b>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <socketBind+0xca>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     1224 <socketBind+0xc4>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <socketBind+0xca>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketBind>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,0xc(%rsp)
mov    0x2e6c(%rip),%r12
mov    (%r12),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     11f0 <socketBind+0x90>
mov    %eax,%ebx
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    11e9 <socketBind+0x89>
mov    %ebx,%edi
mov    %r14d,%esi
call   1060 <listen@plt>
test   %rax,%rax
je     1200 <socketBind+0xa0>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%r15d
jmp    11f0 <socketBind+0x90>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @socketBind(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %41
16: ; preds = %3
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* %6
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%23 = call i32 @memcpy(i32* %22, i32* %5, i32 4)
%24 = load i32, i32* %8
%25 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %16
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %4
br label %41
31: ; preds = %16
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = call i64 @listen(i32 %32, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %4
br label %41
39: ; preds = %31
%40 = load i32, i32* %8
store i32 %40, i32* %4
br label %41
41: ; preds = %39, %36, %28, %15
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @socketBind(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %4
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = icmp slt i32 %8, 0
br i1 %10, label %26, label %11
11: ; preds = %3
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%17 = call i32 @memcpy(i32* %16, i32* %4, i32 4)
%18 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%19 = call i64 @bind(i32 %8, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = call i64 @listen(i32 %8, i32 %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21, %11
%25 = call i32 @close(i32 %8)
br label %26
26: ; preds = %24, %21, %3
%27 = phi i32 [ -1, %3 ], [ %8, %21 ], [ -1, %24 ]
ret i32 %27
}