#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socketTCP(char *ip, unsigned short port, struct sockaddr_in *tcp_addr) {
  int sockfd = socket(PF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket");
    exit(1);
  }
  if (tcp_addr == 0)
    return sockfd;
  memset(tcp_addr, 0, sizeof(struct sockaddr_in));
  tcp_addr->sin_family = AF_INET;
  tcp_addr->sin_port = htons(port);
  tcp_addr->sin_addr.s_addr = inet_addr(ip);
  if (ip == 0) {
    if (bind(sockfd, (struct sockaddr *)tcp_addr, sizeof(struct sockaddr_in)) <
        0) {
      perror("bind");
      exit(1);
    }
  } else {
    if (connect(sockfd, (struct sockaddr *)tcp_addr,
                sizeof(struct sockaddr_in)) < 0) {
      perror("connect");
      exit(1);
    }
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<socketTCP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11d3 <socketTCP+0x53>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11e9 <socketTCP+0x69>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a9 <socketTCP+0x129>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
jne    126d <socketTCP+0xed>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1268 <socketTCP+0xe8>
lea    0xdae(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12a3 <socketTCP+0x123>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    129e <socketTCP+0x11e>
lea    0xd7d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12a3 <socketTCP+0x123>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketTCP>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     121f <socketTCP+0x9f>
mov    %eax,%ebp
test   %rbx,%rbx
je     1212 <socketTCP+0x92>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
movzwl %r15w,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
test   %r14,%r14
je     1208 <socketTCP+0x88>
call   1090 <connect@plt>
test   %rax,%rax
jns    1212 <socketTCP+0x92>
lea    0xe06(%rip),%rdi
jmp    122f <socketTCP+0xaf>
call   1060 <bind@plt>
test   %rax,%rax
js     1228 <socketTCP+0xa8>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdda(%rip),%rdi
jmp    122f <socketTCP+0xaf>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @socketTCP(i8* %0, i16 zeroext %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16
%7 = alloca %struct.sockaddr_in*
%8 = alloca i32
store i8* %0, i8** %5
store i16 %1, i16* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%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* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%19 = icmp eq %struct.sockaddr_in* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32, i32* %8
store i32 %21, i32* %4
br label %61
22: ; preds = %17
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%24 = call i32 @memset(%struct.sockaddr_in* %23, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 2
store i32 %25, i32* %27
%28 = load i16, i16* %6
%29 = call i32 @htons(i16 zeroext %28)
%30 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %30, i32 0, i32 1
store i32 %29, i32* %31
%32 = load i8*, i8** %5
%33 = call i32 @inet_addr(i8* %32)
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %33, i32* %36
%37 = load i8*, i8** %5
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %49
39: ; preds = %22
%40 = load i32, i32* %8
%41 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%42 = bitcast %struct.sockaddr_in* %41 to %struct.sockaddr*
%43 = call i64 @bind(i32 %40, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %39
br label %59
49: ; preds = %22
%50 = load i32, i32* %8
%51 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%52 = bitcast %struct.sockaddr_in* %51 to %struct.sockaddr*
%53 = call i64 @connect(i32 %50, %struct.sockaddr* %52, i32 12)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %49
br label %59
59: ; preds = %58, %48
%60 = load i32, i32* %8
store i32 %60, i32* %4
br label %61
61: ; preds = %59, %20
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @socketTCP(i8* %0, i16 zeroext %1, %struct.sockaddr_in* %2) {
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = icmp eq %struct.sockaddr_in* %2, null
br i1 %12, label %35, label %13
13: ; preds = %11
%14 = tail call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i16 zeroext %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = tail call i32 @inet_addr(i8* %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = icmp eq i8* %0, null
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br i1 %21, label %23, label %29
23: ; preds = %13
%24 = tail call i64 @bind(i32 %6, %struct.sockaddr* %22, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %13
%30 = tail call i64 @connect(i32 %6, %struct.sockaddr* %22, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %23, %29, %11
ret i32 %6
}