#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*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ TCP_NODELAY;
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 setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, ...);
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 could not be created");
    handle = 0;
    *errorh = 1;
  } else {
    int flag = 1;
    int result = setsockopt(handle, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
                            sizeof(int));
    if (result < 0) {
      perror("Couldn't set TCP_NODELAY");
      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    $0x50,%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    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%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    0x2dca(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
cmpl   $0xffffffff,-0x20(%rbp)
jne    122d <tcpStart+0xad>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    1340 <tcpStart+0x1c0>
movl   $0x1,-0x40(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1289 <tcpStart+0x109>
lea    0xdae(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    133b <tcpStart+0x1bb>
mov    0x2d48(%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    12fa <tcpStart+0x17a>
lea    0xd56(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    1336 <tcpStart+0x1b6>
mov    -0x20(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1331 <tcpStart+0x1b1>
lea    0xd1f(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    1336 <tcpStart+0x1b6>
jmp    133b <tcpStart+0x1bb>
jmp    1340 <tcpStart+0x1c0>
mov    -0x20(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpStart>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
movl   $0x0,(%rdx)
lea    0x8(%rsp),%rdi
mov    $0x10,%esi
call   1090 <bzero@plt>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
mov    %rax,%r12
mov    0x2e1a(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
movl   $0x1,0x4(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%ebp
je     12a3 <tcpStart+0x123>
movl   $0x1,(%rsp)
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x4,%r8d
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <setsockopt@plt>
test   %eax,%eax
js     12b8 <tcpStart+0x138>
mov    0x0(%r13),%eax
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r12),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%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     12c1 <tcpStart+0x141>
mov    %ebp,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %eax,%eax
jne    12ca <tcpStart+0x14a>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd56(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,(%r14)
jmp    1292 <tcpStart+0x112>
lea    0xd5d(%rip),%rdi
jmp    12d1 <tcpStart+0x151>
lea    0xd6d(%rip),%rdi
jmp    12d1 <tcpStart+0x151>
lea    0xd69(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,(%r14)
xor    %ebp,%ebp
jmp    1290 <tcpStart+0x110>

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 = [28 x i8] c"Socket could not be created\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.1 = [25 x i8] c"Couldn't set TCP_NODELAY\00"
@.str.2 = [5 x i8] c"Bind\00"
@.str.3 = [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
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%14 = load i32*, i32** %6
store i32 0, i32* %14
%15 = bitcast %struct.sockaddr_in* %10 to i8*
%16 = call i32 @bzero(i8* %15, i32 16)
%17 = load i8*, i8** %4
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %9
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %8
store i32 1, i32* %11
%22 = load i32, i32* %8
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %22, i32 %23, i32 %24, i32* %11, i64 4)
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %3
%29 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %8
%30 = load i32*, i32** %6
store i32 1, i32* %30
br label %76
31: ; preds = %3
store i32 1, i32* %12
%32 = load i32, i32* %8
%33 = load i32, i32* @IPPROTO_TCP
%34 = load i32, i32* @TCP_NODELAY
%35 = bitcast i32* %12 to i8*
%36 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %32, i32 %33, i32 %34, i8* %35, i64 4)
store i32 %36, i32* %13
%37 = load i32, i32* %13
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %8
%41 = load i32*, i32** %6
store i32 1, i32* %41
br label %75
42: ; preds = %31
%43 = load i32, i32* @AF_INET
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 3
store i32 %43, i32* %44
%45 = load i32, i32* %5
%46 = call i32 @htons(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %46, i32* %47
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%49 = load %struct.hostent*, %struct.hostent** %9
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 0
%51 = load i64, i64* %50
%52 = inttoptr i64 %51 to %struct.in_addr*
%53 = bitcast %struct.in_addr* %48 to i8*
%54 = bitcast %struct.in_addr* %52 to i8*
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%56 = call i32 @bzero(i8* %55, i32 8)
%57 = load i32, i32* %8
%58 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%59 = call i32 @bind(i32 %57, %struct.sockaddr* %58, i32 4)
store i32 %59, i32* %7
%60 = load i32, i32* %7
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %42
%63 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %8
%64 = load i32*, i32** %6
store i32 1, i32* %64
br label %74
65: ; preds = %42
%66 = load i32, i32* %8
%67 = call i32 @listen(i32 %66, i32 5)
store i32 %67, i32* %7
%68 = load i32, i32* %7
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %65
%71 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %8
%72 = load i32*, i32** %6
store i32 1, i32* %72
br label %73
73: ; preds = %70, %65
br label %74
74: ; preds = %73, %62
br label %75
75: ; preds = %74, %39
br label %76
76: ; preds = %75, %28
%77 = load i32, i32* %8
ret i32 %77
}

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 = [28 x i8] c"Socket could not be created\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.1 = [25 x i8] c"Couldn't set TCP_NODELAY\00"
@.str.2 = [5 x i8] c"Bind\00"
@.str.3 = [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
%6 = alloca i32
store i32 0, i32* %2
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%8 = call i32 @bzero(i8* %7, i32 16)
%9 = tail call %struct.hostent* @gethostbyname(i8* %0)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %12, i32 %14, i32 %15, i32* %5, i64 4)
%17 = icmp eq i32 %12, -1
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %48
20: ; preds = %3
%21 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%22 = load i32, i32* @IPPROTO_TCP
%23 = load i32, i32* @TCP_NODELAY
%24 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %12, i32 %22, i32 %23, i32* %6, i64 4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %43, label %26
26: ; preds = %20
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 %27, i32* %28
%29 = call i32 @htons(i32 %1)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%32 = load i64, i64* %31
%33 = inttoptr i64 %32 to i32*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%35 = load i32, i32* %33
store i32 %35, i32* %34
%36 = call i32 @bzero(i8* %7, i32 8)
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i32 @bind(i32 %12, %struct.sockaddr* %37, i32 4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %43, label %40
40: ; preds = %26
%41 = call i32 @listen(i32 %12, i32 5)
%42 = icmp eq i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %40, %26, %20
%44 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %20 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %40 ]
%45 = call i32 @perror(i8* %44)
store i32 1, i32* %2
br label %46
46: ; preds = %43, %40
%47 = phi i32 [ %12, %40 ], [ 0, %43 ]
br label %48
48: ; preds = %46, %18
%49 = phi i32 [ 0, %18 ], [ %47, %46 ]
ret i32 %49
}