#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 foo(int);
int main(int argc, char **argv) {
  const char *buf = argv[1];
  unsigned int data = (((unsigned int)buf[0]) << 8) + (unsigned int)buf[1];
  if (data & 0x8000) {
    data &= 0x7fff;
    data ^= 0x7fff;
    data += 1;
    data *= -1;
  }
  return foo((int)data);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
shl    $0x8,%eax
mov    -0x18(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     1180 <main+0x70>
mov    -0x1c(%rbp),%eax
and    $0x7fff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
xor    $0x7fff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
imul   $0xffffffff,-0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <foo@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
mov    0x8(%rsi),%rax
movsbl (%rax),%ecx
shl    $0x8,%ecx
movsbl 0x1(%rax),%eax
add    %ecx,%eax
mov    %eax,%edi
or     $0xffff8000,%edi
test   $0x8000,%eax
cmove  %eax,%edi
jmp    1030 <foo@plt>

LLVM IR (-O0):
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = shl i32 %14, 8
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = add i32 %15, %19
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = and i32 %21, 32768
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %2
%25 = load i32, i32* %7
%26 = and i32 %25, 32767
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = xor i32 %27, 32767
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = add i32 %29, 1
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = mul i32 %31, -1
store i32 %32, i32* %7
br label %33
33: ; preds = %24, %2
%34 = load i32, i32* %7
%35 = call i32 @foo(i32 %34)
ret i32 %35
}

LLVM IR (-O2):
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i8, i8* %4
%6 = sext i8 %5 to i32
%7 = shl nsw i32 %6, 8
%8 = getelementptr inbounds i8, i8* %4, i64 1
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = add nsw i32 %7, %10
%12 = and i32 %11, 32768
%13 = icmp eq i32 %12, 0
%14 = or i32 %11, -32768
%15 = select i1 %13, i32 %11, i32 %14
%16 = tail call i32 @foo(i32 %15)
ret i32 %16
}