#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_socket(int port) {
  int s;
  char *opt;
  char hostname[256 + 1];
  struct sockaddr_in sa;
  struct hostent *hp;
  bzero(&sa, sizeof(struct sockaddr_in));
  gethostname(hostname, 256);
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0) {
    perror("Init-socket");
    exit(1);
  }
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) {
    perror("setsockopt REUSEADDR");
    exit(1);
  }
  if (bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    perror("bind");
    close(s);
    exit(1);
  }
  listen(s, 3);
  return (s);
}


x86 assembly (clang, -O0):
<init_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x128(%rbp),%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10a0 <gethostname@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x128(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1229 <init_socket+0x89>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    126d <init_socket+0xcd>
lea    0xdae(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x128(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    12a9 <init_socket+0x109>
lea    0xd8f(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x3,%esi
call   1060 <listen@plt>
mov    -0x8(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_socket>:
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %edi,%ebx
mov    %rsp,%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   10a0 <gethostname@plt>
mov    0x2e11(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    (%r14),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     124f <init_socket+0xaf>
mov    %eax,%ebx
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1258 <init_socket+0xb8>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     126e <init_socket+0xce>
mov    %ebx,%edi
mov    $0x3,%esi
call   1060 <listen@plt>
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdaa(%rip),%rdi
jmp    125f <init_socket+0xbf>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdac(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"Init-socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [21 x i8] c"setsockopt REUSEADDR\00"
@.str.2 = [5 x i8] c"bind\00"
define i32 @init_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [257 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
store i32 %0, i32* %2
%8 = call i32 @bzero(%struct.sockaddr_in* %6, i32 8)
%9 = getelementptr inbounds [257 x i8], [257 x i8]* %5, i64 0, i64 0
%10 = call i32 @gethostname(i8* %9, i32 256)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* %2
%14 = call i32 @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %1
%25 = load i32, i32* %3
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = bitcast i8** %4 to i8*
%29 = call i64 @setsockopt(i32 %25, i32 %26, i32 %27, i8* %28, i32 8)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %24
%35 = load i32, i32* %3
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i64 @bind(i32 %35, %struct.sockaddr* %36, i32 8)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34
%45 = load i32, i32* %3
%46 = call i32 @listen(i32 %45, i32 3)
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"Init-socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [21 x i8] c"setsockopt REUSEADDR\00"
@.str.2 = [5 x i8] c"bind\00"
define i32 @init_socket(i32 %0) {
%2 = alloca i8*
%3 = alloca [257 x i8]
%4 = alloca %struct.sockaddr_in
%5 = bitcast i8** %2 to i8*
%6 = getelementptr inbounds [257 x i8], [257 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = call i32 @bzero(%struct.sockaddr_in* %4, i32 8)
%9 = call i32 @gethostname(i8* %6, i32 256)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %10, i32* %11
%12 = call i32 @htons(i32 %0)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %1
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i64 @setsockopt(i32 %16, i32 %22, i32 %23, i8* %5, i32 8)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%31 = call i64 @bind(i32 %16, %struct.sockaddr* %30, i32 8)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @close(i32 %16)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29
%38 = call i32 @listen(i32 %16, i32 3)
ret i32 %16
}