#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  unsigned int s_addr;
};
struct sockaddr_in {
  unsigned short sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int listen(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listen_new(unsigned ip, unsigned short port) {
  struct sockaddr_in ser;
  int listen_fd, rv;
  ser.sin_family = AF_INET;
  ser.sin_addr.s_addr = ip;
  ser.sin_port = port;
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_fd == -1)
    return -1;
  rv = bind(listen_fd, (struct sockaddr *)&ser, sizeof(struct sockaddr_in));
  if (rv == -1)
    return -1;
  if (listen(listen_fd, 0) == -1) {
    close(listen_fd);
    return -1;
  }
  return listen_fd;
}


x86 assembly (clang, -O0):
<listen_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xa(%rbp),%ax
mov    %ax,-0x18(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    119e <listen_new+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <listen_new+0xb5>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c8 <listen_new+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <listen_new+0xb5>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
jne    11ef <listen_new+0xaf>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <listen_new+0xb5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_new>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %edi,0xc(%rsp)
mov    %si,0x8(%rsp)
mov    0x2e75(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11a9 <listen_new+0x69>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     11a9 <listen_new+0x69>
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
je     11a2 <listen_new+0x62>
mov    %ebp,%ebx
jmp    11a9 <listen_new+0x69>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

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