#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*/ srvAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, scalar_t__, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
scalar_t__ strlen(char *);
int main(int argc, char **argv) {
  if (argc != 2)
    fprintf(stderr, "Usage: %s [Server IP]\n", argv[0]);
  int sockFd;
  if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    perror("socket() error");
  struct sockaddr_in srvAddr;
  bzero(&srvAddr, sizeof(srvAddr));
  srvAddr.sin_family = AF_INET;
  srvAddr.sin_port = htons(57345);
  if (inet_pton(AF_INET, argv[1], &srvAddr.sin_addr) <= 0)
    perror("inet_pton() error");
  if (connect(sockFd, (const struct sockaddr *)&srvAddr, sizeof(srvAddr)) < 0)
    perror("connect() error");
  char buf[4096];
  while (fgets(buf, 4096, stdin) != NULL) {
    if (!strcmp(buf, "\n"))
      break;
    if (send(sockFd, buf, strlen(buf) + 1, 0) < 0)
      perror("send() error");
    if (recv(sockFd, buf, 4096, 0) < 0)
      perror("recv() error");
    fputs(buf, stdout);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120f <main+0x3f>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf6(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1240 <main+0x70>
lea    0xddc(%rip),%rdi
call   10c0 <perror@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xe001,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <inet_pton@plt>
cmp    $0x0,%rax
jg     1297 <main+0xc7>
lea    0xd94(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    12be <main+0xee>
lea    0xd7f(%rip),%rdi
call   10c0 <perror@plt>
jmp    12c3 <main+0xf3>
lea    -0x1020(%rbp),%rdi
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     13a2 <main+0x1d2>
lea    -0x1020(%rbp),%rdi
lea    0xd20(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1308 <main+0x138>
jmp    13a2 <main+0x1d2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x102c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
lea    -0x1020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x102c(%rbp),%edi
mov    -0x1028(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0x0,%rax
jge    135c <main+0x18c>
lea    0xcf1(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jge    1388 <main+0x1b8>
lea    0xcd2(%rip),%rdi
call   10c0 <perror@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2c2a(%rip),%rax
mov    (%rax),%esi
call   1070 <fputs@plt>
jmp    12c3 <main+0xf3>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
je     11f1 <main+0x31>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe14(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2df0(%rip),%r15
mov    (%r15),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1347 <main+0x187>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0xe001,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edi
mov    0x8(%r14),%rsi
mov    %rbx,%rdx
call   10a0 <inet_pton@plt>
test   %rax,%rax
jle    1358 <main+0x198>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     1369 <main+0x1a9>
mov    0x2d6f(%rip),%r12
mov    (%r12),%edx
lea    0x10(%rsp),%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     1333 <main+0x173>
cmpw   $0xa,0x10(%rsp)
je     1333 <main+0x173>
lea    0x10(%rsp),%rbx
lea    0xda3(%rip),%r14
lea    0xda9(%rip),%r15
mov    0x2d0d(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1060 <send@plt>
test   %rax,%rax
js     131f <main+0x15f>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     1329 <main+0x169>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1070 <fputs@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     1333 <main+0x173>
cmpw   $0xa,0x10(%rsp)
jne    12c0 <main+0x100>
jmp    1333 <main+0x173>
mov    %r14,%rdi
call   10b0 <perror@plt>
jmp    12dd <main+0x11d>
mov    %r15,%rdi
call   10b0 <perror@plt>
jmp    12f3 <main+0x133>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcc9(%rip),%rdi
call   10b0 <perror@plt>
jmp    1215 <main+0x55>
lea    0xcc7(%rip),%rdi
call   10b0 <perror@plt>
jmp    1252 <main+0x92>
lea    0xcc8(%rip),%rdi
call   10b0 <perror@plt>
jmp    126a <main+0xaa>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s [Server IP]\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [15 x i8] c"socket() error\00"
@.str.2 = [18 x i8] c"inet_pton() error\00"
@.str.3 = [16 x i8] c"connect() error\00"
@stdin = global i32 0
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [13 x i8] c"send() error\00"
@.str.6 = [13 x i8] c"recv() error\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %11, %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %26, i32* %27
%28 = call i32 @htons(i32 57345)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* @AF_INET
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%35 = call i64 @inet_pton(i32 %30, i8* %33, i32* %34)
%36 = icmp sle i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %24
%38 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %24
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i64 @connect(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
br label %47
47: ; preds = %74, %46
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%49 = load i32, i32* @stdin
%50 = call i32* @fgets(i8* %48, i32 4096, i32 %49)
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %78
52: ; preds = %47
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%54 = call i32 @strcmp(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %52
br label %78
57: ; preds = %52
%58 = load i32, i32* %6
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%61 = call i64 @strlen(i8* %60)
%62 = add nsw i64 %61, 1
%63 = call i64 @send(i32 %58, i8* %59, i64 %62, i32 0)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %57
%66 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %67
67: ; preds = %65, %57
%68 = load i32, i32* %6
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%70 = call i64 @recv(i32 %68, i8* %69, i32 4096, i32 0)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %74
74: ; preds = %72, %67
%75 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%76 = load i32, i32* @stdout
%77 = call i32 @fputs(i8* %75, i32 %76)
br label %47
78: ; preds = %56, %47
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s [Server IP]\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [15 x i8] c"socket() error\00"
@.str.2 = [18 x i8] c"inet_pton() error\00"
@.str.3 = [16 x i8] c"connect() error\00"
@stdin = global i32 0
@.str.5 = [13 x i8] c"send() error\00"
@.str.6 = [13 x i8] c"recv() error\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [4096 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %6, %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = bitcast %struct.sockaddr_in* %3 to i8*
%19 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 57345)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%28 = call i64 @inet_pton(i32 %24, i8* %26, i32* %27)
%29 = icmp slt i64 %28, 1
br i1 %29, label %30, label %32
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %17
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @connect(i32 %13, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%40 = load i32, i32* @stdin
%41 = call i32* @fgets(i8* %39, i32 4096, i32 %40)
%42 = icmp eq i32* %41, null
%43 = bitcast [4096 x i8]* %4 to i16*
%44 = load i16, i16* %43
%45 = icmp eq i16 %44, 10
%46 = select i1 %42, i1 true, i1 %45
br i1 %46, label %68, label %47
47: ; preds = %38, %59
%48 = call i64 @strlen(i8* %39)
%49 = add nsw i64 %48, 1
%50 = call i64 @send(i32 %13, i8* %39, i64 %49, i32 0)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %47
%55 = call i64 @recv(i32 %13, i8* %39, i32 4096, i32 0)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %57, %54
%60 = load i32, i32* @stdout
%61 = call i32 @fputs(i8* %39, i32 %60)
%62 = load i32, i32* @stdin
%63 = call i32* @fgets(i8* %39, i32 4096, i32 %62)
%64 = icmp eq i32* %63, null
%65 = load i16, i16* %43
%66 = icmp eq i16 %65, 10
%67 = select i1 %64, i1 true, i1 %66
br i1 %67, label %68, label %47
68: ; preds = %59, %38
ret i32 0
}