#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
short htons(short);
int /*<<< orphan*/ memcpy(char *, short *, int);
int /*<<< orphan*/ printf(char *, ...);
int setFlag(short flag, char *datagram) {
  if (flag < 0) {
    printf("Flag is negative !\n");
    exit(EXIT_FAILURE);
  }
  if (flag > 6) {
    printf("Unknown flag value : [%d]\n", (int)flag);
    exit(EXIT_FAILURE);
  }
  flag = htons(flag);
  memcpy(datagram, &flag, sizeof(short));
  return 1;
}


x86 assembly (clang, -O0):
<setFlag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    %rsi,-0x10(%rbp)
movswl -0x2(%rbp),%eax
cmp    $0x0,%eax
jge    117c <setFlag+0x3c>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movswl -0x2(%rbp),%eax
cmp    $0x6,%eax
jle    11a9 <setFlag+0x69>
movswl -0x2(%rbp),%esi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movswl -0x2(%rbp),%edi
call   1030 <htons@plt>
mov    %ax,-0x2(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2(%rbp),%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setFlag>:
push   %rbx
sub    $0x10,%rsp
test   %di,%di
js     118d <setFlag+0x3d>
cmp    $0x7,%di
jae    119b <setFlag+0x4b>
mov    %rsi,%rbx
movswl %di,%edi
call   1040 <htons@plt>
mov    %ax,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1060 <memcpy@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
jmp    11ac <setFlag+0x5c>
movzwl %di,%esi
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Flag is negative !\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Unknown flag value : [%d]\0A\00"
define i32 @setFlag(i16 signext %0, i8* %1) {
%3 = alloca i16
%4 = alloca i8*
store i16 %0, i16* %3
store i8* %1, i8** %4
%5 = load i16, i16* %3
%6 = sext i16 %5 to i32
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = load i16, i16* %3
%14 = sext i16 %13 to i32
%15 = icmp sgt i32 %14, 6
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i16, i16* %3
%18 = sext i16 %17 to i32
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = load i16, i16* %3
%24 = call signext i16 @htons(i16 signext %23)
store i16 %24, i16* %3
%25 = load i8*, i8** %4
%26 = call i32 @memcpy(i8* %25, i16* %3, i32 2)
ret i32 1
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Unknown flag value : [%d]\0A\00"
@str = [19 x i8] c"Flag is negative !\00"
define i32 @setFlag(i16 signext %0, i8* %1) {
%3 = alloca i16
%4 = icmp slt i16 %0, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = icmp ugt i16 %0, 6
br i1 %10, label %11, label %16
11: ; preds = %9
%12 = zext i16 %0 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %9
%17 = tail call signext i16 @htons(i16 signext %0)
store i16 %17, i16* %3
%18 = call i32 @memcpy(i8* %1, i16* %3, i32 2)
ret i32 1
}