#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int make_dgram_server(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ sys_who_called(struct sockaddr_in *);
int main(int argc, char *argv[]) {
  int sock;
  char buf[1024], reply[1024];
  int msg_len;
  socklen_t cli_len;
  struct sockaddr_in cli_addr;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <port>\n", argv[0]);
    exit(1);
  }
  sock = make_dgram_server(atoi(argv[1]));
  if (sock == -1) {
    perror("make_dgram_server() failed");
    exit(1);
  };
  while (1) {
    cli_len = sizeof(cli_addr);
    msg_len =
        recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&cli_addr, &cli_len);
    buf[msg_len] = '\0';
    printf("dgrecv: got a message %s\n", buf);
    sys_who_called(&cli_addr);
    snprintf(reply, 1024, "Thank you for %d char message\n", msg_len);
    sendto(sock, reply, strlen(reply), 0, (struct sockaddr *)&cli_addr,
           sizeof(cli_addr));
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f9 <main+0x49>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe16(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1080 <make_dgram_server@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1230 <main+0x80>
lea    0xdf1(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    1235 <main+0x85>
movl   $0x4,-0x828(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x830(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x828(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x824(%rbp)
movslq -0x824(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xda7(%rip),%rdi
call   1040 <printf@plt>
lea    -0x830(%rbp),%rdi
call   1090 <sys_who_called@plt>
lea    -0x820(%rbp),%rdi
mov    -0x824(%rbp),%ecx
mov    $0x400,%esi
lea    0xd97(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x83c(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x820(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x83c(%rbp),%edi
mov    -0x838(%rbp),%rsi
mov    %eax,%edx
lea    -0x830(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1060 <sendto@plt>
jmp    1235 <main+0x85>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
cmp    $0x2,%edi
jne    1275 <main+0xc5>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1080 <make_dgram_server@plt>
cmp    $0xffffffff,%eax
je     1297 <main+0xe7>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0xe51(%rip),%rbp
lea    0x410(%rsp),%r12
xchg   %ax,%ax
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    0x4(%rsp),%r9
call   10a0 <recvfrom@plt>
movslq %eax,%r13
movb   $0x0,0x10(%rsp,%r13,1)
lea    0xdfc(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r15,%rdi
call   1090 <sys_who_called@plt>
mov    %r12,%rdi
mov    $0x400,%esi
mov    %rbp,%rdx
mov    %r13d,%ecx
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0x4,%r9d
call   1060 <sendto@plt>
jmp    1200 <main+0x50>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd78(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd74(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@.str.1 = [27 x i8] c"make_dgram_server() failed\00"
@.str.2 = [26 x i8] c"dgrecv: got a message %s\0A\00"
@.str.3 = [31 x i8] c"Thank you for %d char message\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = call i32 @make_dgram_server(i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
br label %33
33: ; preds = %32, %33
store i32 4, i32* %10
%34 = load i32, i32* %6
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%36 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%37 = call i32 @recvfrom(i32 %34, i8* %35, i32 1024, i32 0, %struct.sockaddr* %36, i32* %10)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%42 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = call i32 @sys_who_called(%struct.sockaddr_in* %11)
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%45 = load i32, i32* %9
%46 = call i32 @snprintf(i8* %44, i32 1024, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %6
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%52 = call i32 @sendto(i32 %47, i8* %48, i32 %50, i32 0, %struct.sockaddr* %51, i32 4)
br label %33
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@.str.1 = [27 x i8] c"make_dgram_server() failed\00"
@.str.2 = [26 x i8] c"dgrecv: got a message %s\0A\00"
@.str.3 = [31 x i8] c"Thank you for %d char message\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = bitcast i32* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = icmp eq i32 %0, 2
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = tail call i32 @make_dgram_server(i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %28
25: ; preds = %17
%26 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %23, %28
store i32 4, i32* %5
%29 = call i32 @recvfrom(i32 %21, i8* %7, i32 1024, i32 0, %struct.sockaddr* %24, i32* %5)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%33 = call i32 @sys_who_called(%struct.sockaddr_in* %6)
%34 = call i32 @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%35 = call i32 @strlen(i8* %8)
%36 = call i32 @sendto(i32 %21, i8* %8, i32 %35, i32 0, %struct.sockaddr* %24, i32 4)
br label %28
}