#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int ftruncate(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int main(int argc, char *args[]) {
  int fd;
  int i;
  char buf[52 + 32];
  if (argc < 2) {
    printf("Usage: strip filename\n");
    exit(1);
  }
  for (i = 1; i < argc; i++) {
    if ((fd = open(args[i], O_RDWR)) == -1) {
      perror("Error opening file");
      exit(1);
    }
    if (read(fd, buf, 84) == -1) {
      perror("Error reading file");
      exit(1);
    }
    if (ftruncate(fd, *(int *)(buf + 52 + 16)) == -1) {
      perror("Error truncating file");
      exit(1);
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a6 <main+0x36>
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1265 <main+0xf5>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11f5 <main+0x85>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x54,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    1225 <main+0xb5>
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    124f <main+0xdf>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ad <main+0x3d>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
cmp    $0x2,%edi
jl     1221 <main+0xb1>
mov    %rsi,%r15
mov    %edi,%r12d
mov    $0x1,%ebx
mov    0x2e4f(%rip),%r13
mov    %rsp,%r14
nopl   0x0(%rax)
mov    (%r15,%rbx,8),%rdi
mov    0x0(%r13),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f9 <main+0x89>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x54,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1202 <main+0x92>
mov    0x44(%rsp),%esi
mov    %ebp,%edi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     120b <main+0x9b>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11a0 <main+0x30>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe00(%rip),%rdi
jmp    1212 <main+0xa2>
lea    0xe0a(%rip),%rdi
jmp    1212 <main+0xa2>
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Usage: strip filename\0A\00"
@O_RDWR = global i32 0
@.str.1 = [19 x i8] c"Error opening file\00"
@.str.2 = [19 x i8] c"Error reading file\00"
@.str.3 = [22 x i8] c"Error truncating file\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [84 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
store i32 1, i32* %7
br label %15
15: ; preds = %54, %14
%16 = load i32, i32* %7
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %57
19: ; preds = %15
%20 = load i8**, i8*** %5
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %19
%32 = load i32, i32* %6
%33 = getelementptr inbounds [84 x i8], [84 x i8]* %8, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 84)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31
%40 = load i32, i32* %6
%41 = getelementptr inbounds [84 x i8], [84 x i8]* %8, i64 0, i64 0
%42 = getelementptr inbounds i8, i8* %41, i64 52
%43 = getelementptr inbounds i8, i8* %42, i64 16
%44 = bitcast i8* %43 to i32*
%45 = load i32, i32* %44
%46 = call i32 @ftruncate(i32 %40, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %39
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %15
57: ; preds = %15
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.1 = [19 x i8] c"Error opening file\00"
@.str.2 = [19 x i8] c"Error reading file\00"
@.str.3 = [22 x i8] c"Error truncating file\00"
@str = [22 x i8] c"Usage: strip filename\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [84 x i8]
%4 = getelementptr inbounds [84 x i8], [84 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = getelementptr inbounds [84 x i8], [84 x i8]* %3, i64 0, i64 68
%8 = bitcast i8* %7 to i32*
%9 = zext i32 %0 to i64
br label %13
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6, %36
%14 = phi i64 [ 1, %6 ], [ %38, %36 ]
%15 = getelementptr inbounds i8*, i8** %1, i64 %14
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %13
%24 = call i32 @read(i32 %18, i8* %4, i32 84)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = load i32, i32* %8
%31 = tail call i32 @ftruncate(i32 %18, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = tail call i32 @close(i32 %18)
%38 = add nuw nsw i64 %14, 1
%39 = icmp eq i64 %38, %9
br i1 %39, label %40, label %13
40: ; preds = %36
ret i32 0
}