#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*/ socklen_t;
typedef int /*<<< orphan*/ server_address;
typedef int /*<<< orphan*/ client_address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sockerror(char *);
int server_connect(int sockfdes, int portnum) {
  int client_len, newsockfdes;
  struct sockaddr_in server_address, client_address;
  memset((char *)&server_address, 0, sizeof(server_address));
  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = INADDR_ANY;
  server_address.sin_port = htons(portnum);
  if (bind(sockfdes, (struct sockaddr *)&server_address,
           sizeof(server_address)) < 0)
    sockerror("ERROR, while binding!!!");
  printf("listening for connection\n");
  listen(sockfdes, 10);
  client_len = sizeof(client_address);
  newsockfdes = accept(sockfdes, (struct sockaddr *)&client_address,
                       (socklen_t *)&client_len);
  if (newsockfdes < 0)
    sockerror("ERROR, during accept!!!");
  else
    printf("Connection Initiated!!! \n");
  return newsockfdes;
}


x86 assembly (clang, -O0):
<server_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    11d8 <server_connect+0x68>
lea    0xe2d(%rip),%rdi
call   1090 <sockerror@plt>
lea    0xe39(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
movl   $0xc,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1226 <server_connect+0xb6>
lea    0xe16(%rip),%rdi
call   1090 <sockerror@plt>
jmp    1232 <server_connect+0xc2>
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    11d5 <server_connect+0x65>
lea    0xe30(%rip),%rdi
call   1090 <sockerror@plt>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1080 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
js     121a <server_connect+0xaa>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
jmp    1226 <server_connect+0xb6>
lea    0xdf7(%rip),%rdi
call   1090 <sockerror@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
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
@INADDR_ANY = global i32 0
@.str = [24 x i8] c"ERROR, while binding!!!\00"
@.str.1 = [26 x i8] c"listening for connection\0A\00"
@.str.2 = [24 x i8] c"ERROR, during accept!!!\00"
@.str.3 = [26 x i8] c"Connection Initiated!!! \0A\00"
define i32 @server_connect(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = bitcast %struct.sockaddr_in* %7 to i8*
%10 = call i32 @memset(i8* %9, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* %4
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %3
%20 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @sockerror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %2
%26 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %3
%28 = call i32 @listen(i32 %27, i32 10)
store i32 12, i32* %5
%29 = load i32, i32* %3
%30 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%31 = call i32 @accept(i32 %29, %struct.sockaddr* %30, i32* %5)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @sockerror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %38
36: ; preds = %25
%37 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %34
%39 = load i32, i32* %6
ret i32 %39
}

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
@.str = [24 x i8] c"ERROR, while binding!!!\00"
@.str.2 = [24 x i8] c"ERROR, during accept!!!\00"
@str = [25 x i8] c"listening for connection\00"
@str.4 = [25 x i8] c"Connection Initiated!!! \00"
define i32 @server_connect(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = call i32 @memset(i8* %7, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%17 = call i64 @bind(i32 %0, %struct.sockaddr* %16, i32 12)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @sockerror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%23 = call i32 @listen(i32 %0, i32 10)
store i32 12, i32* %3
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = call i32 @accept(i32 %0, %struct.sockaddr* %24, i32* %3)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @sockerror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %31
29: ; preds = %21
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %27
ret i32 %25
}