#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 */
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *);
scalar_t__ sscanf(char const *, char *, int *);
int /*<<< orphan*/ stderr;
uint16_t getu16(const char *prog, const char *p) {
  int n;
  if (sscanf(p, "%d", &n) == 0 || n < 0 || n > 65535) {
    fprintf(stderr, "%s: '%s' is not a valid value.\n", prog, p);
    exit(1);
  }
  return (uint16_t)n;
}


x86 assembly (clang, -O0):
<getu16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <sscanf@plt>
cmp    $0x0,%rax
je     1175 <getu16+0x45>
cmpl   $0x0,-0x14(%rbp)
jl     1175 <getu16+0x45>
cmpl   $0xffff,-0x14(%rbp)
jle    119c <getu16+0x6c>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe76(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getu16>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xebf(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <sscanf@plt>
test   %rax,%rax
je     116a <getu16+0x3a>
mov    0x4(%rsp),%eax
test   %eax,%eax
js     116a <getu16+0x3a>
cmp    $0x10000,%eax
jge    116a <getu16+0x3a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"%s: '%s' is not a valid value.\0A\00"
define i32 @getu16(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = call i64 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 65535
br i1 %14, label %15, label %21
15: ; preds = %12, %9, %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = load i32, i32* %5
ret i32 %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"%s: '%s' is not a valid value.\0A\00"
define i32 @getu16(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i64 @sscanf(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%6 = icmp eq i64 %5, 0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
%9 = select i1 %6, i1 true, i1 %8
%10 = icmp sgt i32 %7, 65535
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
ret i32 %7
}