#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  char sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcpStart(char *hostname, int portno, int *errorh) {
  int error, handle;
  *errorh = 0;
  struct hostent *host;
  struct sockaddr_in server;
  bzero((char *)&server, sizeof(server));
  host = gethostbyname(hostname);
  handle = socket(AF_INET, SOCK_STREAM, 0);
  int one = 1;
  setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  if (handle == -1) {
    perror("Socket");
    handle = 0;
    *errorh = 1;
  } else {
    server.sin_family = AF_INET;
    server.sin_port = htons(portno);
    server.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(server.sin_zero), 8);
    error = bind(handle, (struct sockaddr *)&server, sizeof(struct sockaddr));
    if (error == -1) {
      perror("Bind");
      handle = 0;
      *errorh = 1;
    } else {
      error = listen(handle, 5);
      if (error != 0) {
        perror("Listen");
        handle = 0;
        *errorh = 1;
      }
    }
  }
  return handle;
}


x86 assembly (clang, -O0):
<tcpStart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x38(%rbp),%rdi
mov    $0x10,%esi
call   1090 <bzero@plt>
mov    -0x8(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmpl   $0xffffffff,-0x20(%rbp)
jne    122b <tcpStart+0xab>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    12dd <tcpStart+0x15d>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    129c <tcpStart+0x11c>
lea    0xd86(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    12d8 <tcpStart+0x158>
mov    -0x20(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12d3 <tcpStart+0x153>
lea    0xd4f(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    12d8 <tcpStart+0x158>
jmp    12dd <tcpStart+0x15d>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpStart>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
movl   $0x0,(%rdx)
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
call   1090 <bzero@plt>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
mov    %rax,%rbx
mov    0x2e24(%rip),%r12
mov    (%r12),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%ebp
je     1265 <tcpStart+0xe5>
mov    (%r12),%eax
mov    %eax,0x1c(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     126e <tcpStart+0xee>
mov    %ebp,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %eax,%eax
jne    1277 <tcpStart+0xf7>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd94(%rip),%rdi
jmp    127e <tcpStart+0xfe>
lea    0xd92(%rip),%rdi
jmp    127e <tcpStart+0xfe>
lea    0xd8e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,(%r14)
xor    %ebp,%ebp
jmp    1256 <tcpStart+0xd6>

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

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