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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ alltoupper(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  char buffer[1024];
  struct sockaddr_in addr;
  int sd, addr_size, bytes_read;
  sd = socket(PF_INET, SOCK_DGRAM, 0);
  if (sd < 0) {
    perror("socket");
    abort();
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9999);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    abort();
  }
  do {
    bzero(buffer, 1024);
    addr_size = 1024;
    bytes_read =
        recvfrom(sd, buffer, 1024, 0, (struct sockaddr *)&addr, &addr_size);
    if (bytes_read > 0) {
      printf("Connect: %s:%d \"%s\"\n", inet_ntoa(addr.sin_addr),
             ntohs(addr.sin_port), buffer);
      alltoupper(buffer);
      if (sendto(sd, buffer, bytes_read, 0, (struct sockaddr *)&addr,
                 addr_size) < 0)
        perror("reply");
    } else
      perror("recvfrom");
  } while (bytes_read > 0);
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    1221 <main+0x51>
lea    0xdeb(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,-0x420(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x41c(%rbp)
mov    -0x424(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    1283 <main+0xb3>
lea    0xd90(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1288 <main+0xb8>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10e0 <bzero@plt>
movl   $0x400,-0x428(%rbp)
mov    -0x424(%rbp),%edi
lea    -0x410(%rbp),%rsi
lea    -0x420(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x428(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jle    1365 <main+0x195>
mov    -0x41c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x438(%rbp)
mov    -0x420(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
lea    -0x410(%rbp),%rcx
lea    0xcfb(%rip),%rdi
call   1060 <printf@plt>
lea    -0x410(%rbp),%rdi
call   10a0 <alltoupper@plt>
mov    -0x424(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x42c(%rbp),%edx
lea    -0x420(%rbp),%r8
mov    -0x428(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
cmp    $0x0,%rax
jge    1360 <main+0x190>
lea    0xcc6(%rip),%rdi
call   10d0 <perror@plt>
jmp    1371 <main+0x1a1>
lea    0xcbb(%rip),%rdi
call   10d0 <perror@plt>
jmp    1376 <main+0x1a6>
cmpl   $0x0,-0x42c(%rbp)
jg     1288 <main+0xb8>
mov    -0x424(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1358 <main+0x188>
mov    %eax,%ebp
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     1361 <main+0x191>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   10e0 <bzero@plt>
movl   $0x400,0xc(%rsp)
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
test   %eax,%eax
jle    1331 <main+0x161>
mov    %eax,%r12d
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%r13
jmp    12d3 <main+0x103>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x400,%esi
call   10e0 <bzero@plt>
movl   $0x400,0xc(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r13,%r9
call   1090 <recvfrom@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    1331 <main+0x161>
mov    0x14(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x10(%rsp),%edi
call   10b0 <ntohs@plt>
lea    0xd1d(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10a0 <alltoupper@plt>
mov    0xc(%rsp),%r9d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1070 <sendto@plt>
test   %rax,%rax
jns    12a0 <main+0xd0>
lea    0xcfa(%rip),%rdi
call   10d0 <perror@plt>
jmp    12a0 <main+0xd0>
lea    0xcef(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xca1(%rip),%rdi
jmp    1368 <main+0x198>
lea    0xc9f(%rip),%rdi
call   10d0 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [21 x i8] c"Connect: %s:%d \22%s\22\0A\00"
@.str.3 = [6 x i8] c"reply\00"
@.str.4 = [9 x i8] c"recvfrom\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (...) @abort()
unreachable
15: ; preds = %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 9999)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %4
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %15
br label %31
31: ; preds = %65, %30
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%33 = call i32 @bzero(i8* %32, i32 1024)
store i32 1024, i32* %5
%34 = load i32, i32* %4
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%36 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%37 = call i32 @recvfrom(i32 %34, i8* %35, i32 1024, i32 0, %struct.sockaddr* %36, i32* %5)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %62
40: ; preds = %31
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i8* @inet_ntoa(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%49 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %44, i32 %47, i8* %48)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%51 = call i32 @alltoupper(i8* %50)
%52 = load i32, i32* %4
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%54 = load i32, i32* %6
%55 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%56 = load i32, i32* %5
%57 = call i64 @sendto(i32 %52, i8* %53, i32 %54, i32 0, %struct.sockaddr* %55, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %40
%60 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %40
br label %64
62: ; preds = %31
%63 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %61
br label %65
65: ; preds = %64
%66 = load i32, i32* %6
%67 = icmp sgt i32 %66, 0
br i1 %67, label %31, label %68
68: ; preds = %65
%69 = load i32, i32* %4
%70 = call i32 @close(i32 %69)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [21 x i8] c"Connect: %s:%d \22%s\22\0A\00"
@.str.3 = [6 x i8] c"reply\00"
@.str.4 = [9 x i8] c"recvfrom\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 (...) @abort()
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 9999)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i64 @bind(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %14
%25 = call i32 @bzero(i8* %4, i32 1024)
store i32 1024, i32* %3
%26 = call i32 @recvfrom(i32 %9, i8* %4, i32 1024, i32 0, %struct.sockaddr* %21, i32* %3)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %31, label %48
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %24, %44
%32 = phi i32 [ %46, %44 ], [ %26, %24 ]
%33 = load i32, i32* %20
%34 = call i8* @inet_ntoa(i32 %33)
%35 = load i32, i32* %18
%36 = call i32 @ntohs(i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %34, i32 %36, i8* %4)
%38 = call i32 @alltoupper(i8* %4)
%39 = load i32, i32* %3
%40 = call i64 @sendto(i32 %9, i8* %4, i32 %32, i32 0, %struct.sockaddr* %21, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %31
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %31
%45 = call i32 @bzero(i8* %4, i32 1024)
store i32 1024, i32* %3
%46 = call i32 @recvfrom(i32 %9, i8* %4, i32 1024, i32 0, %struct.sockaddr* %21, i32* %3)
%47 = icmp sgt i32 %46, 0
br i1 %47, label %31, label %48
48: ; preds = %44, %24
%49 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @close(i32 %9)
ret i32 0
}