#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork_str_cli(int /*<<< orphan*/, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  pid_t pid;
  int sockfd, connfd;
  socklen_t clilen;
  struct sockaddr_in servaddr, cliaddr;
  if (2 != argc) {
    printf("USAGE : %s <hostname>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(10001);
  inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("connect");
    exit(EXIT_FAILURE);
  }
  fork_str_cli(stdin, sockfd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11d5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1214 <main+0x84>
lea    0xe16(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x2711,%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <inet_pton@plt>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    128a <main+0xfa>
lea    0xda7(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
call   1060 <fork_str_cli@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11c9 <main+0x39>
mov    0x2e3f(%rip),%r15
mov    (%r15),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
jns    11da <main+0x4a>
lea    0xe50(%rip),%rdi
jmp    122d <main+0x9d>
mov    (%rbx),%rsi
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
jmp    1232 <main+0xa2>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    $0x2711,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%r15),%edi
mov    0x8(%rbx),%rsi
mov    %r14,%rdx
call   1050 <inet_pton@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    1240 <main+0xb0>
lea    0xdf1(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
call   1060 <fork_str_cli@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [23 x i8] c"USAGE : %s <hostname>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@stdin = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 2, %12
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
%31 = call i32 @bzero(%struct.sockaddr_in* %10, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %32, i32* %33
%34 = call i32 @htons(i32 10001)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %34, i32* %35
%36 = load i32, i32* @AF_INET
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%41 = call i32 @inet_pton(i32 %36, i8* %39, i32* %40)
%42 = load i32, i32* %7
%43 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%44 = call i64 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %30
%47 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %30
%51 = load i32, i32* @stdin
%52 = load i32, i32* %7
%53 = call i32 @fork_str_cli(i32 %51, i32 %52)
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @exit(i32 %54)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [23 x i8] c"USAGE : %s <hostname>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@stdin = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %11
%21 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = tail call i32 @htons(i32 10001)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%30 = call i32 @inet_pton(i32 %26, i8* %28, i32* %29)
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @connect(i32 %14, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %20
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %20
%39 = load i32, i32* @stdin
%40 = call i32 @fork_str_cli(i32 %39, i32 %14)
%41 = load i32, i32* @EXIT_SUCCESS
%42 = call i32 @exit(i32 %41)
unreachable
}