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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ strcmp(char *, char *);
int udp_recv(int, char *, int);
scalar_t__ udp_server(int *, int *);
int main() {
  int sock, port, len;
  char buf[2048];
  if (udp_server(&sock, &port) != 0) {
    perror("udp_server");
    return 1;
  }
  (void)printf("UDP server port=%d\n", port);
  while ((len = udp_recv(sock, buf, 2048)) >= 0) {
    (void)printf("UDP server: len=%d, data='%s'\n", len, buf);
    if (strcmp(buf, "exit") == 0) {
      (void)close(sock);
      return 0;
    }
  }
  perror("recvfrom failed");
  (void)printf("UDP server: error from udp_recv, len=%d\n", len);
  (void)close(sock);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <udp_server@plt>
cmp    $0x0,%rax
je     11a1 <main+0x41>
lea    0xe70(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124c <main+0xec>
mov    -0xc(%rbp),%esi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1070 <udp_recv@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jl     1220 <main+0xc0>
mov    -0x10(%rbp),%esi
lea    -0x810(%rbp),%rdx
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x810(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    121b <main+0xbb>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    124c <main+0xec>
jmp    11b2 <main+0x52>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x818,%rsp
lea    0x8(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1060 <udp_server@plt>
test   %rax,%rax
jne    11ff <main+0x9f>
mov    0xc(%rsp),%esi
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x10(%rsp),%rbx
lea    0xe80(%rip),%r14
lea    0xe98(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1070 <udp_recv@plt>
mov    %eax,%ebp
test   %eax,%eax
js     120d <main+0xad>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11b0 <main+0x50>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
jmp    1232 <main+0xd2>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
lea    0xe33(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    $0x1,%eax
jmp    11f1 <main+0x91>

LLVM IR (-O0):
@.str = [11 x i8] c"udp_server\00"
@.str.1 = [20 x i8] c"UDP server port=%d\0A\00"
@.str.2 = [31 x i8] c"UDP server: len=%d, data='%s'\0A\00"
@.str.3 = [5 x i8] c"exit\00"
@.str.4 = [16 x i8] c"recvfrom failed\00"
@.str.5 = [41 x i8] c"UDP server: error from udp_recv, len=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2048 x i8]
store i32 0, i32* %1
%6 = call i64 @udp_server(i32* %2, i32* %3)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %35
10: ; preds = %0
%11 = load i32, i32* %3
%12 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %28, %10
%14 = load i32, i32* %2
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%16 = call i32 @udp_recv(i32 %14, i8* %15, i32 2048)
store i32 %16, i32* %4
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %13
%19 = load i32, i32* %4
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %19, i8* %20)
%22 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
store i32 0, i32* %1
br label %35
28: ; preds = %18
br label %13
29: ; preds = %13
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* %4
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
store i32 1, i32* %1
br label %35
35: ; preds = %29, %25, %8
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [11 x i8] c"udp_server\00"
@.str.1 = [20 x i8] c"UDP server port=%d\0A\00"
@.str.2 = [31 x i8] c"UDP server: len=%d, data='%s'\0A\00"
@.str.3 = [5 x i8] c"exit\00"
@.str.4 = [16 x i8] c"recvfrom failed\00"
@.str.5 = [41 x i8] c"UDP server: error from udp_recv, len=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2048 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%7 = call i64 @udp_server(i32* %1, i32* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %30
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %18, %11
%15 = load i32, i32* %1
%16 = call i32 @udp_recv(i32 %15, i8* %6, i32 2048)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %16, i8* %6)
%20 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %14
22: ; preds = %18
%23 = load i32, i32* %1
%24 = call i32 @close(i32 %23)
br label %30
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %16)
%28 = load i32, i32* %1
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %25, %22, %9
%31 = phi i32 [ 1, %9 ], [ 0, %22 ], [ 1, %25 ]
ret i32 %31
}