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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
unsigned short ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int startup(unsigned short *port) {
  int sockfd;
  int on;
  struct sockaddr_in servaddr;
  socklen_t len;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket() failed");
    return -1;
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(*port);
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  on = 1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
    perror("setsockopt() failed");
    close(sockfd);
    return -1;
  }
  if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
    perror("bind() failed");
    close(sockfd);
    return -1;
  }
  if (*port == 0) {
    len = sizeof(servaddr);
    if (getsockname(sockfd, (struct sockaddr *)&servaddr, &len) == -1) {
      perror("getsockname() failed");
      close(sockfd);
      return -1;
    }
    *port = ntohs(servaddr.sin_port);
  }
  if (listen(sockfd, 128) == -1) {
    perror("listen() failed");
    close(sockfd);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<startup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fa <startup+0x4a>
lea    0xe17(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <startup+0x1b2>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movzwl (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    128a <startup+0xda>
lea    0xd9f(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <startup+0x1b2>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12c4 <startup+0x114>
lea    0xd79(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <startup+0x1b2>
mov    -0x10(%rbp),%rax
movzwl (%rax),%eax
cmp    $0x0,%eax
jne    1326 <startup+0x176>
movl   $0xc,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10c0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    1314 <startup+0x164>
lea    0xd37(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <startup+0x1b2>
mov    -0x28(%rbp),%edi
call   1090 <ntohs@plt>
mov    %ax,%cx
mov    -0x10(%rbp),%rax
mov    %cx,(%rax)
mov    -0x14(%rbp),%edi
mov    $0x80,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    135c <startup+0x1ac>
lea    0xd04(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <startup+0x1b2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startup>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    0x2e1d(%rip),%r15
mov    (%r15),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12ab <startup+0xfb>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
movzwl (%r14),%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
movl   $0x1,0x1c(%rsp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
lea    0x1c(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12b9 <startup+0x109>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12c2 <startup+0x112>
cmpw   $0x0,(%r14)
jne    1291 <startup+0xe1>
movl   $0xc,0x18(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <getsockname@plt>
cmp    $0xffffffff,%eax
je     12cb <startup+0x11b>
mov    0x8(%rsp),%edi
call   1090 <ntohs@plt>
mov    %ax,(%r14)
mov    %ebx,%edi
mov    $0x80,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e3 <startup+0x133>
lea    0xd9e(%rip),%rdi
jmp    12d2 <startup+0x122>
lea    0xd4e(%rip),%rdi
call   10b0 <perror@plt>
jmp    12de <startup+0x12e>
lea    0xd50(%rip),%rdi
jmp    12d2 <startup+0x122>
lea    0xd5b(%rip),%rdi
jmp    12d2 <startup+0x122>
lea    0xd60(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [20 x i8] c"setsockopt() failed\00"
@.str.2 = [14 x i8] c"bind() failed\00"
@.str.3 = [21 x i8] c"getsockname() failed\00"
@.str.4 = [16 x i8] c"listen() failed\00"
define i32 @startup(i16* %0) {
%2 = alloca i32
%3 = alloca i16*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i16* %0, i16** %3
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %74
15: ; preds = %1
%16 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i16*, i16** %3
%20 = load i16, i16* %19
%21 = call i32 @htons(i16 zeroext %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
store i32 1, i32* %5
%27 = load i32, i32* %4
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = call i32 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %5, i32 4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %15
%33 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %2
br label %74
36: ; preds = %15
%37 = load i32, i32* %4
%38 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%39 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %2
br label %74
45: ; preds = %36
%46 = load i16*, i16** %3
%47 = load i16, i16* %46
%48 = zext i16 %47 to i32
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %64
50: ; preds = %45
store i32 12, i32* %7
%51 = load i32, i32* %4
%52 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%53 = call i32 @getsockname(i32 %51, %struct.sockaddr* %52, i32* %7)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* %4
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %2
br label %74
59: ; preds = %50
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call zeroext i16 @ntohs(i32 %61)
%63 = load i16*, i16** %3
store i16 %62, i16* %63
br label %64
64: ; preds = %59, %45
%65 = load i32, i32* %4
%66 = call i32 @listen(i32 %65, i32 128)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %72
68: ; preds = %64
%69 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%70 = load i32, i32* %4
%71 = call i32 @close(i32 %70)
store i32 -1, i32* %2
br label %74
72: ; preds = %64
%73 = load i32, i32* %4
store i32 %73, i32* %2
br label %74
74: ; preds = %72, %68, %55, %41, %32, %13
%75 = load i32, i32* %2
ret i32 %75
}

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
@.str = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [20 x i8] c"setsockopt() failed\00"
@.str.2 = [14 x i8] c"bind() failed\00"
@.str.3 = [21 x i8] c"getsockname() failed\00"
@.str.4 = [16 x i8] c"listen() failed\00"
define i32 @startup(i16* nocapture %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %56
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i16, i16* %0
%19 = call i32 @htons(i16 zeroext %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %22, i32* %23
store i32 1, i32* %2
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %10, i32 %24, i32 %25, i32* %2, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @close(i32 %10)
br label %56
31: ; preds = %14
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @bind(i32 %10, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @close(i32 %10)
br label %56
38: ; preds = %31
%39 = load i16, i16* %0
%40 = icmp eq i16 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %38
store i32 12, i32* %4
%42 = call i32 @getsockname(i32 %10, %struct.sockaddr* %32, i32* %4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @close(i32 %10)
br label %56
47: ; preds = %41
%48 = load i32, i32* %20
%49 = call zeroext i16 @ntohs(i32 %48)
store i16 %49, i16* %0
br label %50
50: ; preds = %47, %38
%51 = call i32 @listen(i32 %10, i32 128)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @close(i32 %10)
br label %56
56: ; preds = %50, %53, %44, %35, %28, %12
%57 = phi i32 [ -1, %12 ], [ -1, %28 ], [ -1, %35 ], [ -1, %44 ], [ -1, %53 ], [ %10, %50 ]
ret i32 %57
}