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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listen_on(char *port) {
  int s = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in sa;
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = INADDR_ANY;
  sa.sin_port = htons(atoi(port));
  int rc = bind(s, (struct sockaddr *)&sa, sizeof(sa));
  if (rc == -1) {
    perror("bind");
    exit(1);
  }
  rc = listen(s, 5);
  if (rc == -1) {
    perror("listen");
    exit(1);
  }
  return s;
}


x86 assembly (clang, -O0):
<listen_on>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f7 <listen_on+0x87>
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1040 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1227 <listen_on+0xb7>
lea    0xded(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_on>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e65(%rip),%r15
mov    (%r15),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     11ef <listen_on+0x7f>
mov    %ebx,%edi
mov    $0x5,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
je     11f8 <listen_on+0x88>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe0a(%rip),%rdi
jmp    11ff <listen_on+0x8f>
lea    0xe06(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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 = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
define i32 @listen_on(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i8* %0, i8** %2
%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* %3
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i8*, i8** %2
%15 = call i32 @atoi(i8* %14)
%16 = call i32 @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %3
%19 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%20 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 12)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %1
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %1
%27 = load i32, i32* %3
%28 = call i32 @listen(i32 %27, i32 5)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i32, i32* %3
ret i32 %35
}

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 = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
define i32 @listen_on(i8* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = tail call i32 @atoi(i8* %0)
%12 = tail call i32 @htons(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = call i32 @bind(i32 %5, %struct.sockaddr* %14, i32 12)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = call i32 @listen(i32 %5, i32 5)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %20
ret i32 %5
}