#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*/ flag;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, unsigned int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      unsigned int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int create_server(int port, unsigned int max_backlog, int *pport) {
  int server;
  server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (server < 0) {
    fputs("Skipping test: cannot create server socket: socket() failed\n",
          stderr);
    exit(77);
  }
  if (port != 0) {
    unsigned int flag = 1;
    if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0) {
      fputs("Skipping test: cannot create server socket: setsockopt() failed\n",
            stderr);
      exit(77);
    }
  }
  {
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    {
      unsigned char dotted[4] = {127, 0, 0, 1};
      memcpy(&addr.sin_addr.s_addr, dotted, 4);
    }
    addr.sin_port = htons(port);
    if (bind(server, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
      fputs("Skipping test: cannot create server socket: bind() failed\n",
            stderr);
      exit(77);
    }
  }
  if (port == 0) {
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    if (getsockname(server, (struct sockaddr *)&addr, &addrlen) < 0) {
      fputs(
          "Skipping test: cannot create server socket: getsockname() failed\n",
          stderr);
      exit(77);
    }
    port = ntohs(addr.sin_port);
  }
  if (listen(server, max_backlog) < 0) {
    fputs("Skipping test: cannot create server socket: listen() failed\n",
          stderr);
    exit(77);
  }
  *pport = port;
  return server;
}


