#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ get_os_sectors(int, char *);
int /*<<< orphan*/ load_srec(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ put_os_sectors(int, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ strrchr(char *, char);
void put_os(int fd, char *filename) {
  char buffer[16384];
  FILE *in;
  get_os_sectors(fd, buffer);
  in = fopen(filename, "r");
  if (!in) {
    fprintf(stderr, "cannot open %s\n", filename);
    exit(1);
  }
  if (strncmp(strrchr(filename, '.'), ".s19", 4) != 0) {
    printf("writing binary\n");
    if (fread(buffer, 1, 16384, in) != 16384) {
      fprintf(stderr, "error reading %s\n", filename);
      fclose(in);
      exit(1);
    }
    fclose(in);
  } else {
    load_srec(in, buffer);
  }
  if (fd != -1)
    put_os_sectors(fd, buffer);
  else
    fprintf(stderr, "Unsupported scheme. L8r.\n");
}


x86 assembly (clang, -O0):
<put_os>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x4010(%rbp),%rsi
call   1080 <get_os_sectors@plt>
mov    -0x10(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x4018(%rbp)
cmpq   $0x0,-0x4018(%rbp)
jne    121b <put_os+0x6b>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %eax,%edi
lea    0xde0(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     12bc <put_os+0x10c>
lea    0xdca(%rip),%rdi
call   1060 <printf@plt>
lea    -0x4010(%rbp),%rdi
mov    -0x4018(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4000,%edx
call   1040 <fread@plt>
cmp    $0x4000,%eax
je     12ab <put_os+0xfb>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x4018(%rbp),%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x4018(%rbp),%rdi
call   1050 <fclose@plt>
jmp    12cf <put_os+0x11f>
mov    -0x4018(%rbp),%rdi
lea    -0x4010(%rbp),%rsi
call   10b0 <load_srec@plt>
cmpl   $0xffffffff,-0x4(%rbp)
je     12ed <put_os+0x13d>
mov    -0x4(%rbp),%edi
lea    -0x4010(%rbp),%rsi
call   10a0 <put_os_sectors@plt>
jmp    1304 <put_os+0x154>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
lea    0xd3c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<put_os>:
push   %rbp
push   %r14
push   %rbx
sub    $0x4000,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
call   1080 <get_os_sectors@plt>
lea    0xe31(%rip),%rsi
mov    %r14,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     127c <put_os+0xcc>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
lea    0xe1b(%rip),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1249 <put_os+0x99>
lea    0xe34(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x4000,%eax
jne    12a0 <put_os+0xf0>
mov    %rbx,%rdi
call   1060 <fclose@plt>
cmp    $0xffffffff,%ebp
je     1259 <put_os+0xa9>
mov    %rsp,%rsi
mov    %ebp,%edi
call   10a0 <put_os_sectors@plt>
jmp    1270 <put_os+0xc0>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10b0 <load_srec@plt>
cmp    $0xffffffff,%ebp
jne    123d <put_os+0x8d>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
add    $0x4000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"cannot open %s\0A\00"
@.str.2 = [5 x i8] c".s19\00"
@.str.3 = [16 x i8] c"writing binary\0A\00"
@.str.4 = [18 x i8] c"error reading %s\0A\00"
@.str.5 = [26 x i8] c"Unsupported scheme. L8r.\0A\00"
define void @put_os(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [16384 x i8]
%6 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i32, i32* %3
%8 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%9 = call i32 @get_os_sectors(i32 %7, i8* %8)
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = call i32 @strrchr(i8* %20, i8 signext 46)
%22 = call i64 @strncmp(i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%26 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%27 = load i32*, i32** %6
%28 = call i32 @fread(i8* %26, i32 1, i32 16384, i32* %27)
%29 = icmp ne i32 %28, 16384
br i1 %29, label %30, label %37
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %4
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = load i32*, i32** %6
%35 = call i32 @fclose(i32* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %24
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
br label %44
40: ; preds = %19
%41 = load i32*, i32** %6
%42 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%43 = call i32 @load_srec(i32* %41, i8* %42)
br label %44
44: ; preds = %40, %37
%45 = load i32, i32* %3
%46 = icmp ne i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32, i32* %3
%49 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%50 = call i32 @put_os_sectors(i32 %48, i8* %49)
br label %54
51: ; preds = %44
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %51, %47
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"cannot open %s\0A\00"
@.str.2 = [5 x i8] c".s19\00"
@.str.4 = [18 x i8] c"error reading %s\0A\00"
@.str.5 = [26 x i8] c"Unsupported scheme. L8r.\0A\00"
@str = [15 x i8] c"writing binary\00"
define void @put_os(i32 %0, i8* %1) {
%3 = alloca [16384 x i8]
%4 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%5 = call i32 @get_os_sectors(i32 %0, i8* %4)
%6 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = call i32 @strrchr(i8* %1, i8 signext 46)
%14 = call i64 @strncmp(i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%18 = call i32 @fread(i8* %4, i32 1, i32 16384, i32* %6)
%19 = icmp eq i32 %18, 16384
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%23 = call i32 @fclose(i32* %6)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = call i32 @fclose(i32* %6)
br label %29
27: ; preds = %12
%28 = call i32 @load_srec(i32* %6, i8* %4)
br label %29
29: ; preds = %27, %25
%30 = icmp eq i32 %0, -1
br i1 %30, label %33, label %31
31: ; preds = %29
%32 = call i32 @put_os_sectors(i32 %0, i8* %4)
br label %36
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %33, %31
ret void
}