#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;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ opt;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Done(int);
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int DeploySocket(char *SockName, int SockPort) {
  struct sockaddr_in addr;
  int SockFd, opt;
  SockFd = socket(PF_INET, SOCK_STREAM, 0);
  if (SockFd < 0) {
    perror("socket()");
    Done(2);
  }
  addr.sin_family = AF_INET;
  if (*SockName) {
    struct hostent *host = gethostbyname(SockName);
    if (!host) {
      perror("gethostbyname()");
      exit(6);
    }
    addr.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
  } else {
    addr.sin_addr.s_addr = INADDR_ANY;
  }
  addr.sin_port = htons(SockPort);
  opt = 1;
  if (setsockopt(SockFd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
    perror("setsockopt()");
    Done(2);
  }
  if (bind(SockFd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind()");
    Done(2);
  }
  if (listen(SockFd, 16) < 0) {
    perror("listen()");
    Done(2);
  }
  if (fcntl(SockFd, F_SETFL, O_NONBLOCK) < 0) {
    perror("fcntl()");
    Done(2);
  }
  printf("Deployed socket: %s:%d\n", SockName, SockPort);
  return SockFd;
}


x86 assembly (clang, -O0):
<DeploySocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11fb <DeploySocket+0x4b>
lea    0xe14(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1253 <DeploySocket+0xa3>
mov    -0x8(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1242 <DeploySocket+0x92>
lea    0xdd6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x6,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    125f <DeploySocket+0xaf>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    12b5 <DeploySocket+0x105>
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12e6 <DeploySocket+0x136>
lea    0xd4f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
mov    -0x1c(%rbp),%edi
mov    $0x10,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    1313 <DeploySocket+0x163>
lea    0xd29(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
mov    -0x1c(%rbp),%edi
mov    0x2ca3(%rip),%rax
mov    (%rax),%esi
mov    0x2cb2(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
cmp    $0x0,%rax
jge    134d <DeploySocket+0x19d>
lea    0xcf8(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xce3(%rip),%rdi
call   1070 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DeploySocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12c5 <DeploySocket+0x115>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
cmpb   $0x0,(%rbx)
je     1209 <DeploySocket+0x59>
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     134c <DeploySocket+0x19c>
mov    (%rax),%rax
jmp    1210 <DeploySocket+0x60>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     12e0 <DeploySocket+0x130>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     12fb <DeploySocket+0x14b>
mov    %ebp,%edi
mov    $0x10,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     1316 <DeploySocket+0x166>
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1050 <fcntl@plt>
test   %rax,%rax
js     1331 <DeploySocket+0x181>
lea    0xd8f(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1070 <printf@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd34(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
jmp    11e1 <DeploySocket+0x31>
lea    0xd32(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
jmp    1257 <DeploySocket+0xa7>
lea    0xd24(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
jmp    1271 <DeploySocket+0xc1>
lea    0xd10(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
jmp    1286 <DeploySocket+0xd6>
lea    0xcfe(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   1030 <Done@plt>
jmp    12a8 <DeploySocket+0xf8>
lea    0xcb6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x6,%edi
call   10c0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@AF_INET = global i32 0
@.str.1 = [16 x i8] c"gethostbyname()\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [13 x i8] c"setsockopt()\00"
@.str.3 = [7 x i8] c"bind()\00"
@.str.4 = [9 x i8] c"listen()\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.5 = [8 x i8] c"fcntl()\00"
@.str.6 = [24 x i8] c"Deployed socket: %s:%d\0A\00"
define i32 @DeploySocket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @Done(i32 2)
br label %14
14: ; preds = %11, %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i8, i8* %0
%18 = icmp eq i8 %17, 0
br i1 %18, label %30, label %19
19: ; preds = %14
%20 = tail call %struct.hostent* @gethostbyname(i8* %0)
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 6)
unreachable
25: ; preds = %19
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to %struct.in_addr*
%29 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %28, i64 0, i32 0
br label %30
30: ; preds = %14, %25
%31 = phi i32* [ %29, %25 ], [ @INADDR_ANY, %14 ]
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %32, i32* %33
%34 = tail call i32 @htons(i32 %1)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %34, i32* %35
store i32 1, i32* %4
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_REUSEADDR
%38 = call i64 @setsockopt(i32 %9, i32 %36, i32 %37, i32* %4, i32 4)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @Done(i32 2)
br label %43
43: ; preds = %40, %30
%44 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%45 = call i64 @bind(i32 %9, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @Done(i32 2)
br label %50
50: ; preds = %47, %43
%51 = call i64 @listen(i32 %9, i32 16)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @Done(i32 2)
br label %56
56: ; preds = %53, %50
%57 = load i32, i32* @F_SETFL
%58 = load i32, i32* @O_NONBLOCK
%59 = call i64 @fcntl(i32 %9, i32 %57, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%63 = call i32 @Done(i32 2)
br label %64
64: ; preds = %61, %56
%65 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %0, i32 %1)
ret i32 %9
}