#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*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ optval;
typedef int /*<<< orphan*/ inaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int server_connect(uint16_t server_port) {
  struct sockaddr_in inaddr;
  int lsock = -1;
  int dsock = -1;
  int optval = 1;
  int ret;
  lsock = socket(AF_INET, SOCK_STREAM, 0);
  do {
    if (lsock < 0) {
      fprintf(stderr, "Failed to %s\n", "open server socket");
      goto err;
    }
  } while (0);
  optval = 1;
  ret = setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  do {
    if (ret < 0) {
      fprintf(stderr, "Failed to %s\n", "server setsockopt()");
      goto err_sock;
    }
  } while (0);
  inaddr.sin_family = AF_INET;
  inaddr.sin_port = htons(server_port);
  inaddr.sin_addr.s_addr = INADDR_ANY;
  memset(inaddr.sin_zero, 0, sizeof(inaddr.sin_zero));
  ret = bind(lsock, (struct sockaddr *)&inaddr, sizeof(inaddr));
  do {
    if (ret < 0) {
      fprintf(stderr, "Failed to %s\n", "bind server");
      goto err_sock;
    }
  } while (0);
  ret = listen(lsock, 0);
  do {
    if (ret < 0) {
      fprintf(stderr, "Failed to %s\n", "listen server");
      goto err_sock;
    }
  } while (0);
  fprintf(stdout, "Waiting for connection...\n");
  dsock = accept(lsock, NULL, NULL);
  do {
    if (dsock < 0) {
      fprintf(stderr, "Failed to %s\n", "accept server");
      goto err_sock;
    }
  } while (0);
  close(lsock);
  return dsock;
err_sock:
  close(lsock);
err:
  return -1;
}


