#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*/ b;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, ...);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, long *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  long b;
  FILE *fp;
  int fd;
  if (argc != 2) {
    (void)fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
    return 1;
  }
  if ((fd = open(argv[1], O_RDWR)) == -1) {
    perror("open");
    return 1;
  }
  if (read(fd, &b, sizeof(b)) != sizeof(b)) {
    perror("read");
    return 1;
  }
  if (lseek(fd, 0, SEEK_SET) == -1) {
    perror("lseek");
    return 1;
  }
  (void)close(fd);
  if ((fp = fopen(argv[1], "w")) == NULL) {
    perror("fopen");
    return 1;
  }
  if (fprintf(fp, "%ld\n", b) < 0) {
    perror("fprintf");
    return 1;
  }
  if (fclose(fp) != 0) {
    perror("fclose");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cb <main+0x4b>
mov    0x2e31(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x19a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1205 <main+0x85>
lea    0xe30(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x19a>
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
cltq
cmp    $0x8,%rax
je     123a <main+0xba>
lea    0xded(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x19a>
mov    -0x24(%rbp),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    126e <main+0xee>
lea    0xdbe(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x19a>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd9c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12b0 <main+0x130>
lea    0xd84(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x19a>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd6a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
cmp    $0x0,%rax
jge    12e8 <main+0x168>
lea    0xd57(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x19a>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1313 <main+0x193>
lea    0xd34(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x19a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1230 <main+0xb0>
mov    0x8(%rbx),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124d <main+0xcd>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <read@plt>
cmp    $0x8,%eax
jne    1256 <main+0xd6>
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     125f <main+0xdf>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x8(%rbx),%rdi
lea    0xe30(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1268 <main+0xe8>
mov    %rax,%rbp
mov    (%rsp),%rdx
lea    0xe20(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %rax,%rax
js     1271 <main+0xf1>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     1282 <main+0x102>
lea    0xe08(%rip),%rdi
jmp    1278 <main+0xf8>
mov    0x2da1(%rip),%rax
movslq (%rax),%rdi
mov    (%rbx),%rdx
lea    0xdbc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    127d <main+0xfd>
lea    0xdd0(%rip),%rdi
jmp    1278 <main+0xf8>
lea    0xdb9(%rip),%rdi
jmp    1278 <main+0xf8>
lea    0xdb5(%rip),%rdi
jmp    1278 <main+0xf8>
lea    0xdb4(%rip),%rdi
jmp    1278 <main+0xf8>
lea    0xdb6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [6 x i8] c"fopen\00"
@.str.6 = [5 x i8] c"%ld\0A\00"
@.str.7 = [8 x i8] c"fprintf\00"
@.str.8 = [7 x i8] c"fclose\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i32*
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i64 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %66
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %66
28: ; preds = %19
%29 = load i32, i32* %8
%30 = call i32 @read(i32 %29, i64* %6, i32 8)
%31 = sext i32 %30 to i64
%32 = icmp ne i64 %31, 8
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = 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 %66
35: ; preds = %28
%36 = load i32, i32* %8
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @lseek(i32 %36, i32 0, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %66
42: ; preds = %35
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = call i32* @fopen(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %48, i32** %7
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %66
52: ; preds = %42
%53 = load i32*, i32** %7
%54 = load i64, i64* %6
%55 = call i64 (i32*, i8*, ...) @fprintf(i32* %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %66
59: ; preds = %52
%60 = load i32*, i32** %7
%61 = call i64 @fclose(i32* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %66
65: ; preds = %59
store i32 0, i32* %3
br label %66
66: ; preds = %65, %63, %57, %50, %40, %33, %26, %11
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [6 x i8] c"fopen\00"
@.str.6 = [5 x i8] c"%ld\0A\00"
@.str.7 = [8 x i8] c"fprintf\00"
@.str.8 = [7 x i8] c"fclose\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i32*
%10 = load i8*, i8** %1
%11 = tail call i64 (i32*, i8*, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %49
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %49
20: ; preds = %12
%21 = call i32 @read(i32 %16, i64* %3, i32 8)
%22 = icmp eq i32 %21, 8
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %49
25: ; preds = %20
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @lseek(i32 %16, i32 0, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %49
31: ; preds = %25
%32 = tail call i32 @close(i32 %16)
%33 = load i8*, i8** %13
%34 = tail call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %49
38: ; preds = %31
%39 = load i64, i64* %3
%40 = tail call i64 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %49
44: ; preds = %38
%45 = tail call i64 @fclose(i32* %34)
%46 = icmp eq i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %49
49: ; preds = %44, %47, %42, %36, %29, %23, %18, %6
%50 = phi i32 [ 1, %6 ], [ 1, %18 ], [ 1, %23 ], [ 1, %29 ], [ 1, %36 ], [ 1, %42 ], [ 1, %47 ], [ 0, %44 ]
ret i32 %50
}