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

/* Variables and functions */
int O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int);
scalar_t__ pe_get16(int, int, int /*<<< orphan*/ *);
scalar_t__ pe_get32(int, int, int *);
int get_characteristics(const char *pathname, uint16_t *coff_characteristics,
                        uint16_t *pe_characteristics) {
  uint32_t pe_header_offset, opthdr_ofs;
  int status = 1;
  int fd, size;
  uint32_t pe_sig;
  fd = open(pathname, O_RDONLY | 0);
  if (fd == -1)
    return status;
  if (pe_get32(fd, 0x3c, &pe_header_offset) != 0)
    goto done;
  opthdr_ofs = pe_header_offset + 4 + 20;
  pe_sig = 0;
  if (pe_get32(fd, pe_header_offset, &pe_sig) != 0)
    goto done;
  if (pe_sig != 0x00004550)
    goto done;
  if (pe_get16(fd, pe_header_offset + 4 + 18, coff_characteristics) != 0)
    goto done;
  if (pe_get16(fd, opthdr_ofs + 70, pe_characteristics) != 0)
    goto done;
  status = 0;
done:
  close(fd);
  return status;
}


x86 assembly (clang, -O0):
<get_characteristics>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   1060 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1188 <get_characteristics+0x48>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1245 <get_characteristics+0x105>
mov    -0x30(%rbp),%edi
mov    $0x3c,%esi
lea    -0x24(%rbp),%rdx
call   1030 <pe_get32@plt>
cmp    $0x0,%rax
je     11a8 <get_characteristics+0x68>
jmp    1237 <get_characteristics+0xf7>
mov    -0x24(%rbp),%eax
add    $0x4,%eax
add    $0x14,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x24(%rbp),%esi
lea    -0x38(%rbp),%rdx
call   1030 <pe_get32@plt>
cmp    $0x0,%rax
je     11d9 <get_characteristics+0x99>
jmp    1237 <get_characteristics+0xf7>
cmpl   $0x4550,-0x38(%rbp)
je     11eb <get_characteristics+0xab>
jmp    1237 <get_characteristics+0xf7>
mov    -0x30(%rbp),%edi
mov    -0x24(%rbp),%esi
add    $0x4,%esi
add    $0x12,%esi
mov    -0x18(%rbp),%rdx
call   1050 <pe_get16@plt>
cmp    $0x0,%rax
je     120f <get_characteristics+0xcf>
jmp    1237 <get_characteristics+0xf7>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%esi
add    $0x46,%esi
mov    -0x20(%rbp),%rdx
call   1050 <pe_get16@plt>
cmp    $0x0,%rax
je     1230 <get_characteristics+0xf0>
jmp    1237 <get_characteristics+0xf7>
movl   $0x0,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_characteristics>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     118b <get_characteristics+0x4b>
mov    %eax,%ebx
lea    0xc(%rsp),%rdx
mov    %eax,%edi
mov    $0x3c,%esi
call   1030 <pe_get32@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     119f <get_characteristics+0x5f>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1190 <get_characteristics+0x50>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0xc(%rsp),%r12d
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
mov    %r12d,%esi
call   1030 <pe_get32@plt>
test   %rax,%rax
jne    1182 <get_characteristics+0x42>
cmpl   $0x4550,0x8(%rsp)
jne    1182 <get_characteristics+0x42>
mov    0xc(%rsp),%esi
add    $0x16,%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <pe_get16@plt>
test   %rax,%rax
jne    1182 <get_characteristics+0x42>
add    $0x5e,%r12d
mov    %ebx,%edi
mov    %r12d,%esi
mov    %r14,%rdx
call   1050 <pe_get16@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
jmp    1182 <get_characteristics+0x42>

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @get_characteristics(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 1, i32* %10
%14 = load i8*, i8** %5
%15 = load i32, i32* @O_RDONLY
%16 = or i32 %15, 0
%17 = call i32 @open(i8* %14, i32 %16)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = load i32, i32* %10
store i32 %21, i32* %4
br label %62
22: ; preds = %3
%23 = load i32, i32* %11
%24 = call i64 @pe_get32(i32 %23, i32 60, i32* %8)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
br label %58
27: ; preds = %22
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 4
%30 = add nsw i32 %29, 20
store i32 %30, i32* %9
store i32 0, i32* %13
%31 = load i32, i32* %11
%32 = load i32, i32* %8
%33 = call i64 @pe_get32(i32 %31, i32 %32, i32* %13)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %27
br label %58
36: ; preds = %27
%37 = load i32, i32* %13
%38 = icmp ne i32 %37, 17744
br i1 %38, label %39, label %40
39: ; preds = %36
br label %58
40: ; preds = %36
%41 = load i32, i32* %11
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 4
%44 = add nsw i32 %43, 18
%45 = load i32*, i32** %6
%46 = call i64 @pe_get16(i32 %41, i32 %44, i32* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %40
br label %58
49: ; preds = %40
%50 = load i32, i32* %11
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 70
%53 = load i32*, i32** %7
%54 = call i64 @pe_get16(i32 %50, i32 %52, i32* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %49
br label %58
57: ; preds = %49
store i32 0, i32* %10
br label %58
58: ; preds = %57, %56, %48, %39, %35, %26
%59 = load i32, i32* %11
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %10
store i32 %61, i32* %4
br label %62
62: ; preds = %58, %20
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @get_characteristics(i8* nocapture %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %34, label %11
11: ; preds = %3
%12 = call i64 @pe_get32(i32 %9, i32 60, i32* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %11
%15 = load i32, i32* %4
store i32 0, i32* %5
%16 = call i64 @pe_get32(i32 %9, i32 %15, i32* %5)
%17 = icmp ne i64 %16, 0
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 17744
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %31, label %21
21: ; preds = %14
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 22
%24 = call i64 @pe_get16(i32 %9, i32 %23, i32* %1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = add nsw i32 %15, 94
%28 = call i64 @pe_get16(i32 %9, i32 %27, i32* %2)
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %26, %21, %14, %11
%32 = phi i32 [ 1, %11 ], [ 1, %14 ], [ 1, %21 ], [ %30, %26 ]
%33 = call i32 @close(i32 %9)
br label %34
34: ; preds = %3, %31
%35 = phi i32 [ %32, %31 ], [ 1, %3 ]
ret i32 %35
}