#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 hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ handle;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr_in *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *);
int recvMSG(int);
int sendMSG(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(int argc, char *argv[]) {
  int sockfd, portno, quit;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char handle[256];
  if (argc < 3) {
    fprintf(stderr, "usage %s error requires 3 arguments\n", argv[0]);
    exit(0);
  }
  printf("What is your handle?\n");
  fgets(handle, sizeof(handle), stdin);
  handle[strlen(handle) - 1] = '\0';
  portno = atoi(argv[2]);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  server = gethostbyname(argv[1]);
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(0);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(portno);
  if (connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR connecting");
  printf("Now connected\n");
  printf("Please enter the message: please type \"quit\" to exit chat.\n");
  quit = 0;
  while (quit != 1) {
    quit = sendMSG(sockfd, handle);
    if (quit != 1)
      quit = recvMSG(sockfd);
  }
  printf("Connection closed\n");
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1248 <main+0x48>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    0xdd6(%rip),%rdi
call   1070 <printf@plt>
lea    -0x130(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1090 <fgets@plt>
lea    -0x130(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x130(%rbp,%rax,1)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12c9 <main+0xc9>
lea    0xd77(%rip),%rdi
call   10d0 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1303 <main+0x103>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
lea    0xd5b(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10c0 <bcopy@plt>
mov    -0x18(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jge    136a <main+0x16a>
lea    0xd00(%rip),%rdi
call   10d0 <error@plt>
lea    0xd05(%rip),%rdi
call   1070 <printf@plt>
lea    0xd08(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
je     13bf <main+0x1bf>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
call   1030 <sendMSG@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
je     13ba <main+0x1ba>
mov    -0x14(%rbp),%edi
call   1040 <recvMSG@plt>
mov    %eax,-0x1c(%rbp)
jmp    1389 <main+0x189>
lea    0xcfb(%rip),%rdi
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jle    1350 <main+0x150>
lea    0xe3e(%rip),%rdi
call   1040 <puts@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x10(%r14),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r15d
mov    0x2d84(%rip),%r12
mov    (%r12),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    128a <main+0x8a>
lea    0xda0(%rip),%rdi
call   10d0 <error@plt>
mov    0x8(%r14),%rdi
call   10a0 <gethostbyname@plt>
test   %rax,%rax
je     1371 <main+0x171>
mov    %rax,%rbp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbp),%rsi
lea    0x4(%rsp),%rdi
mov    0x0(%rbp),%edx
call   10c0 <memmove@plt>
mov    %r15d,%edi
call   1070 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
test   %rax,%rax
jns    12f3 <main+0xf3>
lea    0xd61(%rip),%rdi
call   10d0 <error@plt>
lea    0xd7b(%rip),%rdi
call   1040 <puts@plt>
lea    0xd7d(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <sendMSG@plt>
cmp    $0x1,%eax
je     132b <main+0x12b>
mov    %ebx,%edi
call   1050 <recvMSG@plt>
cmp    $0x1,%eax
jne    1310 <main+0x110>
lea    0xd8c(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xc9d(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    0x2c58(%rip),%rax
mov    (%rax),%edi
lea    0xcb9(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
@stderr = global i32 0
@.str = [37 x i8] c"usage %s error requires 3 arguments\0A\00"
@.str.1 = [22 x i8] c"What is your handle?\0A\00"
@stdin = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"ERROR opening socket\00"
@.str.3 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.4 = [17 x i8] c"ERROR connecting\00"
@.str.5 = [15 x i8] c"Now connected\0A\00"
@.str.6 = [60 x i8] c"Please enter the message: please type \22quit\22 to exit chat.\0A\00"
@.str.7 = [19 x i8] c"Connection closed\0A\00"
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 %struct.sockaddr_in
%10 = alloca %struct.hostent*
%11 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %23, i32 256, i32 %24)
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %29
store i8 0, i8* %30
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %7
%35 = load i32, i32* @AF_INET
%36 = load i32, i32* @SOCK_STREAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %21
%41 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %21
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call %struct.hostent* @gethostbyname(i8* %45)
store %struct.hostent* %46, %struct.hostent** %10
%47 = load %struct.hostent*, %struct.hostent** %10
%48 = icmp eq %struct.hostent* %47, null
br i1 %48, label %49, label %53
49: ; preds = %42
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %42
%54 = bitcast %struct.sockaddr_in* %9 to i8*
%55 = call i32 @bzero(i8* %54, i32 12)
%56 = load i32, i32* @AF_INET
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %56, i32* %57
%58 = load %struct.hostent*, %struct.hostent** %10
%59 = getelementptr inbounds %struct.hostent, %struct.hostent* %58, i32 0, i32 1
%60 = load i64, i64* %59
%61 = inttoptr i64 %60 to i8*
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%63 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %62, i32 0, i32 0
%64 = bitcast i32* %63 to i8*
%65 = load %struct.hostent*, %struct.hostent** %10
%66 = getelementptr inbounds %struct.hostent, %struct.hostent* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i32 @bcopy(i8* %61, i8* %64, i32 %67)
%69 = load i32, i32* %7
%70 = call i32 @htons(i32 %69)
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %70, i32* %71
%72 = load i32, i32* %6
%73 = call i64 @connect(i32 %72, %struct.sockaddr_in* %9, i32 12)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %53
%76 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %77
77: ; preds = %75, %53
%78 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%79 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %8
br label %80
80: ; preds = %92, %77
%81 = load i32, i32* %8
%82 = icmp ne i32 %81, 1
br i1 %82, label %83, label %93
83: ; preds = %80
%84 = load i32, i32* %6
%85 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%86 = call i32 @sendMSG(i32 %84, i8* %85)
store i32 %86, i32* %8
%87 = load i32, i32* %8
%88 = icmp ne i32 %87, 1
br i1 %88, label %89, label %92
89: ; preds = %83
%90 = load i32, i32* %6
%91 = call i32 @recvMSG(i32 %90)
store i32 %91, i32* %8
br label %92
92: ; preds = %89, %83
br label %80
93: ; preds = %80
%94 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%95 = load i32, i32* %6
%96 = call i32 @close(i32 %95)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
@stderr = global i32 0
@.str = [37 x i8] c"usage %s error requires 3 arguments\0A\00"
@stdin = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"ERROR opening socket\00"
@.str.3 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.4 = [17 x i8] c"ERROR connecting\00"
@str = [21 x i8] c"What is your handle?\00"
@str.8 = [14 x i8] c"Now connected\00"
@str.9 = [59 x i8] c"Please enter the message: please type \22quit\22 to exit chat.\00"
@str.10 = [18 x i8] c"Connection closed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %6, i32 256, i32 %15)
%17 = call i32 @strlen(i8* %6)
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %19
store i8 0, i8* %20
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %13
%29 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %13
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = call %struct.hostent* @gethostbyname(i8* %32)
%34 = icmp eq %struct.hostent* %33, null
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %30
%40 = call i32 @bzero(i8* %5, i32 12)
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i64 0, i32 1
%44 = load i64, i64* %43
%45 = inttoptr i64 %44 to i8*
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%47 = bitcast i32* %46 to i8*
%48 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i64 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @htons(i32 %23)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %50, i32* %51
%52 = call i64 @connect(i32 %26, %struct.sockaddr_in* %3, i32 12)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %39
%55 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %39
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.9, i64 0, i64 0))
br label %59
59: ; preds = %56, %62
%60 = call i32 @sendMSG(i32 %26, i8* %6)
%61 = icmp eq i32 %60, 1
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = call i32 @recvMSG(i32 %26)
%64 = icmp eq i32 %63, 1
br i1 %64, label %65, label %59
65: ; preds = %59, %62
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.10, i64 0, i64 0))
%67 = call i32 @close(i32 %26)
ret i32 0
}