#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_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ local;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
void *INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char const *, TYPE_1__ *);
int /*<<< orphan*/ isdigit(char const);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memcpy(TYPE_1__ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_sock(const char *host, int port) {
  struct sockaddr_in local;
  struct hostent *h;
  int sock;
  memset(&local, 0, sizeof(local));
  local.sin_family = AF_INET;
  local.sin_port = htons(port);
  if (host == 0 || *host == 0) {
    local.sin_addr.s_addr = INADDR_ANY;
  } else if (!isdigit(host[0]) || inet_aton(host, &local.sin_addr) == 0) {
    if ((h = gethostbyname(host)) != 0) {
      memcpy(&local.sin_addr, h->h_addr_list[0], h->h_length);
    } else {
      local.sin_addr.s_addr = INADDR_ANY;
    }
  }
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    return sock;
  }
  if (bind(sock, (struct sockaddr *)&local, sizeof(local)) < 0 ||
      listen(sock, 5) < 0) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<setup_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f1 <setup_sock+0x51>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1204 <setup_sock+0x64>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    127d <setup_sock+0xdd>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
je     1230 <setup_sock+0x90>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    1278 <setup_sock+0xd8>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1265 <setup_sock+0xc5>
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
jmp    1273 <setup_sock+0xd3>
mov    0x2d74(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1278 <setup_sock+0xd8>
jmp    127d <setup_sock+0xdd>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    12ad <setup_sock+0x10d>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f9 <setup_sock+0x159>
mov    -0x34(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jl     12df <setup_sock+0x13f>
mov    -0x34(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
jge    12f3 <setup_sock+0x153>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <setup_sock+0x159>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_sock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e25(%rip),%r14
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
test   %rbx,%rbx
je     121b <setup_sock+0x7b>
mov    (%rbx),%al
test   %al,%al
je     121b <setup_sock+0x7b>
movsbl %al,%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11fc <setup_sock+0x5c>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <inet_aton@plt>
test   %rax,%rax
jne    1229 <setup_sock+0x89>
mov    %rbx,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     121b <setup_sock+0x7b>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
mov    %rsp,%rdi
call   1080 <memcpy@plt>
jmp    1229 <setup_sock+0x89>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    (%r14),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1273 <setup_sock+0xd3>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     1267 <setup_sock+0xc7>
mov    %ebx,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
test   %rax,%rax
jns    1273 <setup_sock+0xd3>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i8* }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i8* null
@SOCK_STREAM = global i32 0
define i32 @setup_sock(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 16)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* %5
%13 = call i32 @htons(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17, %2
%23 = load i8*, i8** @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i32 0, i32 0
store i8* %23, i8** %25
br label %58
26: ; preds = %17
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = call i32 @isdigit(i8 signext %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%35 = call i64 @inet_aton(i8* %33, %struct.TYPE_3__* %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %57
37: ; preds = %32, %26
%38 = load i8*, i8** %4
%39 = call %struct.hostent* @gethostbyname(i8* %38)
store %struct.hostent* %39, %struct.hostent** %7
%40 = icmp ne %struct.hostent* %39, null
br i1 %40, label %41, label %52
41: ; preds = %37
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%43 = load %struct.hostent*, %struct.hostent** %7
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 1
%45 = load i32*, i32** %44
%46 = getelementptr inbounds i32, i32* %45, i64 0
%47 = load i32, i32* %46
%48 = load %struct.hostent*, %struct.hostent** %7
%49 = getelementptr inbounds %struct.hostent, %struct.hostent* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @memcpy(%struct.TYPE_3__* %42, i32 %47, i32 %50)
br label %56
52: ; preds = %37
%53 = load i8*, i8** @INADDR_ANY
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%55 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %54, i32 0, i32 0
store i8* %53, i8** %55
br label %56
56: ; preds = %52, %41
br label %57
57: ; preds = %56, %32
br label %58
58: ; preds = %57, %22
%59 = load i32, i32* @AF_INET
%60 = load i32, i32* @SOCK_STREAM
%61 = call i32 @socket(i32 %59, i32 %60, i32 0)
store i32 %61, i32* %8
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %58
%64 = load i32, i32* %8
store i32 %64, i32* %3
br label %79
65: ; preds = %58
%66 = load i32, i32* %8
%67 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%68 = call i64 @bind(i32 %66, %struct.sockaddr* %67, i32 16)
%69 = icmp slt i64 %68, 0
br i1 %69, label %74, label %70
70: ; preds = %65
%71 = load i32, i32* %8
%72 = call i64 @listen(i32 %71, i32 5)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %70, %65
%75 = load i32, i32* %8
%76 = call i32 @close(i32 %75)
store i32 -1, i32* %3
br label %79
77: ; preds = %70
%78 = load i32, i32* %8
store i32 %78, i32* %3
br label %79
79: ; preds = %77, %74, %63
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i8* }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i8* null
@SOCK_STREAM = global i32 0
define i32 @setup_sock(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 16)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = call i32 @htons(i32 %1)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = icmp eq i8* %0, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11, %2
%15 = load i8*, i8** @INADDR_ANY
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i8* %15, i8** %16
br label %38
17: ; preds = %11
%18 = call i32 @isdigit(i8 signext %12)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%22 = call i64 @inet_aton(i8* %0, %struct.TYPE_3__* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %38
24: ; preds = %20, %17
%25 = call %struct.hostent* @gethostbyname(i8* %0)
%26 = icmp eq %struct.hostent* %25, null
br i1 %26, label %35, label %27
27: ; preds = %24
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i64 0, i32 1
%30 = load i32*, i32** %29
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(%struct.TYPE_3__* %28, i32 %31, i32 %33)
br label %38
35: ; preds = %24
%36 = load i8*, i8** @INADDR_ANY
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i8* %36, i8** %37
br label %38
38: ; preds = %20, %35, %27, %14
%39 = load i32, i32* @AF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
%42 = icmp slt i32 %41, 0
br i1 %42, label %52, label %43
43: ; preds = %38
%44 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%45 = call i64 @bind(i32 %41, %struct.sockaddr* %44, i32 16)
%46 = icmp slt i64 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %43
%48 = call i64 @listen(i32 %41, i32 5)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47, %43
%51 = call i32 @close(i32 %41)
br label %52
52: ; preds = %47, %38, %50
%53 = phi i32 [ -1, %50 ], [ %41, %38 ], [ %41, %47 ]
ret i32 %53
}