x86 assembly (clang, -O0):
<create_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120e <create_server+0x5e>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xe01(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x4d,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     1271 <create_server+0xc1>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    126c <create_server+0xbc>
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
lea    0xde0(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x4d,%edi
call   10c0 <exit@plt>
jmp    1271 <create_server+0xc1>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0xea5(%rip),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12f1 <create_server+0x141>
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
lea    0xd9c(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x4d,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1346 <create_server+0x196>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10b0 <getsockname@plt>
cmp    $0x0,%rax
jge    133b <create_server+0x18b>
mov    0x2ca5(%rip),%rax
mov    (%rax),%esi
lea    0xd8d(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x4d,%edi
call   10c0 <exit@plt>
mov    -0x38(%rbp),%edi
call   1090 <ntohs@plt>
mov    %eax,-0x4(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    137a <create_server+0x1ca>
mov    0x2c66(%rip),%rax
mov    (%rax),%esi
lea    0xd90(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x4d,%edi
call   10c0 <exit@plt>
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12d1 <create_server+0x121>
mov    %eax,%ebx
test   %ebp,%ebp
je     1227 <create_server+0x77>
movl   $0x1,0x10(%rsp)
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1307 <create_server+0x157>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movl   $0x100007f,0xc(%rsp)
01
lea    0x14(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     12e3 <create_server+0x133>
test   %ebp,%ebp
jne    12b0 <create_server+0x100>
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10b0 <getsockname@plt>
test   %rax,%rax
js     1319 <create_server+0x169>
mov    0x10(%rsp),%edi
call   1090 <ntohs@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r15d,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     12f5 <create_server+0x145>
mov    %ebp,(%r14)
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
lea    0xd1f(%rip),%rdi
jmp    1329 <create_server+0x179>
mov    0x2cde(%rip),%rax
mov    (%rax),%esi
lea    0xd8b(%rip),%rdi
jmp    1329 <create_server+0x179>
mov    0x2ccc(%rip),%rax
mov    (%rax),%esi
lea    0xdf6(%rip),%rdi
jmp    1329 <create_server+0x179>
mov    0x2cba(%rip),%rax
mov    (%rax),%esi
lea    0xd26(%rip),%rdi
jmp    1329 <create_server+0x179>
mov    0x2ca8(%rip),%rax
mov    (%rax),%esi
lea    0xd90(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x4d,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [61 x i8] c"Skipping test: cannot create server socket: socket() failed\0A\00"
@stderr = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [65 x i8] c"Skipping test: cannot create server socket: setsockopt() failed\0A\00"
@AF_INET = global i32 0
@__const.create_server.dotted = [4 x i8] c"\7F\00\00\01"
@.str.2 = [59 x i8] c"Skipping test: cannot create server socket: bind() failed\0A\00"
@.str.3 = [66 x i8] c"Skipping test: cannot create server socket: getsockname() failed\0A\00"
@.str.4 = [61 x i8] c"Skipping test: cannot create server socket: listen() failed\0A\00"
define i32 @create_server(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca [4 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = load i32, i32* @IPPROTO_TCP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = call i32 @fputs(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = call i32 @exit(i32 77)
unreachable
23: ; preds = %3
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %23
store i32 1, i32* %8
%27 = load i32, i32* %7
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = call i64 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %8, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = call i32 @fputs(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = call i32 @exit(i32 77)
unreachable
36: ; preds = %26
br label %37
37: ; preds = %36, %23
%38 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %39, i32* %40
%41 = bitcast [4 x i8]* %10 to i8*
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%45 = call i32 @memcpy(i32* %43, i8* %44, i32 4)
%46 = load i32, i32* %4
%47 = call i32 @htons(i32 %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %47, i32* %48
%49 = load i32, i32* %7
%50 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%51 = call i64 @bind(i32 %49, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %37
%54 = load i32, i32* @stderr
%55 = call i32 @fputs(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = call i32 @exit(i32 77)
unreachable
57: ; preds = %37
%58 = load i32, i32* %4
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %73
60: ; preds = %57
store i32 12, i32* %12
%61 = load i32, i32* %7
%62 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%63 = call i64 @getsockname(i32 %61, %struct.sockaddr* %62, i32* %12)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %60
%66 = load i32, i32* @stderr
%67 = call i32 @fputs(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i32 %66)
%68 = call i32 @exit(i32 77)
unreachable
69: ; preds = %60
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i32 @ntohs(i32 %71)
store i32 %72, i32* %4
br label %73
73: ; preds = %69, %57
%74 = load i32, i32* %7
%75 = load i32, i32* %5
%76 = call i64 @listen(i32 %74, i32 %75)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %73
%79 = load i32, i32* @stderr
%80 = call i32 @fputs(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0), i32 %79)
%81 = call i32 @exit(i32 77)
unreachable
82: ; preds = %73
%83 = load i32, i32* %4
%84 = load i32*, i32** %6
store i32 %83, i32* %84
%85 = load i32, i32* %7
ret i32 %85
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [61 x i8] c"Skipping test: cannot create server socket: socket() failed\0A\00"
@stderr = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [65 x i8] c"Skipping test: cannot create server socket: setsockopt() failed\0A\00"
@AF_INET = global i32 0
@.str.2 = [59 x i8] c"Skipping test: cannot create server socket: bind() failed\0A\00"
@.str.3 = [66 x i8] c"Skipping test: cannot create server socket: getsockname() failed\0A\00"
@.str.4 = [61 x i8] c"Skipping test: cannot create server socket: listen() failed\0A\00"
define i32 @create_server(i32 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = load i32, i32* @IPPROTO_TCP
%12 = tail call i32 @socket(i32 %9, i32 %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = tail call i32 @fputs(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = tail call i32 @exit(i32 77)
unreachable
18: ; preds = %3
%19 = icmp eq i32 %0, 0
br i1 %19, label %31, label %20
20: ; preds = %18
%21 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i64 @setsockopt(i32 %12, i32 %22, i32 %23, i32* %4, i32 4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fputs(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 @exit(i32 77)
unreachable
30: ; preds = %20
br label %31
31: ; preds = %30, %18
%32 = bitcast %struct.sockaddr_in* %5 to i8*
%33 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %34, i32* %35
%36 = bitcast i32* %6 to i8*
store i32 16777343, i32* %6
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%38 = call i32 @memcpy(i32* %37, i8* %36, i32 4)
%39 = call i32 @htons(i32 %0)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%42 = call i64 @bind(i32 %12, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %31
%45 = load i32, i32* @stderr
%46 = call i32 @fputs(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = call i32 @exit(i32 77)
unreachable
48: ; preds = %31
br i1 %19, label %49, label %63
49: ; preds = %48
%50 = bitcast %struct.sockaddr_in* %7 to i8*
%51 = bitcast i32* %8 to i8*
store i32 12, i32* %8
%52 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%53 = call i64 @getsockname(i32 %12, %struct.sockaddr* %52, i32* %8)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = call i32 @fputs(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i32 %56)
%58 = call i32 @exit(i32 77)
unreachable
59: ; preds = %49
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @ntohs(i32 %61)
br label %63
63: ; preds = %59, %48
%64 = phi i32 [ %62, %59 ], [ %0, %48 ]
%65 = call i64 @listen(i32 %12, i32 %1)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %63
%68 = load i32, i32* @stderr
%69 = call i32 @fputs(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0), i32 %68)
%70 = call i32 @exit(i32 77)
unreachable
71: ; preds = %63
store i32 %64, i32* %2
ret i32 %12
}