#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 sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_adr;
typedef int /*<<< orphan*/ message;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error_handling(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sock;
  char message[30];
  int str_len;
  struct sockaddr_in serv_adr;
  if (argc != 3) {
    printf("Usage: %s <IP> <port>\n", argv[0]);
    exit(1);
  }
  sock = socket(PF_INET, SOCK_DGRAM, 0);
  if (sock == -1)
    error_handling("socket() error");
  memset(&serv_adr, 0, sizeof(serv_adr));
  serv_adr.sin_family = AF_INET;
  serv_adr.sin_addr.s_addr = inet_addr(argv[1]);
  serv_adr.sin_port = htons(atoi(argv[2]));
  connect(sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr));
  while (1) {
    fputs("Insert message(q to quit): ", stdout);
    fgets(message, sizeof(message), stdin);
    if (!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
      break;
    write(sock, message, strlen(message));
    str_len = read(sock, message, sizeof(message) - 1);
    message[str_len] = 0;
    printf("Message from server: %s", message);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     123d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdd2(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    126f <main+0x6f>
lea    0xdad(%rip),%rdi
call   10e0 <error_handling@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <inet_addr@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%esi
lea    0xd53(%rip),%rdi
call   1070 <fputs@plt>
lea    -0x40(%rbp),%rdi
mov    0x2cfd(%rip),%rax
mov    (%rax),%edx
mov    $0x1e,%esi
call   10b0 <fgets@plt>
lea    -0x40(%rbp),%rdi
lea    0xd48(%rip),%rsi
call   10d0 <strcmp@plt>
cmp    $0x0,%eax
je     1321 <main+0x121>
lea    -0x40(%rbp),%rdi
lea    0xd32(%rip),%rsi
call   10d0 <strcmp@plt>
cmp    $0x0,%eax
jne    1326 <main+0x126>
jmp    137d <main+0x17d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1d,%edx
call   10a0 <read@plt>
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xcd5(%rip),%rdi
call   1060 <printf@plt>
jmp    12c3 <main+0xc3>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1386 <main+0x196>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1236 <main+0x46>
lea    0xde6(%rip),%rdi
call   10d0 <error_handling@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbx),%rdi
call   10c0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    0x10(%rbx),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
lea    0xd92(%rip),%rdi
call   1070 <fputs@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x1e,%esi
call   10b0 <fgets@plt>
mov    $0xa71,%r12d
mov    0x10(%rsp),%eax
xor    %r12d,%eax
movzbl 0x12(%rsp),%ecx
or     %ax,%cx
je     136e <main+0x17e>
mov    $0xa51,%r13d
lea    0x10(%rsp),%rbx
lea    0xd6a(%rip),%r14
lea    0xd41(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rsp),%eax
xor    %r13d,%eax
movzbl 0x12(%rsp),%ecx
or     %ax,%cx
je     136e <main+0x17e>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1d,%edx
call   10a0 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    0x2c83(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <fputs@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1e,%esi
call   10b0 <fgets@plt>
mov    0x10(%rsp),%eax
xor    %r12d,%eax
movzbl 0x12(%rsp),%ecx
or     %ax,%cx
jne    12f0 <main+0x100>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xc70(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [23 x i8] c"Usage: %s <IP> <port>\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [15 x i8] c"socket() error\00"
@AF_INET = global i32 0
@.str.2 = [28 x i8] c"Insert message(q to quit): \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.3 = [3 x i8] c"q\0A\00"
@.str.4 = [3 x i8] c"Q\0A\00"
@.str.5 = [24 x i8] c"Message from server: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [30 x i8]
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @error_handling(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @inet_addr(i8* %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %6
%43 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%44 = call i32 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
br label %45
45: ; preds = %26, %59
%46 = load i32, i32* @stdout
%47 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %46)
%48 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%49 = load i32, i32* @stdin
%50 = call i32 @fgets(i8* %48, i32 30, i32 %49)
%51 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%52 = call i32 @strcmp(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %45
%55 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%56 = call i32 @strcmp(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %54, %45
br label %73
59: ; preds = %54
%60 = load i32, i32* %6
%61 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%62 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%63 = call i32 @strlen(i8* %62)
%64 = call i32 @write(i32 %60, i8* %61, i32 %63)
%65 = load i32, i32* %6
%66 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%67 = call i32 @read(i32 %65, i8* %66, i32 29)
store i32 %67, i32* %8
%68 = load i32, i32* %8
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 %69
store i8 0, i8* %70
%71 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%72 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %71)
br label %45
73: ; preds = %58
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [23 x i8] c"Usage: %s <IP> <port>\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [15 x i8] c"socket() error\00"
@AF_INET = global i32 0
@.str.2 = [28 x i8] c"Insert message(q to quit): \00"
@stdout = global i32 0
@stdin = global i32 0
@.str.3 = [3 x i8] c"q\0A\00"
@.str.4 = [3 x i8] c"Q\0A\00"
@.str.5 = [24 x i8] c"Message from server: %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [30 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @error_handling(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %21, i32* %22
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @inet_addr(i8* %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%33 = call i32 @connect(i32 %15, %struct.sockaddr* %32, i32 12)
%34 = load i32, i32* @stdout
%35 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @stdin
%37 = call i32 @fgets(i8* %5, i32 30, i32 %36)
%38 = call i32 @bcmp(i8* dereferenceable(3) %5, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 3)
%39 = icmp eq i32 %38, 0
br i1 %39, label %56, label %40
40: ; preds = %19, %43
%41 = call i32 @bcmp(i8* dereferenceable(3) %5, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i64 3)
%42 = icmp eq i32 %41, 0
br i1 %42, label %56, label %43
43: ; preds = %40
%44 = call i32 @strlen(i8* %5)
%45 = call i32 @write(i32 %15, i8* %5, i32 %44)
%46 = call i32 @read(i32 %15, i8* %5, i32 29)
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%50 = load i32, i32* @stdout
%51 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = load i32, i32* @stdin
%53 = call i32 @fgets(i8* %5, i32 30, i32 %52)
%54 = call i32 @bcmp(i8* dereferenceable(3) %5, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 3)
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %40
56: ; preds = %40, %43, %19
%57 = call i32 @close(i32 %15)
ret i32 0
}