#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sock_name;
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int set_nonblocking(int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_listen(int port, int flags) {
  struct sockaddr_in sock_name;
  int s, optval;
  sock_name.sin_family = AF_INET;
  if (flags & 2)
    sock_name.sin_addr.s_addr = htonl(0x7f000001);
  else
    sock_name.sin_addr.s_addr = INADDR_ANY;
  sock_name.sin_port = htons(port);
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == -1)
    return -1;
  optval = 1;
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
                 sizeof(optval)) == -1 ||
      bind(s, (struct sockaddr *)&sock_name, sizeof(sock_name)) == -1 ||
      listen(s, 10) == -1) {
    close(s);
    return -1;
  }
  if (flags & 1)
    return set_nonblocking(s);
  return s;
}


x86 assembly (clang, -O0):
<socket_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11bb <socket_listen+0x3b>
mov    $0x7f000001,%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
jmp    11c7 <socket_listen+0x47>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1204 <socket_listen+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <socket_listen+0x121>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1268 <socket_listen+0xe8>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1268 <socket_listen+0xe8>
mov    -0x1c(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    127c <socket_listen+0xfc>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <socket_listen+0x121>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     129b <socket_listen+0x11b>
mov    -0x1c(%rbp),%edi
call   1090 <set_nonblocking@plt>
mov    %eax,-0x4(%rbp)
jmp    12a1 <socket_listen+0x121>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_listen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2e4c(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
test   $0x2,%r14b
jne    11ac <socket_listen+0x2c>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
jmp    11b6 <socket_listen+0x36>
mov    $0x7f000001,%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x0(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     124d <socket_listen+0xcd>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1246 <socket_listen+0xc6>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1246 <socket_listen+0xc6>
mov    %ebp,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     1246 <socket_listen+0xc6>
test   $0x1,%r14b
jne    1258 <socket_listen+0xd8>
mov    %ebp,%ebx
jmp    124d <socket_listen+0xcd>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebp,%edi
call   1090 <set_nonblocking@plt>
mov    %eax,%ebx
jmp    124d <socket_listen+0xcd>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @socket_listen(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %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* %5
%12 = and i32 %11, 2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 @htonl(i32 2130706433)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
br label %22
18: ; preds = %2
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
br label %22
22: ; preds = %18, %14
%23 = load i32, i32* %4
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %22
store i32 -1, i32* %3
br label %60
32: ; preds = %22
store i32 1, i32* %8
%33 = load i32, i32* %7
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = bitcast i32* %8 to i8*
%37 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i8* %36, i32 4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %48, label %39
39: ; preds = %32
%40 = load i32, i32* %7
%41 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %48, label %44
44: ; preds = %39
%45 = load i32, i32* %7
%46 = call i32 @listen(i32 %45, i32 10)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %44, %39, %32
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %3
br label %60
51: ; preds = %44
%52 = load i32, i32* %5
%53 = and i32 %52, 1
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* %7
%57 = call i32 @set_nonblocking(i32 %56)
store i32 %57, i32* %3
br label %60
58: ; preds = %51
%59 = load i32, i32* %7
store i32 %59, i32* %3
br label %60
60: ; preds = %58, %55, %48, %31
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @socket_listen(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %7, i32* %8
%9 = and i32 %1, 2
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = tail call i32 @htonl(i32 2130706433)
br label %15
13: ; preds = %2
%14 = load i32, i32* @INADDR_ANY
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ %14, %13 ], [ %12, %11 ]
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 %0)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = tail call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %43, label %24
24: ; preds = %15
store i32 1, i32* %4
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %22, i32 %25, i32 %26, i8* %6, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %36, label %29
29: ; preds = %24
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i32 @bind(i32 %22, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = call i32 @listen(i32 %22, i32 10)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %33, %29, %24
%37 = call i32 @close(i32 %22)
br label %43
38: ; preds = %33
%39 = and i32 %1, 1
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 @set_nonblocking(i32 %22)
br label %43
43: ; preds = %38, %15, %41, %36
%44 = phi i32 [ -1, %36 ], [ %42, %41 ], [ -1, %15 ], [ %22, %38 ]
ret i32 %44
}