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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ printw(char *);
int /*<<< orphan*/ refresh();
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_socket() {
  struct sockaddr_in saddr;
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(123);
  saddr.sin_addr.s_addr = INADDR_ANY;
  int s;
  if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    printw("Something wrong with socket!\n");
    refresh();
  }
  if (bind(s, (struct sockaddr *)&saddr, (socklen_t)(sizeof(saddr))) < 0) {
    printw("Bind error!\n");
    refresh();
    return -1;
  }
  printw("Bind ok.\n");
  refresh();
  if (listen(s, 3) == -1) {
    printw("Listen failed\n");
    refresh();
  }
  return s;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x7b,%edi
call   1030 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11c5 <open_socket+0x65>
lea    0xe47(%rip),%rdi
call   1040 <printw@plt>
mov    $0x0,%al
call   1060 <refresh@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    11ff <open_socket+0x9f>
lea    0xe37(%rip),%rdi
call   1040 <printw@plt>
mov    $0x0,%al
call   1060 <refresh@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <open_socket+0xe1>
lea    0xe25(%rip),%rdi
call   1040 <printw@plt>
mov    $0x0,%al
call   1060 <refresh@plt>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    123b <open_socket+0xdb>
lea    0xe06(%rip),%rdi
call   1040 <printw@plt>
mov    $0x0,%al
call   1060 <refresh@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x7b,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11bf <open_socket+0x5f>
lea    0xe4d(%rip),%rdi
call   1040 <printw@plt>
xor    %eax,%eax
call   1060 <refresh@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1200 <open_socket+0xa0>
lea    0xe51(%rip),%rdi
call   1040 <printw@plt>
xor    %eax,%eax
call   1060 <refresh@plt>
mov    %ebx,%edi
mov    $0x3,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    1218 <open_socket+0xb8>
lea    0xe37(%rip),%rdi
jmp    120c <open_socket+0xac>
mov    $0xffffffff,%ebx
lea    0xe12(%rip),%rdi
call   1040 <printw@plt>
xor    %eax,%eax
call   1060 <refresh@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [30 x i8] c"Something wrong with socket!\0A\00"
@.str.1 = [13 x i8] c"Bind error!\0A\00"
@.str.2 = [10 x i8] c"Bind ok.\0A\00"
@.str.3 = [15 x i8] c"Listen failed\0A\00"
define i32 @open_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %4, i32* %5
%6 = call i32 @htons(i32 123)
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @printw(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%17 = call i32 (...) @refresh()
br label %18
18: ; preds = %15, %0
%19 = load i32, i32* %3
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @printw(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 (...) @refresh()
store i32 -1, i32* %1
br label %37
26: ; preds = %18
%27 = call i32 @printw(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 (...) @refresh()
%29 = load i32, i32* %3
%30 = call i32 @listen(i32 %29, i32 3)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @printw(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 (...) @refresh()
br label %35
35: ; preds = %32, %26
%36 = load i32, i32* %3
store i32 %36, i32* %1
br label %37
37: ; preds = %35, %23
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [30 x i8] c"Something wrong with socket!\0A\00"
@.str.1 = [13 x i8] c"Bind error!\0A\00"
@.str.2 = [10 x i8] c"Bind ok.\0A\00"
@.str.3 = [15 x i8] c"Listen failed\0A\00"
define i32 @open_socket() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %3, i32* %4
%5 = tail call i32 @htons(i32 123)
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %5, i32* %6
%7 = load i32, i32* @INADDR_ANY
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @printw(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 (...) @refresh()
br label %16
16: ; preds = %13, %0
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i64 @bind(i32 %11, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = call i32 @printw(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 (...) @refresh()
%23 = call i32 @listen(i32 %11, i32 3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %20, %16
%26 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %16 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), %20 ]
%27 = phi i32 [ -1, %16 ], [ %11, %20 ]
%28 = call i32 @printw(i8* %26)
%29 = call i32 (...) @refresh()
br label %30
30: ; preds = %25, %20
%31 = phi i32 [ %11, %20 ], [ %27, %25 ]
ret i32 %31
}