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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Close(int);
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ STDOUT_FILENO;
int Socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ Write(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perr_exit(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
void udp_client(char *no_use) {
  struct sockaddr_in servaddr;
  int sockfd, n;
  char buf[80];
  sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
  servaddr.sin_port = htons(8000);
  while (fgets(buf, 80, stdin) != NULL) {
    n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr,
               sizeof(servaddr));
    if (n == -1)
      perr_exit("sendto error");
    n = recvfrom(sockfd, buf, 80, 0, NULL, 0);
    if (n == -1)
      perr_exit("recvfrom error");
    Write(STDOUT_FILENO, buf, n);
  }
  Close(sockfd);
}


x86 assembly (clang, -O0):
<udp_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <Socket@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
add    $0x4,%rdx
lea    0xdf3(%rip),%rsi
call   1080 <inet_pton@plt>
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
lea    -0x70(%rbp),%rdi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12d9 <udp_client+0x129>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x7c(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
lea    -0x18(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    128a <udp_client+0xda>
lea    0xd85(%rip),%rdi
call   10d0 <perr_exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
xor    %r9d,%r9d
xor    %eax,%eax
mov    %eax,%r8d
mov    %r9d,%ecx
call   1090 <recvfrom@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12bf <udp_client+0x10f>
lea    0xd5d(%rip),%rdi
call   10d0 <perr_exit@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <Write@plt>
jmp    121f <udp_client+0x6f>
mov    -0x1c(%rbp),%edi
call   10a0 <Close@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    0x2e1b(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <Socket@plt>
mov    %eax,%r13d
mov    %rsp,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%rbx),%edi
mov    %edi,0x8(%rsp)
lea    0x4(%rsp),%rdx
lea    0xe07(%rip),%rsi
call   1080 <inet_pton@plt>
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x2dc6(%rip),%r15
mov    (%r15),%edx
lea    0x10(%rsp),%rdi
mov    $0x50,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     12c4 <udp_client+0x114>
lea    0x10(%rsp),%rbx
mov    %rsp,%r14
mov    0x2db4(%rip),%r12
jmp    1263 <udp_client+0xb3>
xchg   %ax,%ax
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <Write@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x50,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     12c4 <udp_client+0x114>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1294 <udp_client+0xe4>
lea    0xd7b(%rip),%rdi
call   10d0 <perr_exit@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x50,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <recvfrom@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1240 <udp_client+0x90>
lea    0xd5d(%rip),%rdi
call   10d0 <perr_exit@plt>
jmp    1240 <udp_client+0x90>
mov    %r13d,%edi
call   10a0 <Close@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@stdin = global i32 0
@.str.1 = [13 x i8] c"sendto error\00"
@.str.2 = [15 x i8] c"recvfrom error\00"
@STDOUT_FILENO = global i32 0
define void @udp_client(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca [80 x i8]
store i8* %0, i8** %2
%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* %4
%10 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%15 = call i32 @inet_pton(i32 %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %14)
%16 = call i32 @htons(i32 8000)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %16, i32* %17
br label %18
18: ; preds = %42, %1
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%20 = load i32, i32* @stdin
%21 = call i32* @fgets(i8* %19, i32 80, i32 %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %47
23: ; preds = %18
%24 = load i32, i32* %4
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @sendto(i32 %24, i8* %25, i32 %27, i32 0, %struct.sockaddr* %28, i32 12)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 @perr_exit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %23
%35 = load i32, i32* %4
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%37 = call i32 @recvfrom(i32 %35, i8* %36, i32 80, i32 0, i32* null, i32 0)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perr_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
%43 = load i32, i32* @STDOUT_FILENO
%44 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%45 = load i32, i32* %5
%46 = call i32 @Write(i32 %43, i8* %44, i32 %45)
br label %18
47: ; preds = %18
%48 = load i32, i32* %4
%49 = call i32 @Close(i32 %48)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@stdin = global i32 0
@.str.1 = [13 x i8] c"sendto error\00"
@.str.2 = [15 x i8] c"recvfrom error\00"
@STDOUT_FILENO = global i32 0
define void @udp_client(i8* nocapture readnone %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [80 x i8]
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @Socket(i32 %6, i32 %7, i32 0)
%9 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%13 = call i32 @inet_pton(i32 %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %12)
%14 = call i32 @htons(i32 8000)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @stdin
%17 = call i32* @fgets(i8* %5, i32 80, i32 %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %38, label %19
19: ; preds = %1
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %21
21: ; preds = %19, %32
%22 = call i32 @strlen(i8* %5)
%23 = call i32 @sendto(i32 %8, i8* %5, i32 %22, i32 0, %struct.sockaddr* %20, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perr_exit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = call i32 @recvfrom(i32 %8, i8* %5, i32 80, i32 0, i32* null, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @perr_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i32 @Write(i32 %33, i8* %5, i32 %28)
%35 = load i32, i32* @stdin
%36 = call i32* @fgets(i8* %5, i32 80, i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %21
38: ; preds = %32, %1
%39 = call i32 @Close(i32 %8)
ret void
}