#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*/ server;
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;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ getsockname(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sock_create(int *port) {
  struct sockaddr_in server;
  int fd;
  unsigned length;
  int on = 1;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("sock_create: (err) socket");
    return -1;
  }
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(*port);
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  if (bind(fd, (struct sockaddr *)&server, sizeof(server))) {
    perror("sock_create: (err) bind");
    return -1;
  }
  length = sizeof(struct sockaddr_in);
  if (getsockname(fd, (struct sockaddr *)&server, &length)) {
    perror("sock_create: (err) getsockname");
    return -1;
  }
  *port = ntohs(server.sin_port);
  if (listen(fd, 10) < 0) {
    perror("sockCreate: (err) listen");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<sock_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11d0 <sock_create+0x50>
lea    0xe41(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <sock_create+0x14b>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     124d <sock_create+0xcd>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <sock_create+0x14b>
movl   $0xc,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1090 <getsockname@plt>
cmp    $0x0,%rax
je     1286 <sock_create+0x106>
lea    0xdbd(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <sock_create+0x14b>
mov    -0x20(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    12c5 <sock_create+0x145>
lea    0xd9d(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <sock_create+0x14b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_create>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
movl   $0x1,0x18(%rsp)
mov    0x2e45(%rip),%r15
mov    (%r15),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1256 <sock_create+0xd6>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    (%r14),%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    125f <sock_create+0xdf>
movl   $0xc,0x1c(%rsp)
lea    0x8(%rsp),%rsi
lea    0x1c(%rsp),%rdx
mov    %ebx,%edi
call   1090 <getsockname@plt>
test   %rax,%rax
jne    1268 <sock_create+0xe8>
mov    0x8(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,(%r14)
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
test   %rax,%rax
jns    1279 <sock_create+0xf9>
lea    0xdfd(%rip),%rdi
jmp    126f <sock_create+0xef>
lea    0xda3(%rip),%rdi
jmp    126f <sock_create+0xef>
lea    0xdb4(%rip),%rdi
jmp    126f <sock_create+0xef>
lea    0xdc3(%rip),%rdi
call   1080 <perror@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 = [26 x i8] c"sock_create: (err) socket\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [24 x i8] c"sock_create: (err) bind\00"
@.str.2 = [31 x i8] c"sock_create: (err) getsockname\00"
@.str.3 = [25 x i8] c"sockCreate: (err) listen\00"
define i32 @sock_create(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 1, i32* %7
%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* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %53
14: ; preds = %1
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32*, i32** %3
%21 = load i32, i32* %20
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %5
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %7, i32 4)
%28 = load i32, i32* %5
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %14
%33 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %53
34: ; preds = %14
store i32 12, i32* %6
%35 = load i32, i32* %5
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i64 @getsockname(i32 %35, %struct.sockaddr* %36, i32* %6)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %53
41: ; preds = %34
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @ntohs(i32 %43)
%45 = load i32*, i32** %3
store i32 %44, i32* %45
%46 = load i32, i32* %5
%47 = call i64 @listen(i32 %46, i32 10)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %53
51: ; preds = %41
%52 = load i32, i32* %5
store i32 %52, i32* %2
br label %53
53: ; preds = %51, %49, %39, %32, %12
%54 = load i32, i32* %2
ret i32 %54
}

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 = [26 x i8] c"sock_create: (err) socket\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [24 x i8] c"sock_create: (err) bind\00"
@.str.2 = [31 x i8] c"sock_create: (err) getsockname\00"
@.str.3 = [25 x i8] c"sockCreate: (err) listen\00"
define i32 @sock_create(i32* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%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 slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %42
14: ; preds = %1
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %0
%20 = tail call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %10, i32 %22, i32 %23, i32* %4, i32 4)
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i64 @bind(i32 %10, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %42
30: ; preds = %14
store i32 12, i32* %3
%31 = call i64 @getsockname(i32 %10, %struct.sockaddr* %25, i32* %3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %42
35: ; preds = %30
%36 = load i32, i32* %21
%37 = call i32 @ntohs(i32 %36)
store i32 %37, i32* %0
%38 = call i64 @listen(i32 %10, i32 10)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %35, %40, %33, %28, %12
%43 = phi i32 [ -1, %12 ], [ -1, %28 ], [ -1, %33 ], [ -1, %40 ], [ %10, %35 ]
ret i32 %43
}