#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 */
typedef int /*<<< orphan*/ svaddr;
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*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ atoi(char const *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int create_socket(const char *port) {
  struct sockaddr_in svaddr;
  int fd, opt;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    fprintf(stderr, "socket() failed: %s\n", strerror(errno));
    return -1;
  }
  opt = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
    fprintf(stderr, "setsockopt() failed: %s\n", strerror(errno));
    return -1;
  }
  memset(&svaddr, 0, sizeof(svaddr));
  svaddr.sin_family = AF_INET;
  svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  svaddr.sin_port = htons(atoi(port));
  if (bind(fd, (struct sockaddr *)&svaddr, sizeof(svaddr)) == -1) {
    fprintf(stderr, "bind() failed: %s\n", strerror(errno));
    close(fd);
    return -1;
  }
  if (listen(fd, 128) == -1) {
    fprintf(stderr, "listen() failed: %s\n", strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    121a <create_socket+0x6a>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdf7(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <create_socket+0x1cd>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1287 <create_socket+0xd7>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xd9f(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <create_socket+0x1cd>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1321 <create_socket+0x171>
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xd26(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <create_socket+0x1cd>
mov    -0x24(%rbp),%edi
mov    $0x80,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1377 <create_socket+0x1c7>
mov    0x2c8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xce3(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <create_socket+0x1cd>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e1c(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12a0 <create_socket+0xf0>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     12c0 <create_socket+0x110>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12ea <create_socket+0x13a>
mov    %ebx,%edi
mov    $0x80,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    131e <create_socket+0x16e>
mov    0x2d41(%rip),%rax
mov    (%rax),%ebp
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xda3(%rip),%rsi
jmp    1308 <create_socket+0x158>
mov    0x2d21(%rip),%rax
mov    (%rax),%ebx
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd42(%rip),%rsi
jmp    12de <create_socket+0x12e>
mov    0x2d01(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd37(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
jmp    1319 <create_socket+0x169>
mov    0x2cd7(%rip),%rax
mov    (%rax),%ebp
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd26(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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
@stderr = global i32 0
@.str = [21 x i8] c"socket() failed: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [25 x i8] c"setsockopt() failed: %s\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [19 x i8] c"bind() failed: %s\0A\00"
@.str.3 = [21 x i8] c"listen() failed: %s\0A\00"
define i32 @create_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %2
br label %64
17: ; preds = %1
store i32 1, i32* %6
%18 = load i32, i32* %5
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i64 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %6, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 -1, i32* %2
br label %64
28: ; preds = %17
%29 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%33 = call i32 @htonl(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i8*, i8** %3
%37 = call i32 @atoi(i8* %36)
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %5
%41 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %51
44: ; preds = %28
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %2
br label %64
51: ; preds = %28
%52 = load i32, i32* %5
%53 = call i32 @listen(i32 %52, i32 128)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %62
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %58)
%60 = load i32, i32* %5
%61 = call i32 @close(i32 %60)
store i32 -1, i32* %2
br label %64
62: ; preds = %51
%63 = load i32, i32* %5
store i32 %63, i32* %2
br label %64
64: ; preds = %62, %55, %44, %23, %12
%65 = load i32, i32* %2
ret i32 %65
}

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