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

/* Type definitions */
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
scalar_t__ listen(int, int);
int socket_listen(int sockfd, char *port) {
  int status = 0;
  struct sockaddr_in address;
  struct in_addr in_address;
  address.sin_family = AF_INET;
  address.sin_port = htons(atoi(port));
  inet_aton("0.0.0.0", &in_address.s_addr);
  address.sin_addr = in_address;
  if ((status = bind(sockfd, (struct sockaddr *)&address,
                     sizeof(struct sockaddr_in))) < 0) {
    return -1;
  }
  if (listen(sockfd, 1024) < 0) {
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<socket_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x28(%rbp),%rsi
lea    0xe70(%rip),%rdi
call   1040 <inet_aton@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11c4 <socket_listen+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <socket_listen+0x9d>
mov    -0x8(%rbp),%edi
mov    $0x400,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    11e7 <socket_listen+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <socket_listen+0x9d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_listen>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsi,%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xe82(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <inet_aton@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %eax,%eax
js     11be <socket_listen+0x6e>
mov    %ebx,%edi
mov    $0x400,%esi
call   1050 <listen@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    11c3 <socket_listen+0x73>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
define i32 @socket_listen(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.in_addr
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i8*, i8** %5
%12 = call i32 @atoi(i8* %11)
%13 = call i32 @htons(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %8, i32 0, i32 0
%16 = call i32 @inet_aton(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32* %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%18 = bitcast %struct.in_addr* %17 to i8*
%19 = bitcast %struct.in_addr* %8 to i8*
%20 = load i32, i32* %4
%21 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 -1, i32* %3
br label %32
25: ; preds = %2
%26 = load i32, i32* %4
%27 = call i64 @listen(i32 %26, i32 1024)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %3
br label %32
30: ; preds = %25
%31 = load i32, i32* %4
store i32 %31, i32* %3
br label %32
32: ; preds = %30, %29, %24
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
define i32 @socket_listen(i32 %0, i8* nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.in_addr
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast %struct.in_addr* %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 = tail call i32 @atoi(i8* %1)
%10 = tail call i32 @htons(i32 %9)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
%13 = call i32 @inet_aton(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32* %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%15 = load i32, i32* %12
store i32 %15, i32* %14
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i32 @bind(i32 %0, %struct.sockaddr* %16, i32 12)
%18 = icmp slt i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %2
%20 = call i64 @listen(i32 %0, i32 1024)
%21 = icmp slt i64 %20, 0
%22 = select i1 %21, i32 -1, i32 %0
br label %23
23: ; preds = %19, %2
%24 = phi i32 [ -1, %2 ], [ %22, %19 ]
ret i32 %24
}