#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*/ serveraddr;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int O_NONBLOCK;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int server_t(const char *ipaddr, int port, int blocking) {
  int server_sock = 0;
  int on = 1;
  int stat = 0;
  struct sockaddr_in serveraddr;
  server_sock = socket(AF_INET, SOCK_STREAM, 0);
  if (server_sock < 0) {
    return -1;
  }
  {
    on = 1;
    setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  }
  if (!blocking) {
    stat = fcntl(server_sock, F_GETFL, NULL);
    stat |= O_NONBLOCK;
    fcntl(server_sock, F_SETFL, stat);
  }
  memset(&serveraddr, 0, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  if (!ipaddr || !ipaddr[0]) {
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  } else {
    serveraddr.sin_addr.s_addr = inet_addr(ipaddr);
  }
  serveraddr.sin_port = htons(port);
  if (bind(server_sock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) <
      0) {
    close(server_sock);
    return -1;
  }
  if (listen(server_sock, 5) < 0) {
    close(server_sock);
    return -1;
  }
  signal(SIGPIPE, SIG_IGN);
  return server_sock;
}


x86 assembly (clang, -O0):
<server_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1209 <server_t+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <server_t+0x19f>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1277 <server_t+0xc7>
mov    -0x1c(%rbp),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1040 <fcntl@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12ab <server_t+0xfb>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    12c1 <server_t+0x111>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x2c(%rbp)
jmp    12cd <server_t+0x11d>
mov    -0x10(%rbp),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    1307 <server_t+0x157>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <server_t+0x19f>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
jge    1332 <server_t+0x182>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <server_t+0x19f>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
mov    0x2c86(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_t>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%r12
mov    0x2e13(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     12e5 <server_t+0x135>
mov    %eax,%ebp
movl   $0x1,0x4(%rsp)
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %ebx,%ebx
jne    124e <server_t+0x9e>
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2d9e(%rip),%rcx
or     (%rcx),%eax
mov    0x2d75(%rip),%rcx
mov    (%rcx),%esi
mov    %ebp,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
test   %r12,%r12
je     127d <server_t+0xcd>
cmpb   $0x0,(%r12)
je     127d <server_t+0xcd>
mov    %r12,%rdi
call   1090 <inet_addr@plt>
jmp    128b <server_t+0xdb>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     12de <server_t+0x12e>
mov    %ebp,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
test   %rax,%rax
js     12de <server_t+0x12e>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
mov    0x2cf6(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
mov    %ebp,%r15d
jmp    12e5 <server_t+0x135>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@INADDR_ANY = global i32 0
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
define i32 @server_t(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 1, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %80
18: ; preds = %3
store i32 1, i32* %9
%19 = load i32, i32* %8
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, i32* %9, i32 4)
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %36, label %25
25: ; preds = %18
%26 = load i32, i32* %8
%27 = load i32, i32* @F_GETFL
%28 = call i32 @fcntl(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %10
%29 = load i32, i32* @O_NONBLOCK
%30 = load i32, i32* %10
%31 = or i32 %30, %29
store i32 %31, i32* %10
%32 = load i32, i32* %8
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* %10
%35 = call i32 @fcntl(i32 %32, i32 %33, i32 %34)
br label %36
36: ; preds = %25, %18
%37 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %38, i32* %39
%40 = load i8*, i8** %5
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8, i8* %44
%46 = icmp ne i8 %45, 0
br i1 %46, label %52, label %47
47: ; preds = %42, %36
%48 = load i32, i32* @INADDR_ANY
%49 = call i32 @htonl(i32 %48)
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
store i32 %49, i32* %51
br label %57
52: ; preds = %42
%53 = load i8*, i8** %5
%54 = call i32 @inet_addr(i8* %53)
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
store i32 %54, i32* %56
br label %57
57: ; preds = %52, %47
%58 = load i32, i32* %6
%59 = call i32 @htons(i32 %58)
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %59, i32* %60
%61 = load i32, i32* %8
%62 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%63 = call i64 @bind(i32 %61, %struct.sockaddr* %62, i32 12)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %57
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %4
br label %80
68: ; preds = %57
%69 = load i32, i32* %8
%70 = call i64 @listen(i32 %69, i32 5)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %68
%73 = load i32, i32* %8
%74 = call i32 @close(i32 %73)
store i32 -1, i32* %4
br label %80
75: ; preds = %68
%76 = load i32, i32* @SIGPIPE
%77 = load i32, i32* @SIG_IGN
%78 = call i32 @signal(i32 %76, i32 %77)
%79 = load i32, i32* %8
store i32 %79, i32* %4
br label %80
80: ; preds = %75, %72, %65, %17
%81 = load i32, i32* %4
ret i32 %81
}

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
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@INADDR_ANY = global i32 0
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
define i32 @server_t(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %56, label %12
12: ; preds = %3
store i32 1, i32* %4
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %10, i32 %13, i32 %14, i32* %4, i32 4)
%16 = icmp eq i32 %2, 0
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = load i32, i32* @F_GETFL
%19 = call i32 @fcntl(i32 %10, i32 %18, i32 0)
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %20, %19
%22 = load i32, i32* @F_SETFL
%23 = call i32 @fcntl(i32 %10, i32 %22, i32 %21)
br label %24
24: ; preds = %17, %12
%25 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %26, i32* %27
%28 = icmp eq i8* %0, null
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = load i8, i8* %0
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29, %24
%33 = load i32, i32* @INADDR_ANY
%34 = call i32 @htonl(i32 %33)
br label %37
35: ; preds = %29
%36 = call i32 @inet_addr(i8* %0)
br label %37
37: ; preds = %35, %32
%38 = phi i32 [ %36, %35 ], [ %34, %32 ]
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %38, i32* %39
%40 = call i32 @htons(i32 %1)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %40, i32* %41
%42 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%43 = call i64 @bind(i32 %10, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 @close(i32 %10)
br label %56
47: ; preds = %37
%48 = call i64 @listen(i32 %10, i32 5)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @close(i32 %10)
br label %56
52: ; preds = %47
%53 = load i32, i32* @SIGPIPE
%54 = load i32, i32* @SIG_IGN
%55 = call i32 @signal(i32 %53, i32 %54)
br label %56
56: ; preds = %3, %52, %50, %45
%57 = phi i32 [ -1, %45 ], [ -1, %50 ], [ %10, %52 ], [ -1, %3 ]
ret i32 %57
}