long posLongExtract(unsigned char *inbuf) {
  long tmp;
  tmp = (long)((((inbuf[3]) << 24) & 0xFF000000) |
               (((inbuf[2]) << 16) & 0x00FF0000) |
               (((inbuf[1]) << 8) & 0x0000FF00) | ((inbuf[0]) & 0x000000FF));
  if (tmp & (1L << 31))
    return (-(tmp & ~(1L << 31)));
  else
    return (tmp);
}


x86 assembly (clang, -O0):
<posLongExtract>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x3(%rax),%eax
shl    $0x18,%eax
and    $0xff000000,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
shl    $0x10,%ecx
and    $0xff0000,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
shl    $0x8,%ecx
and    $0xff00,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%rcx
movzbl (%rcx),%ecx
and    $0xff,%ecx
or     %ecx,%eax
mov    %eax,%eax
mov    %rax,-0x18(%rbp)
movabs $0x80000000,%rax
and    -0x18(%rbp),%rax
cmp    $0x0,%rax
je     1187 <posLongExtract+0x87>
movabs $0xffffffff7fffffff,%rcx
ff ff ff
and    -0x18(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    118f <posLongExtract+0x8f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<posLongExtract>:
movzbl 0x3(%rdi),%ecx
mov    %rcx,%rax
shl    $0x18,%rax
movzbl 0x2(%rdi),%edx
shl    $0x10,%rdx
or     %rax,%rdx
movzbl 0x1(%rdi),%eax
shl    $0x8,%rax
or     %rdx,%rax
movzbl (%rdi),%edx
or     %rax,%rdx
mov    %edx,%eax
and    $0x7fffffff,%eax
neg    %rax
test   $0x80,%cl
cmove  %rdx,%rax
ret

LLVM IR (-O0):
define i64 @posLongExtract(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = getelementptr inbounds i8, i8* %5, i64 3
%7 = load i8, i8* %6
%8 = zext i8 %7 to i32
%9 = shl i32 %8, 24
%10 = and i32 %9, -16777216
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 2
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = shl i32 %14, 16
%16 = and i32 %15, 16711680
%17 = or i32 %10, %16
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = shl i32 %21, 8
%23 = and i32 %22, 65280
%24 = or i32 %17, %23
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = and i32 %28, 255
%30 = or i32 %24, %29
%31 = zext i32 %30 to i64
store i64 %31, i64* %4
%32 = load i64, i64* %4
%33 = and i64 %32, 2147483648
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %1
%36 = load i64, i64* %4
%37 = and i64 %36, -2147483649
%38 = sub nsw i64 0, %37
store i64 %38, i64* %2
br label %41
39: ; preds = %1
%40 = load i64, i64* %4
store i64 %40, i64* %2
br label %41
41: ; preds = %39, %35
%42 = load i64, i64* %2
ret i64 %42
}

LLVM IR (-O2):
define i64 @posLongExtract(i8* nocapture %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 3
%3 = load i8, i8* %2
%4 = zext i8 %3 to i64
%5 = shl nuw nsw i64 %4, 24
%6 = getelementptr inbounds i8, i8* %0, i64 2
%7 = load i8, i8* %6
%8 = zext i8 %7 to i64
%9 = shl nuw nsw i64 %8, 16
%10 = or i64 %9, %5
%11 = getelementptr inbounds i8, i8* %0, i64 1
%12 = load i8, i8* %11
%13 = zext i8 %12 to i64
%14 = shl nuw nsw i64 %13, 8
%15 = or i64 %10, %14
%16 = load i8, i8* %0
%17 = zext i8 %16 to i64
%18 = or i64 %15, %17
%19 = and i64 %4, 128
%20 = icmp eq i64 %19, 0
%21 = and i64 %18, 2147483647
%22 = sub nsw i64 0, %21
%23 = select i1 %20, i64 %18, i64 %22
ret i64 %23
}