#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ accept(int, struct sockaddr *, int /*<<< orphan*/ *);
unsigned short atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned short);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void *listen_on_port(void *arg) {
  unsigned short port = atoi((char *)arg);
  char buf[1024];
  int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr))) {
    snprintf(buf, 1024, "Failed to bind port %u", port);
    perror(buf);
    return NULL;
  }
  if (listen(sockfd, 100)) {
    snprintf(buf, 1024, "Failed to listen on port %u", port);
    perror(buf);
    return NULL;
  }
  printf("listen on port %u\n", port);
  socklen_t len;
  while (accept(sockfd, (struct sockaddr *)&addr, &len) > 0) {
    printf("%u\n", port);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<listen_on_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %ax,-0x12(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x424(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x42c(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x430(%rbp)
mov    -0x424(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     126a <listen_on_port+0xca>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
lea    0xdb8(%rip),%rdx
movzwl -0x12(%rbp),%ecx
call   1050 <snprintf@plt>
lea    -0x420(%rbp),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1309 <listen_on_port+0x169>
mov    -0x424(%rbp),%edi
mov    $0x64,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
je     12b9 <listen_on_port+0x119>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
lea    0xd80(%rip),%rdx
movzwl -0x12(%rbp),%ecx
call   1050 <snprintf@plt>
lea    -0x420(%rbp),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1309 <listen_on_port+0x169>
lea    0xd73(%rip),%rdi
movzwl -0x12(%rbp),%esi
call   1040 <printf@plt>
mov    -0x424(%rbp),%edi
lea    -0x430(%rbp),%rsi
lea    -0x434(%rbp),%rdx
call   10a0 <accept@plt>
cmp    $0x0,%rax
jle    1301 <listen_on_port+0x161>
lea    0xd4f(%rip),%rdi
movzwl -0x12(%rbp),%esi
call   1040 <printf@plt>
jmp    12c9 <listen_on_port+0x129>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_on_port>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
call   10b0 <atoi@plt>
mov    %eax,%ebx
mov    0x2e2b(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,%r15d
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
movzwl %bx,%r14d
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jne    1290 <listen_on_port+0xf0>
mov    %r15d,%edi
mov    $0x64,%esi
call   1070 <listen@plt>
test   %rax,%rax
jne    1299 <listen_on_port+0xf9>
lea    0xe03(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
mov    %r15d,%edi
call   10a0 <accept@plt>
test   %rax,%rax
jle    127e <listen_on_port+0xde>
lea    0xdee(%rip),%r12
mov    %rsp,%rbp
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   10a0 <accept@plt>
test   %rax,%rax
jg     1260 <listen_on_port+0xc0>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd69(%rip),%rdx
jmp    12a0 <listen_on_port+0x100>
lea    0xd77(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1090 <perror@plt>
jmp    127e <listen_on_port+0xde>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@INADDR_ANY = global i32 0
@.str = [23 x i8] c"Failed to bind port %u\00"
@.str.1 = [28 x i8] c"Failed to listen on port %u\00"
@.str.2 = [19 x i8] c"listen on port %u\0A\00"
@.str.3 = [4 x i8] c"%u\0A\00"
define i8* @listen_on_port(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i16
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call zeroext i16 @atoi(i8* %9)
store i16 %10, i16* %4
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @IPPROTO_TCP
%14 = call i32 @socket(i32 %11, i32 %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i16, i16* %4
%18 = call i32 @htons(i16 zeroext %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %6
%25 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %1
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = load i16, i16* %4
%31 = call i32 @snprintf(i8* %29, i32 1024, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i16 zeroext %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = call i32 @perror(i8* %32)
store i8* null, i8** %2
br label %56
34: ; preds = %1
%35 = load i32, i32* %6
%36 = call i64 @listen(i32 %35, i32 100)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %34
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%40 = load i16, i16* %4
%41 = call i32 @snprintf(i8* %39, i32 1024, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i16 zeroext %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%43 = call i32 @perror(i8* %42)
store i8* null, i8** %2
br label %56
44: ; preds = %34
%45 = load i16, i16* %4
%46 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %45)
br label %47
47: ; preds = %52, %44
%48 = load i32, i32* %6
%49 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%50 = call i64 @accept(i32 %48, %struct.sockaddr* %49, i32* %8)
%51 = icmp sgt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i16, i16* %4
%54 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i16 zeroext %53)
br label %47
55: ; preds = %47
store i8* null, i8** %2
br label %56
56: ; preds = %55, %38, %28
%57 = load i8*, i8** %2
ret i8* %57
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@INADDR_ANY = global i32 0
@.str = [23 x i8] c"Failed to bind port %u\00"
@.str.1 = [28 x i8] c"Failed to listen on port %u\00"
@.str.2 = [19 x i8] c"listen on port %u\0A\00"
@.str.3 = [4 x i8] c"%u\0A\00"
define noalias i8* @listen_on_port(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = tail call zeroext i16 @atoi(i8* %0)
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @IPPROTO_TCP
%10 = tail call i32 @socket(i32 %7, i32 %8, i32 %9)
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i16 zeroext %5)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = tail call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i64 @bind(i32 %10, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %1
%23 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i16 zeroext %5)
%24 = call i32 @perror(i8* %6)
br label %41
25: ; preds = %1
%26 = call i64 @listen(i32 %10, i32 100)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i16 zeroext %5)
%30 = call i32 @perror(i8* %6)
br label %41
31: ; preds = %25
%32 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %5)
%33 = bitcast i32* %4 to i8*
%34 = call i64 @accept(i32 %10, %struct.sockaddr* %19, i32* %4)
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %31, %36
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i16 zeroext %5)
%38 = call i64 @accept(i32 %10, %struct.sockaddr* %19, i32* %4)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %36, label %40
40: ; preds = %36, %31
br label %41
41: ; preds = %40, %28, %22
ret i8* null
}