#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;
};
typedef int /*<<< orphan*/ serv;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int main() {
  int sock_fd;
  struct sockaddr_in serv;
  int servlen, n;
  char buff[256];
  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock_fd <= 0) {
    perror("create socket error!");
    return 0;
  }
  perror("Create socket");
  serv.sin_family = AF_INET;
  serv.sin_port = htons(9090);
  serv.sin_addr.s_addr = INADDR_ANY;
  servlen = sizeof(serv);
  strcpy(buff, "time");
  if (-1 == sendto(sock_fd, buff, sizeof(buff), 0, (struct sockaddr *)&serv,
                   servlen)) {
    perror("send data");
    close(sock_fd);
    return 0;
  }
  printf("send time request\n");
  n = recvfrom(sock_fd, buff, sizeof(buff), 0, (struct sockaddr *)&serv,
               &servlen);
  if (n <= 0) {
    perror("recv data!\n");
    close(sock_fd);
    return 0;
  }
  buff[n] = '\0';
  printf("time from server: %s", buff);
  strcpy(buff, "quit");
  if (-1 == sendto(sock_fd, buff, sizeof(buff), 0, (struct sockaddr *)&serv,
                   servlen)) {
    perror("send data");
    close(sock_fd);
    return 0;
  }
  printf("send quit command\n");
  close(sock_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     11d0 <main+0x50>
lea    0xe41(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    135f <main+0x1df>
lea    0xe3e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x2382,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0xc,-0x1c(%rbp)
lea    -0x120(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    $0x100,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1268 <main+0xe8>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    135f <main+0x1df>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x18(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x1c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     12c1 <main+0x141>
lea    0xd9d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    135f <main+0x1df>
movslq -0x20(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rsi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
lea    0xd76(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    $0x100,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1342 <main+0x1c2>
lea    0xcff(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    135f <main+0x1df>
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x120,%rsp
mov    0x2e46(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
jg     11c9 <main+0x39>
lea    0xe41(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c5 <main+0x135>
mov    %eax,%ebp
lea    0xe43(%rip),%rdi
call   10a0 <perror@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x2382,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movl   $0xc,0xc(%rsp)
lea    0xe1c(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%r8
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
cmp    $0xffffffff,%eax
je     12d3 <main+0x143>
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebp,%edi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
test   %eax,%eax
jle    12e1 <main+0x151>
mov    %eax,%eax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xdca(%rip),%rdi
lea    0x20(%rsp),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdc9(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    0xc(%rsp),%r9d
lea    0x10(%rsp),%r8
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1070 <sendto@plt>
cmp    $0xffffffff,%eax
je     12d3 <main+0x143>
lea    0xdb1(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd4e(%rip),%rdi
call   10a0 <perror@plt>
jmp    12be <main+0x12e>
lea    0xd4a(%rip),%rdi
call   10a0 <perror@plt>
jmp    12be <main+0x12e>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [21 x i8] c"create socket error!\00"
@.str.1 = [14 x i8] c"Create socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"time\00"
@.str.3 = [10 x i8] c"send data\00"
@.str.4 = [19 x i8] c"send time request\0A\00"
@.str.5 = [12 x i8] c"recv data!\0A\00"
@.str.6 = [21 x i8] c"time from server: %s\00"
@.str.7 = [5 x i8] c"quit\00"
@.str.8 = [19 x i8] c"send quit command\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
store i32 0, i32* %1
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %69
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 9090)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
store i32 12, i32* %4
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = load i32, i32* %4
%29 = call i32 @sendto(i32 %25, i8* %26, i32 256, i32 0, %struct.sockaddr* %27, i32 %28)
%30 = icmp eq i32 -1, %29
br i1 %30, label %31, label %35
31: ; preds = %14
%32 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
store i32 0, i32* %1
br label %69
35: ; preds = %14
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* %2
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @recvfrom(i32 %37, i8* %38, i32 256, i32 0, %struct.sockaddr* %39, i32* %4)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
store i32 0, i32* %1
br label %69
47: ; preds = %35
%48 = load i32, i32* %5
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %49
store i8 0, i8* %50
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %51)
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%54 = call i32 @strcpy(i8* %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%55 = load i32, i32* %2
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%57 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%58 = load i32, i32* %4
%59 = call i32 @sendto(i32 %55, i8* %56, i32 256, i32 0, %struct.sockaddr* %57, i32 %58)
%60 = icmp eq i32 -1, %59
br i1 %60, label %61, label %65
61: ; preds = %47
%62 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
store i32 0, i32* %1
br label %69
65: ; preds = %47
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
%67 = load i32, i32* %2
%68 = call i32 @close(i32 %67)
store i32 0, i32* %1
br label %69
69: ; preds = %65, %61, %43, %31, %12
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [21 x i8] c"create socket error!\00"
@.str.1 = [14 x i8] c"Create socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"time\00"
@.str.3 = [10 x i8] c"send data\00"
@.str.5 = [12 x i8] c"recv data!\0A\00"
@.str.6 = [21 x i8] c"time from server: %s\00"
@.str.7 = [5 x i8] c"quit\00"
@str = [18 x i8] c"send time request\00"
@str.9 = [18 x i8] c"send quit command\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %49
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 9090)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
store i32 12, i32* %2
%21 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%23 = call i32 @sendto(i32 %9, i8* %6, i32 256, i32 0, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %13
%26 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @close(i32 %9)
br label %49
28: ; preds = %13
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%30 = call i32 @recvfrom(i32 %9, i8* %6, i32 256, i32 0, %struct.sockaddr* %22, i32* %2)
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%34 = call i32 @close(i32 %9)
br label %49
35: ; preds = %28
%36 = zext i32 %30 to i64
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%39 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%40 = load i32, i32* %2
%41 = call i32 @sendto(i32 %9, i8* %6, i32 256, i32 0, %struct.sockaddr* %22, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @close(i32 %9)
br label %49
46: ; preds = %35
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
%48 = call i32 @close(i32 %9)
br label %49
49: ; preds = %46, %43, %32, %25, %11
ret i32 0
}