#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*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listen_server(void) {
  int listenfd = 0, connfd = 0, ret = -1;
  struct sockaddr_in serv_addr;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&serv_addr, '0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(5000);
  ret = bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (ret != 0) {
    printf("\nbind failed\n");
    return -1;
  }
  ret = listen(listenfd, 10);
  if (ret != 0) {
    printf("\nlisten failed\n");
    return -1;
  }
  while (connfd == 0) {
    connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);
    sleep(1);
  }
  close(listenfd);
  return connfd;
}


x86 assembly (clang, -O0):
<listen_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1388,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     124c <listen_server+0xac>
lea    0xdc5(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <listen_server+0x11c>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     127e <listen_server+0xde>
lea    0xda1(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <listen_server+0x11c>
jmp    1283 <listen_server+0xe3>
cmpl   $0x0,-0xc(%rbp)
jne    12ae <listen_server+0x10e>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10a0 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1283 <listen_server+0xe3>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e39(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x1388,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
je     121b <listen_server+0x7b>
lea    0xdf6(%rip),%rdi
jmp    1232 <listen_server+0x92>
mov    %ebx,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
test   %eax,%eax
je     1250 <listen_server+0xb0>
lea    0xdce(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <accept@plt>
mov    %eax,%ebp
mov    $0x1,%edi
call   10b0 <sleep@plt>
test   %ebp,%ebp
je     1250 <listen_server+0xb0>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    123c <listen_server+0x9c>

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
@INADDR_ANY = global i32 0
@.str = [14 x i8] c"\0Abind failed\0A\00"
@.str.1 = [16 x i8] c"\0Alisten failed\0A\00"
define i32 @listen_server() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 0, i32* %2
store i32 0, i32* %3
store i32 -1, i32* %4
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = call i32 @memset(%struct.sockaddr_in* %5, i32 48, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = call i32 @htons(i32 5000)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %2
%19 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%20 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 12)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %0
%24 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
25: ; preds = %0
%26 = load i32, i32* %2
%27 = call i32 @listen(i32 %26, i32 10)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
32: ; preds = %25
br label %33
33: ; preds = %36, %32
%34 = load i32, i32* %3
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* %2
%38 = call i32 @accept(i32 %37, %struct.sockaddr* null, i32* null)
store i32 %38, i32* %3
%39 = call i32 @sleep(i32 1)
br label %33
40: ; preds = %33
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %3
store i32 %43, i32* %1
br label %44
44: ; preds = %40, %30, %23
%45 = load i32, i32* %1
ret i32 %45
}

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
@INADDR_ANY = global i32 0
@str = [15 x i8] c"\0Alisten failed\00"
@str.2 = [13 x i8] c"\0Abind failed\00"
define i32 @listen_server() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = call i32 @memset(%struct.sockaddr_in* %1, i32 48, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = call i32 @htonl(i32 %9)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %10, i32* %11
%12 = call i32 @htons(i32 5000)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%15 = call i32 @bind(i32 %5, %struct.sockaddr* %14, i32 12)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %0
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0))
br label %30
19: ; preds = %0
%20 = call i32 @listen(i32 %5, i32 10)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %30
24: ; preds = %19, %24
%25 = call i32 @accept(i32 %5, %struct.sockaddr* null, i32* null)
%26 = call i32 @sleep(i32 1)
%27 = icmp eq i32 %25, 0
br i1 %27, label %24, label %28
28: ; preds = %24
%29 = call i32 @close(i32 %5)
br label %30
30: ; preds = %28, %22, %17
%31 = phi i32 [ -1, %17 ], [ -1, %22 ], [ %25, %28 ]
ret i32 %31
}