x86 assembly (clang, -O0):
<server_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f9 <server_connect+0x69>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
lea    0xe21(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    138b <server_connect+0x1fb>
jmp    11fe <server_connect+0x6e>
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1259 <server_connect+0xc9>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
lea    0xdd4(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1383 <server_connect+0x1f3>
jmp    125e <server_connect+0xce>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12d1 <server_connect+0x141>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xd42(%rip),%rsi
lea    0xd70(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1383 <server_connect+0x1f3>
jmp    12d6 <server_connect+0x146>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1080 <listen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1310 <server_connect+0x180>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
lea    0xd03(%rip),%rsi
lea    0xd3d(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1383 <server_connect+0x1f3>
jmp    1315 <server_connect+0x185>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
lea    0xd2a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10a0 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    136b <server_connect+0x1db>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
lea    0xca8(%rip),%rsi
lea    0xd0b(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1383 <server_connect+0x1f3>
jmp    1370 <server_connect+0x1e0>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1392 <server_connect+0x202>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
mov    0x2e3f(%rip),%r14
mov    (%r14),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1270 <server_connect+0xe0>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1290 <server_connect+0x100>
mov    (%r14),%eax
mov    %eax,0x1c(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %eax,%eax
js     1299 <server_connect+0x109>
mov    %ebx,%edi
xor    %esi,%esi
call   1080 <listen@plt>
test   %eax,%eax
js     12a2 <server_connect+0x112>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <accept@plt>
test   %eax,%eax
js     12ab <server_connect+0x11b>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
jmp    12d5 <server_connect+0x145>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
lea    0xd87(%rip),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12d0 <server_connect+0x140>
lea    0xd8a(%rip),%rdx
jmp    12b2 <server_connect+0x122>
lea    0xd95(%rip),%rdx
jmp    12b2 <server_connect+0x122>
lea    0xd98(%rip),%rdx
jmp    12b2 <server_connect+0x122>
lea    0xdb8(%rip),%rdx
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, 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 = [14 x i8] c"Failed to %s\0A\00"
@.str.1 = [19 x i8] c"open server socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [20 x i8] c"server setsockopt()\00"
@INADDR_ANY = global i32 0
@.str.3 = [12 x i8] c"bind server\00"
@.str.4 = [14 x i8] c"listen server\00"
@stdout = global i32 0
@.str.5 = [27 x i8] c"Waiting for connection...\0A\00"
@.str.6 = [14 x i8] c"accept server\00"
define i32 @server_connect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %5
store i32 -1, i32* %6
store i32 1, i32* %7
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
br label %12
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %82
18: ; preds = %12
br label %19
19: ; preds = %18
store i32 1, i32* %7
%20 = load i32, i32* %5
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %7, i32 4)
store i32 %23, i32* %8
br label %24
24: ; preds = %19
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %79
30: ; preds = %24
br label %31
31: ; preds = %30
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 3
store i32 %32, i32* %33
%34 = load i32, i32* %3
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i32, i32* @INADDR_ANY
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @memset(i32 %41, i32 0, i32 4)
%43 = load i32, i32* %5
%44 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%45 = call i32 @bind(i32 %43, %struct.sockaddr* %44, i32 16)
store i32 %45, i32* %8
br label %46
46: ; preds = %31
%47 = load i32, i32* %8
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %79
52: ; preds = %46
br label %53
53: ; preds = %52
%54 = load i32, i32* %5
%55 = call i32 @listen(i32 %54, i32 0)
store i32 %55, i32* %8
br label %56
56: ; preds = %53
%57 = load i32, i32* %8
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %79
62: ; preds = %56
br label %63
63: ; preds = %62
%64 = load i32, i32* @stdout
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%66 = load i32, i32* %5
%67 = call i32 @accept(i32 %66, i32* null, i32* null)
store i32 %67, i32* %6
br label %68
68: ; preds = %63
%69 = load i32, i32* %6
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %79
74: ; preds = %68
br label %75
75: ; preds = %74
%76 = load i32, i32* %5
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %6
store i32 %78, i32* %2
br label %83
79: ; preds = %71, %59, %49, %27
%80 = load i32, i32* %5
%81 = call i32 @close(i32 %80)
br label %82
82: ; preds = %79, %15
store i32 -1, i32* %2
br label %83
83: ; preds = %82, %75
%84 = load i32, i32* %2
ret i32 %84
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, 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 = [14 x i8] c"Failed to %s\0A\00"
@.str.1 = [19 x i8] c"open server socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [20 x i8] c"server setsockopt()\00"
@INADDR_ANY = global i32 0
@.str.3 = [12 x i8] c"bind server\00"
@.str.4 = [14 x i8] c"listen server\00"
@stdout = global i32 0
@.str.5 = [27 x i8] c"Waiting for connection...\0A\00"
@.str.6 = [14 x i8] c"accept server\00"
define i32 @server_connect(i32 %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 slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %44
13: ; preds = %1
store i32 1, i32* %3
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %8, i32 %14, i32 %15, i32* %3, i32 4)
%17 = icmp slt i32 %16, 0
br i1 %17, label %39, label %18
18: ; preds = %13
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %19, i32* %20
%21 = call i32 @htons(i32 %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %23, i32* %24
%25 = call i32 @memset(i32 undef, i32 0, i32 4)
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i32 @bind(i32 %8, %struct.sockaddr* %26, i32 16)
%28 = icmp slt i32 %27, 0
br i1 %28, label %39, label %29
29: ; preds = %18
%30 = call i32 @listen(i32 %8, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %39, label %32
32: ; preds = %29
%33 = load i32, i32* @stdout
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 @accept(i32 %8, i32* null, i32* null)
%36 = icmp slt i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = call i32 @close(i32 %8)
br label %44
39: ; preds = %32, %29, %18, %13
%40 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %13 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), %18 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), %29 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), %32 ]
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %40)
%43 = call i32 @close(i32 %8)
br label %44
44: ; preds = %10, %39, %37
%45 = phi i32 [ %35, %37 ], [ -1, %39 ], [ -1, %10 ]
ret i32 %45
}