#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 sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
scalar_t__ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_aton(char *, int /*<<< orphan*/ *);
int listen(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strchr(char *, char);
char *strdup(char const *);
int /*<<< orphan*/ warn(char *);
int listen_at(const char *addr_and_port) {
  int s;
  char *buf, *p;
  struct sockaddr_in sin;
  assert(addr_and_port && "Missing mandatory argument");
  bzero(&sin, sizeof(sin));
  if ((buf = strdup(addr_and_port)) == NULL)
    err(1, "strdup");
  if ((p = strchr(buf, ':')) == NULL)
    return -1;
  *p++ = '\0';
  sin.sin_port = htons((in_port_t)atoi(p));
  if (inet_aton(buf, &sin.sin_addr) == -1) {
    free(buf);
    return -1;
  }
  free(buf);
  sin.sin_family = AF_INET;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    warn("socket");
    return -1;
  }
  if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    warn("bind");
    return -1;
  }
  if (listen(s, 0) == -1) {
    warn("listen");
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<listen_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x39(%rbp)
je     11f6 <listen_at+0x26>
mov    $0x1,%al
mov    %al,-0x39(%rbp)
jmp    11f6 <listen_at+0x26>
mov    -0x39(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    -0x10(%rbp),%rdi
call   10e0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1239 <listen_at+0x69>
mov    $0x1,%edi
lea    0xde7(%rip),%rsi
call   1090 <err@plt>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1261 <listen_at+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <listen_at+0x19a>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
add    $0x4,%rsi
call   1080 <inet_aton@plt>
cmp    $0xffffffff,%eax
jne    12b5 <listen_at+0xe5>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <listen_at+0x19a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1307 <listen_at+0x137>
lea    0xd2c(%rip),%rdi
call   1040 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <listen_at+0x19a>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1339 <listen_at+0x169>
lea    0xd01(%rip),%rdi
call   1040 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <listen_at+0x19a>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1364 <listen_at+0x194>
lea    0xcdb(%rip),%rdi
call   1040 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <listen_at+0x19a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_at>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1070 <assert@plt>
mov    %rsp,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    %rbx,%rdi
call   10e0 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1217 <listen_at+0x47>
lea    0xdf3(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     12d4 <listen_at+0x104>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <inet_aton@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebp
je     12d4 <listen_at+0x104>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    %edi,(%rsp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     12b9 <listen_at+0xe9>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     12c2 <listen_at+0xf2>
mov    %r14d,%edi
xor    %esi,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12d4 <listen_at+0x104>
lea    0xd5c(%rip),%rdi
jmp    12c9 <listen_at+0xf9>
lea    0xd47(%rip),%rdi
jmp    12c9 <listen_at+0xf9>
lea    0xd45(%rip),%rdi
call   1040 <warn@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"Missing mandatory argument\00"
@.str.1 = [7 x i8] c"strdup\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
define i32 @listen_at(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
br label %11
11: ; preds = %10, %1
%12 = phi i1 [ false, %1 ], [ true, %10 ]
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%16 = load i8*, i8** %3
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %5
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = load i8*, i8** %5
%23 = call i8* @strchr(i8* %22, i8 signext 58)
store i8* %23, i8** %6
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %2
br label %67
26: ; preds = %21
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %6
store i8 0, i8* %27
%29 = load i8*, i8** %6
%30 = call i64 @atoi(i8* %29)
%31 = trunc i64 %30 to i32
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %32, i32* %33
%34 = load i8*, i8** %5
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%36 = call i32 @inet_aton(i8* %34, i32* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %26
%39 = load i8*, i8** %5
%40 = call i32 @free(i8* %39)
store i32 -1, i32* %2
br label %67
41: ; preds = %26
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* @AF_INET
%47 = load i32, i32* @SOCK_STREAM
%48 = call i32 @socket(i32 %46, i32 %47, i32 0)
store i32 %48, i32* %4
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %41
%51 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %67
52: ; preds = %41
%53 = load i32, i32* %4
%54 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%55 = call i32 @bind(i32 %53, %struct.sockaddr* %54, i32 12)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @warn(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %67
59: ; preds = %52
%60 = load i32, i32* %4
%61 = call i32 @listen(i32 %60, i32 0)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %67
65: ; preds = %59
%66 = load i32, i32* %4
store i32 %66, i32* %2
br label %67
67: ; preds = %65, %63, %57, %50, %38, %25
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [7 x i8] c"strdup\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
define i32 @listen_at(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%8 = tail call i8* @strdup(i8* %0)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = tail call i8* @strchr(i8* dereferenceable(1) %8, i8 signext 58)
%14 = icmp eq i8* %13, null
br i1 %14, label %41, label %15
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %13, i64 1
store i8 0, i8* %13
%17 = tail call i64 @atoi(i8* %16)
%18 = trunc i64 %17 to i32
%19 = tail call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%22 = call i32 @inet_aton(i8* %8, i32* %21)
%23 = icmp eq i32 %22, -1
%24 = call i32 @free(i8* %8)
br i1 %23, label %41, label %25
25: ; preds = %15
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %26, i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %38, label %31
31: ; preds = %25
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i32 @bind(i32 %29, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = call i32 @listen(i32 %29, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %35, %31, %25
%39 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %25 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %31 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %35 ]
%40 = call i32 @warn(i8* %39)
br label %41
41: ; preds = %38, %15, %35, %12
%42 = phi i32 [ -1, %12 ], [ %29, %35 ], [ -1, %15 ], [ -1, %38 ]
ret i32 %42
}