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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ getsockname(int, struct sockaddr *, unsigned int *);
scalar_t__ htons(int);
int ntohs(scalar_t__);
int /*<<< orphan*/ reset();
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createsocket(int *port) {
  unsigned int length;
  int sock;
  struct sockaddr_in server;
  int sc_reuseaddr = 1, temp;
  reset();
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    return -1;
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  if (*port != 0)
    server.sin_port = htons(*port);
  else
    server.sin_port = 0;
  temp = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &sc_reuseaddr,
                    sizeof(sc_reuseaddr));
  if (bind(sock, (struct sockaddr *)&server, sizeof(server)))
    return -1;
  length = sizeof(server);
  if (getsockname(sock, (struct sockaddr *)&server, &length))
    return -1;
  *port = ntohs(server.sin_port);
  return sock;
}


x86 assembly (clang, -O0):
<createsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <reset@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bc <createsocket+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <createsocket+0x11f>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     11f5 <createsocket+0x85>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x28(%rbp)
jmp    11fd <createsocket+0x8d>
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     124b <createsocket+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <createsocket+0x11f>
movl   $0x10,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <getsockname@plt>
cmp    $0x0,%rax
je     1278 <createsocket+0x108>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <createsocket+0x11f>
mov    -0x28(%rbp),%rdi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createsocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movl   $0x1,(%rsp)
xor    %eax,%eax
call   1050 <reset@plt>
mov    0x2e4e(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1242 <createsocket+0xd2>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%r14),%edi
test   %edi,%edi
je     11d5 <createsocket+0x65>
call   1040 <htons@plt>
jmp    11d7 <createsocket+0x67>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    1242 <createsocket+0xd2>
movl   $0x10,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1080 <getsockname@plt>
test   %rax,%rax
jne    1242 <createsocket+0xd2>
mov    0x8(%rsp),%rdi
call   1060 <ntohs@plt>
mov    %eax,(%r14)
mov    %ebp,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @createsocket(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32 1, i32* %7
%9 = call i32 (...) @reset()
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %54
16: ; preds = %1
%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 i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32*, i32** %3
%23 = load i32, i32* %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %16
%26 = load i32*, i32** %3
%27 = load i32, i32* %26
%28 = call i64 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i64 %28, i64* %29
br label %32
30: ; preds = %16
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i64 0, i64* %31
br label %32
32: ; preds = %30, %25
%33 = load i32, i32* %5
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i32* %7, i32 4)
store i32 %36, i32* %8
%37 = load i32, i32* %5
%38 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 16)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %32
store i32 -1, i32* %2
br label %54
42: ; preds = %32
store i32 16, i32* %4
%43 = load i32, i32* %5
%44 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%45 = call i64 @getsockname(i32 %43, %struct.sockaddr* %44, i32* %4)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 -1, i32* %2
br label %54
48: ; preds = %42
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%50 = load i64, i64* %49
%51 = call i32 @ntohs(i64 %50)
%52 = load i32*, i32** %3
store i32 %51, i32* %52
%53 = load i32, i32* %5
store i32 %53, i32* %2
br label %54
54: ; preds = %48, %47, %41, %15
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @createsocket(i32* 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*
store i32 1, i32* %4
%8 = tail call i32 (...) @reset()
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %37, label %13
13: ; preds = %1
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %0
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %13
%21 = tail call i64 @htons(i32 %18)
br label %22
22: ; preds = %13, %20
%23 = phi i64 [ %21, %20 ], [ 0, %13 ]
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i64 %23, i64* %24
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %11, i32 %25, i32 %26, i32* %4, i32 4)
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @bind(i32 %11, %struct.sockaddr* %28, i32 16)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %22
store i32 16, i32* %2
%32 = call i64 @getsockname(i32 %11, %struct.sockaddr* %28, i32* %2)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i64, i64* %24
%36 = call i32 @ntohs(i64 %35)
store i32 %36, i32* %0
br label %37
37: ; preds = %31, %22, %1, %34
%38 = phi i32 [ %11, %34 ], [ -1, %1 ], [ -1, %22 ], [ -1, %31 ]
ret i32 %38
}