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

/* Variables and functions */
int O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int filesize(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int lseek(int, int, int /*<<< orphan*/);
unsigned char *malloc(int);
int open(char *, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write_cfile(unsigned char *, int, char *);
scalar_t__ write_hfile(unsigned char *, int, char *);
int main(int argc, char *argv[]) {
  char *infile;
  char *cname;
  int fd;
  unsigned char *buf;
  int len;
  int n;
  if (argc != 3) {
    fprintf(stderr, "Usage: ipod2c file.ipod cname\n");
    return 0;
  }
  infile = argv[1];
  cname = argv[2];
  fd = open(infile, O_RDONLY | 0);
  if (fd < 0) {
    fprintf(stderr, "Can not open %s\n", infile);
    return 0;
  }
  len = filesize(fd) - 8;
  n = lseek(fd, 8, SEEK_SET);
  if (n != 8) {
    fprintf(stderr, "Seek failed\n");
    return 0;
  }
  buf = malloc(len);
  n = read(fd, buf, len);
  if (n < len) {
    fprintf(stderr, "Short read, aborting\n");
    return 0;
  }
  close(fd);
  if (write_cfile(buf, len, cname) < 0) {
    return -1;
  }
  if (write_hfile(buf, len, cname) < 0) {
    return -1;
  }
  return 0;
}


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)
cmpl   $0x3,-0x8(%rbp)
je     11d3 <main+0x43>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x1a0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1234 <main+0xa4>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x1a0>
mov    -0x24(%rbp),%edi
call   10b0 <filesize@plt>
sub    $0x8,%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
mov    $0x8,%esi
call   1040 <lseek@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x8,-0x38(%rbp)
je     1288 <main+0xf8>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x1a0>
mov    -0x34(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    12d5 <main+0x145>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x1a0>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1090 <write_cfile@plt>
cmp    $0x0,%rax
jge    1303 <main+0x173>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <main+0x1a0>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1030 <write_hfile@plt>
cmp    $0x0,%rax
jge    1329 <main+0x199>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <main+0x1a0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    120d <main+0x7d>
mov    0x8(%rsi),%rbp
mov    0x10(%rsi),%r14
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     121f <main+0x8f>
mov    %eax,%ebx
mov    %eax,%edi
call   10b0 <filesize@plt>
mov    %eax,%ebp
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x8,%esi
call   1040 <lseek@plt>
cmp    $0x8,%eax
jne    123d <main+0xad>
add    $0xfffffff8,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
cmp    %ebp,%eax
jge    1263 <main+0xd3>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
jmp    124d <main+0xbd>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
jmp    124d <main+0xbd>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
xor    %ebx,%ebx
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1256 <main+0xc6>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1090 <write_cfile@plt>
test   %rax,%rax
js     1292 <main+0x102>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <write_hfile@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
jmp    1256 <main+0xc6>
mov    $0xffffffff,%ebx
jmp    1256 <main+0xc6>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Usage: ipod2c file.ipod cname\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"Can not open %s\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [13 x i8] c"Seek failed\0A\00"
@.str.3 = [22 x i8] c"Short read, aborting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %76
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
store i8* %23, i8** %7
%24 = load i8*, i8** %6
%25 = load i32, i32* @O_RDONLY
%26 = or i32 %25, 0
%27 = call i32 @open(i8* %24, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %17
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %6
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i32 0, i32* %3
br label %76
34: ; preds = %17
%35 = load i32, i32* %8
%36 = call i32 @filesize(i32 %35)
%37 = sub nsw i32 %36, 8
store i32 %37, i32* %10
%38 = load i32, i32* %8
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @lseek(i32 %38, i32 8, i32 %39)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = icmp ne i32 %41, 8
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %76
46: ; preds = %34
%47 = load i32, i32* %10
%48 = call i8* @malloc(i32 %47)
store i8* %48, i8** %9
%49 = load i32, i32* %8
%50 = load i8*, i8** %9
%51 = load i32, i32* %10
%52 = call i32 @read(i32 %49, i8* %50, i32 %51)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = load i32, i32* %10
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %59
56: ; preds = %46
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %76
59: ; preds = %46
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
%62 = load i8*, i8** %9
%63 = load i32, i32* %10
%64 = load i8*, i8** %7
%65 = call i64 @write_cfile(i8* %62, i32 %63, i8* %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %59
store i32 -1, i32* %3
br label %76
68: ; preds = %59
%69 = load i8*, i8** %9
%70 = load i32, i32* %10
%71 = load i8*, i8** %7
%72 = call i64 @write_hfile(i8* %69, i32 %70, i8* %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %68
store i32 -1, i32* %3
br label %76
75: ; preds = %68
store i32 0, i32* %3
br label %76
76: ; preds = %75, %74, %67, %56, %43, %30, %14
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Usage: ipod2c file.ipod cname\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"Can not open %s\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [13 x i8] c"Seek failed\0A\00"
@.str.3 = [22 x i8] c"Short read, aborting\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %42
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %9, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %42
18: ; preds = %7
%19 = tail call i32 @filesize(i32 %13)
%20 = add nsw i32 %19, -8
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %13, i32 8, i32 %21)
%23 = icmp eq i32 %22, 8
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %42
27: ; preds = %18
%28 = tail call i8* @malloc(i32 %20)
%29 = tail call i32 @read(i32 %13, i8* %28, i32 %20)
%30 = icmp slt i32 %29, %20
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %42
34: ; preds = %27
%35 = tail call i32 @close(i32 %13)
%36 = tail call i64 @write_cfile(i8* %28, i32 %20, i8* %11)
%37 = icmp slt i64 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = tail call i64 @write_hfile(i8* %28, i32 %20, i8* %11)
%40 = ashr i64 %39, 63
%41 = trunc i64 %40 to i32
br label %42
42: ; preds = %38, %34, %31, %24, %15, %4
%43 = phi i32 [ 0, %4 ], [ 0, %15 ], [ 0, %24 ], [ 0, %31 ], [ -1, %34 ], [ %41, %38 ]
ret i32 %43
}