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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int make_dgram_server_socket(int);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ reply_to_sender(int, char *, struct sockaddr_in *, int);
int /*<<< orphan*/ say_who_called(struct sockaddr_in);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int portnum = atoi(argv[1]);
  if (argc != 2 || portnum <= 0) {
    fprintf(stderr, "usage: dgrevc port\n");
    exit(EXIT_FAILURE);
  }
  int sock_id = make_dgram_server_socket(portnum);
  char buf[BUFSIZ];
  struct sockaddr_in saddr;
  socklen_t saddrlen = sizeof(saddr);
  int msglen = 0;
  while ((msglen = recvfrom(sock_id, buf, BUFSIZ, 0, (struct sockaddr *)&saddr,
                            &saddrlen)) > 0) {
    buf[msglen] = '\0';
    printf("dgrecv: got a message: %s\n", buf);
    say_who_called(saddr);
    reply_to_sender(sock_id, buf, &saddr, saddrlen);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11ba <main+0x3a>
cmpl   $0x0,-0x14(%rbp)
jg     11dd <main+0x5d>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <make_dgram_server_socket@plt>
mov    %eax,-0x18(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x4,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jle    1285 <main+0x105>
mov    -0x40(%rbp),%rsi
movslq -0x38(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xdb4(%rip),%rdi
call   1030 <printf@plt>
mov    -0x30(%rbp),%edi
call   1070 <say_who_called@plt>
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x34(%rbp),%ecx
lea    -0x30(%rbp),%rdx
call   10a0 <reply_to_sender@plt>
jmp    1222 <main+0xa2>
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
cmp    $0x2,%ebx
jne    1262 <main+0xe2>
test   %eax,%eax
jle    1262 <main+0xe2>
mov    %eax,%edi
call   1060 <make_dgram_server_socket@plt>
mov    %eax,%r14d
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x4,-0x2c(%rbp)
lea    -0x30(%rbp),%r8
lea    -0x2c(%rbp),%r9
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1050 <recvfrom@plt>
test   %eax,%eax
jle    124a <main+0xca>
lea    0xe1c(%rip),%r15
lea    -0x30(%rbp),%r12
lea    -0x2c(%rbp),%r13
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    -0x30(%rbp),%edi
call   1070 <say_who_called@plt>
mov    -0x2c(%rbp),%ecx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10a0 <reply_to_sender@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   1050 <recvfrom@plt>
test   %eax,%eax
jg     1200 <main+0x80>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"usage: dgrevc port\0A\00"
@EXIT_FAILURE = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [27 x i8] c"dgrecv: got a message: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 2
br i1 %18, label %22, label %19
19: ; preds = %2
%20 = load i32, i32* %6
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19, %2
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = load i32, i32* %6
%29 = call i32 @make_dgram_server_socket(i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* @BUFSIZ
%31 = zext i32 %30 to i64
%32 = call i8* @llvm.stacksave()
store i8* %32, i8** %8
%33 = alloca i8, i64 %31
store i64 %31, i64* %9
store i32 4, i32* %11
store i32 0, i32* %12
br label %34
34: ; preds = %40, %27
%35 = load i32, i32* %7
%36 = load i32, i32* @BUFSIZ
%37 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%38 = call i32 @recvfrom(i32 %35, i8* %33, i32 %36, i32 0, %struct.sockaddr* %37, i32* %11)
store i32 %38, i32* %12
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %51
40: ; preds = %34
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %33, i64 %42
store i8 0, i8* %43
%44 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @say_who_called(i32 %46)
%48 = load i32, i32* %7
%49 = load i32, i32* %11
%50 = call i32 @reply_to_sender(i32 %48, i8* %33, %struct.sockaddr_in* %10, i32 %49)
br label %34
51: ; preds = %34
%52 = load i32, i32* @EXIT_SUCCESS
store i32 %52, i32* %3
%53 = load i8*, i8** %8
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"usage: dgrevc port\0A\00"
@EXIT_FAILURE = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [27 x i8] c"dgrecv: got a message: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = icmp ne i32 %0, 2
%9 = icmp slt i32 %7, 1
%10 = select i1 %8, i1 true, i1 %9
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = tail call i32 @make_dgram_server_socket(i32 %7)
%18 = load i32, i32* @BUFSIZ
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
%21 = bitcast %struct.sockaddr_in* %3 to i8*
%22 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i32 @recvfrom(i32 %17, i8* %20, i32 %18, i32 0, %struct.sockaddr* %23, i32* %4)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %16
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %28
28: ; preds = %26, %28
%29 = phi i32 [ %24, %26 ], [ %38, %28 ]
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %20, i64 %30
store i8 0, i8* %31
%32 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%33 = load i32, i32* %27
%34 = call i32 @say_who_called(i32 %33)
%35 = load i32, i32* %4
%36 = call i32 @reply_to_sender(i32 %17, i8* %20, %struct.sockaddr_in* %3, i32 %35)
%37 = load i32, i32* @BUFSIZ
%38 = call i32 @recvfrom(i32 %17, i8* %20, i32 %37, i32 0, %struct.sockaddr* %23, i32* %4)
%39 = icmp sgt i32 %38, 0
br i1 %39, label %28, label %40
40: ; preds = %28, %16
%41 = load i32, i32* @EXIT_SUCCESS
ret i32 %41
}