#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*/ uint64_t;
typedef int /*<<< orphan*/ uint32_t;
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getopt(int, char **, char *);
int /*<<< orphan*/ htobe32(int /*<<< orphan*/);
int /*<<< orphan*/ htole32(int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
char *optarg;
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strtoul(char *, int /*<<< orphan*/ *, int);
int write(int, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  struct stat s;
  int c;
  int fd;
  uint64_t offset = 0;
  uint32_t size = 0;
  char *file = NULL;
  int ret = 1;
  int is_bigendian = 0;
  while ((c = getopt(argc, argv, "hf:o:b")) != -1) {
    switch (c) {
    case 'f':
      file = optarg;
      break;
    case 'o':
      offset = strtoul(optarg, NULL, 16);
      break;
    case 'b':
      is_bigendian = 1;
      break;
    }
  }
  if (!file) {
    fprintf(stderr, "missing file\n");
    return 1;
  }
  if (stat(file, &s)) {
    perror("stat");
    return 1;
  }
  fd = open(file, O_WRONLY);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  ret = lseek(fd, offset, SEEK_SET);
  if (ret < 0) {
    perror("lseek");
    ret = 1;
    goto err;
  }
  size = s.st_size;
  if (is_bigendian)
    size = htobe32(size);
  else
    size = htole32(size);
  ret = write(fd, &size, 4);
  if (ret != 4) {
    perror("write");
    ret = 1;
    goto err;
  }
  ret = 0;
err:
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe08(%rip),%rdx
call   10c0 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     127e <main+0xce>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x62,%eax
je     1272 <main+0xc2>
jmp    121d <main+0x6d>
mov    -0x3c(%rbp),%eax
sub    $0x66,%eax
je     123f <main+0x8f>
jmp    122e <main+0x7e>
mov    -0x3c(%rbp),%eax
sub    $0x6f,%eax
je     1252 <main+0xa2>
jmp    1279 <main+0xc9>
mov    0x2db2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1279 <main+0xc9>
mov    0x2d9f(%rip),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10d0 <strtoul@plt>
mov    %eax,-0x24(%rbp)
jmp    1279 <main+0xc9>
movl   $0x1,-0x38(%rbp)
jmp    11ea <main+0x3a>
cmpq   $0x0,-0x30(%rbp)
jne    12aa <main+0xfa>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13bf <main+0x20f>
mov    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     12d9 <main+0x129>
lea    0xd4d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13bf <main+0x20f>
mov    -0x30(%rbp),%rdi
mov    0x2cdc(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1310 <main+0x160>
lea    0xd1b(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13bf <main+0x20f>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    0x2cb3(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1349 <main+0x199>
lea    0xce7(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x34(%rbp)
jmp    13b1 <main+0x201>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1369 <main+0x1b9>
mov    -0x28(%rbp),%edi
call   1090 <htobe32@plt>
mov    %eax,-0x28(%rbp)
jmp    1374 <main+0x1c4>
mov    -0x28(%rbp),%edi
call   1030 <htole32@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
je     13aa <main+0x1fa>
lea    0xc8c(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x34(%rbp)
jmp    13b1 <main+0x201>
movl   $0x0,-0x34(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,0xc(%rsp)
xor    %r14d,%r14d
lea    0xe2b(%rip),%r12
xor    %r15d,%r15d
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10c0 <getopt@plt>
cmp    $0x65,%eax
jg     1210 <main+0x60>
cmp    $0xffffffff,%eax
je     1241 <main+0x91>
cmp    $0x62,%eax
jne    11e0 <main+0x30>
mov    $0x1,%r13d
jmp    11e0 <main+0x30>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x6f,%eax
je     1226 <main+0x76>
cmp    $0x66,%eax
jne    11e0 <main+0x30>
mov    0x2dd7(%rip),%rax
mov    (%rax),%r15
jmp    11e0 <main+0x30>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10d0 <strtoul@plt>
mov    %eax,%r14d
jmp    11e0 <main+0x30>
test   %r15,%r15
je     12a2 <main+0xf2>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1080 <stat@plt>
test   %rax,%rax
jne    12f7 <main+0x147>
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1300 <main+0x150>
mov    %eax,%ebx
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14d,%esi
call   1050 <lseek@plt>
test   %eax,%eax
js     1313 <main+0x163>
mov    0x10(%rsp),%edi
test   %r13d,%r13d
je     12be <main+0x10e>
call   1090 <htobe32@plt>
jmp    12c3 <main+0x113>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xd55(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%ebp
jmp    12e6 <main+0x136>
call   1030 <htole32@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1040 <write@plt>
xor    %ebp,%ebp
cmp    $0x4,%eax
jne    131c <main+0x16c>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd17(%rip),%rdi
jmp    1307 <main+0x157>
lea    0xd13(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebp
jmp    12e6 <main+0x136>
lea    0xd05(%rip),%rdi
jmp    1323 <main+0x173>
lea    0xd02(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebp
jmp    12df <main+0x12f>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"hf:o:b\00"
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [14 x i8] c"missing file\0A\00"
@.str.2 = [5 x i8] c"stat\00"
@O_WRONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.4 = [6 x i8] c"lseek\00"
@.str.5 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
store i32 0, i32* %10
store i8* null, i8** %11
store i32 1, i32* %12
store i32 0, i32* %13
br label %14
14: ; preds = %27, %2
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = call i32 @getopt(i32 %15, i8** %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %7
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i32, i32* %7
switch i32 %20, label %27 [
i32 102, label %21
i32 111, label %23
i32 98, label %26
]
21: ; preds = %19
%22 = load i8*, i8** @optarg
store i8* %22, i8** %11
br label %27
23: ; preds = %19
%24 = load i8*, i8** @optarg
%25 = call i32 @strtoul(i8* %24, i32* null, i32 16)
store i32 %25, i32* %9
br label %27
26: ; preds = %19
store i32 1, i32* %13
br label %27
27: ; preds = %19, %26, %23, %21
br label %14
28: ; preds = %14
%29 = load i8*, i8** %11
%30 = icmp ne i8* %29, null
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %80
34: ; preds = %28
%35 = load i8*, i8** %11
%36 = call i64 @stat(i8* %35, %struct.stat* %6)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %80
40: ; preds = %34
%41 = load i8*, i8** %11
%42 = load i32, i32* @O_WRONLY
%43 = call i32 @open(i8* %41, i32 %42)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %80
48: ; preds = %40
%49 = load i32, i32* %8
%50 = load i32, i32* %9
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @lseek(i32 %49, i32 %50, i32 %51)
store i32 %52, i32* %12
%53 = load i32, i32* %12
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %12
br label %76
57: ; preds = %48
%58 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%59 = load i32, i32* %58
store i32 %59, i32* %10
%60 = load i32, i32* %13
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i32, i32* %10
%64 = call i32 @htobe32(i32 %63)
store i32 %64, i32* %10
br label %68
65: ; preds = %57
%66 = load i32, i32* %10
%67 = call i32 @htole32(i32 %66)
store i32 %67, i32* %10
br label %68
68: ; preds = %65, %62
%69 = load i32, i32* %8
%70 = call i32 @write(i32 %69, i32* %10, i32 4)
store i32 %70, i32* %12
%71 = load i32, i32* %12
%72 = icmp ne i32 %71, 4
br i1 %72, label %73, label %75
73: ; preds = %68
%74 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %12
br label %76
75: ; preds = %68
store i32 0, i32* %12
br label %76
76: ; preds = %75, %73, %55
%77 = load i32, i32* %8
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %12
store i32 %79, i32* %3
br label %80
80: ; preds = %76, %46, %38, %31
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"hf:o:b\00"
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [14 x i8] c"missing file\0A\00"
@.str.2 = [5 x i8] c"stat\00"
@O_WRONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.4 = [6 x i8] c"lseek\00"
@.str.5 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
br label %7
7: ; preds = %20, %2
%8 = phi i32 [ %22, %20 ], [ 0, %2 ]
%9 = phi i8* [ %12, %20 ], [ null, %2 ]
%10 = phi i32 [ %15, %20 ], [ 0, %2 ]
br label %11
11: ; preds = %7, %18
%12 = phi i8* [ %9, %7 ], [ %19, %18 ]
%13 = phi i32 [ %10, %7 ], [ %15, %18 ]
br label %14
14: ; preds = %16, %11
%15 = phi i32 [ %13, %11 ], [ 1, %16 ]
br label %16
16: ; preds = %14, %16
%17 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
switch i32 %17, label %16 [
i32 -1, label %23
i32 102, label %18
i32 111, label %20
i32 98, label %14
]
18: ; preds = %16
%19 = load i8*, i8** @optarg
br label %11
20: ; preds = %16
%21 = load i8*, i8** @optarg
%22 = tail call i32 @strtoul(i8* nocapture %21, i32* null, i32 16)
br label %7
23: ; preds = %16
%24 = icmp eq i8* %12, null
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %61
28: ; preds = %23
%29 = call i64 @stat(i8* %12, %struct.stat* %3)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %61
33: ; preds = %28
%34 = load i32, i32* @O_WRONLY
%35 = call i32 @open(i8* %12, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %61
39: ; preds = %33
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @lseek(i32 %35, i32 %8, i32 %40)
%42 = icmp slt i32 %41, 0
br i1 %42, label %55, label %43
43: ; preds = %39
%44 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%45 = load i32, i32* %44
%46 = icmp eq i32 %15, 0
br i1 %46, label %49, label %47
47: ; preds = %43
%48 = call i32 @htobe32(i32 %45)
br label %51
49: ; preds = %43
%50 = call i32 @htole32(i32 %45)
br label %51
51: ; preds = %49, %47
%52 = phi i32 [ %50, %49 ], [ %48, %47 ]
store i32 %52, i32* %4
%53 = call i32 @write(i32 %35, i32* %4, i32 4)
%54 = icmp eq i32 %53, 4
br i1 %54, label %58, label %55
55: ; preds = %51, %39
%56 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), %39 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %51 ]
%57 = call i32 @perror(i8* %56)
br label %58
58: ; preds = %55, %51
%59 = phi i32 [ 0, %51 ], [ 1, %55 ]
%60 = call i32 @close(i32 %35)
br label %61
61: ; preds = %58, %37, %31, %25
%62 = phi i32 [ 1, %31 ], [ 1, %37 ], [ %59, %58 ], [ 1, %25 ]
ret i32 %62
}