#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 assemble(int /*<<< orphan*/ *, int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ mkstemp(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ remove(char *);
scalar_t__ rename(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("Usage: %s infile outfile\n", argv[0]);
    return 1;
  }
  FILE *in_file = fopen(argv[1], "r");
  if (in_file == NULL) {
    printf("Could not open input file\n");
    return 1;
  }
  char *temp_output_filename =
      (char *)malloc(sizeof(char) * (strlen(argv[2]) + 7));
  strcpy(temp_output_filename, argv[2]);
  strcat(temp_output_filename, "XXXXXX");
  mkstemp(temp_output_filename);
  FILE *out_file = fopen(temp_output_filename, "wb");
  if (out_file == NULL) {
    printf("Could not open temp output file\n");
    return 1;
  }
  int assembler_success = assemble(in_file, out_file, argv[1]);
  fclose(in_file);
  fclose(out_file);
  if (assembler_success == 0) {
    if (rename(temp_output_filename, argv[2]) != 0) {
      printf("Could not write output file\n");
      return 1;
    }
  } else {
    remove(temp_output_filename);
  }
  free(temp_output_filename);
  return assembler_success;
}


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   $0x3,-0x8(%rbp)
je     1201 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x188>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0a(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123e <main+0x7e>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x188>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <strlen@plt>
add    $0x7,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xdbc(%rip),%rsi
call   10e0 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <mkstemp@plt>
mov    -0x20(%rbp),%rdi
lea    0xdaa(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12c2 <main+0x102>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x188>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
call   1090 <assemble@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1330 <main+0x170>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   10d0 <rename@plt>
cmp    $0x0,%rax
je     132b <main+0x16b>
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x188>
jmp    1339 <main+0x179>
mov    -0x20(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1285 <main+0xb5>
mov    0x8(%rbx),%rdi
lea    0xe2b(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1298 <main+0xc8>
mov    %rax,%r15
mov    0x10(%rbx),%rdi
call   1080 <strlen@plt>
lea    0x7(%rax),%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rsi
mov    %rax,%rdi
call   1050 <strcpy@plt>
lea    0xdf5(%rip),%rsi
mov    %r14,%rdi
call   10f0 <strcat@plt>
mov    %r14,%rdi
call   10c0 <mkstemp@plt>
lea    0xde5(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12a1 <main+0xd1>
mov    %rax,%r12
mov    0x8(%rbx),%rdx
mov    %r15,%rdi
mov    %rax,%rsi
call   10a0 <assemble@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    %r12,%rdi
call   1070 <fclose@plt>
test   %ebp,%ebp
je     12aa <main+0xda>
mov    %r14,%rdi
call   1040 <remove@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    12cc <main+0xfc>
mov    (%rbx),%rsi
lea    0xd71(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    12c7 <main+0xf7>
lea    0xdc3(%rip),%rdi
jmp    12c2 <main+0xf2>
lea    0xd9a(%rip),%rdi
jmp    12c2 <main+0xf2>
mov    0x10(%rbx),%rsi
mov    %r14,%rdi
call   10e0 <rename@plt>
test   %rax,%rax
je     127b <main+0xab>
lea    0xd64(%rip),%rdi
call   1060 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Usage: %s infile outfile\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"Could not open input file\0A\00"
@.str.3 = [7 x i8] c"XXXXXX\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [33 x i8] c"Could not open temp output file\0A\00"
@.str.6 = [29 x i8] c"Could not write output file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %82
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %82
26: ; preds = %17
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @strlen(i8* %29)
%31 = add nsw i32 %30, 7
%32 = sext i32 %31 to i64
%33 = mul i64 1, %32
%34 = trunc i64 %33 to i32
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i32 @strcpy(i8* %37, i8* %40)
%42 = load i8*, i8** %7
%43 = call i32 @strcat(i8* %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%44 = load i8*, i8** %7
%45 = call i32 @mkstemp(i8* %44)
%46 = load i8*, i8** %7
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %47, i32** %8
%48 = load i32*, i32** %8
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %52
50: ; preds = %26
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %82
52: ; preds = %26
%53 = load i32*, i32** %6
%54 = load i32*, i32** %8
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = call i32 @assemble(i32* %53, i32* %54, i8* %57)
store i32 %58, i32* %9
%59 = load i32*, i32** %6
%60 = call i32 @fclose(i32* %59)
%61 = load i32*, i32** %8
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %9
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %75
65: ; preds = %52
%66 = load i8*, i8** %7
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 2
%69 = load i8*, i8** %68
%70 = call i64 @rename(i8* %66, i8* %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %65
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %82
74: ; preds = %65
br label %78
75: ; preds = %52
%76 = load i8*, i8** %7
%77 = call i32 @remove(i8* %76)
br label %78
78: ; preds = %75, %74
%79 = load i8*, i8** %7
%80 = call i32 @free(i8* %79)
%81 = load i32, i32* %9
store i32 %81, i32* %3
br label %82
82: ; preds = %78, %72, %50, %24, %12
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@.str = [26 x i8] c"Usage: %s infile outfile\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [7 x i8] c"XXXXXX\00"
@.str.4 = [3 x i8] c"wb\00"
@str = [28 x i8] c"Could not write output file\00"
@str.7 = [32 x i8] c"Could not open temp output file\00"
@str.8 = [26 x i8] c"Could not open input file\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 i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %45
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
br label %45
14: ; preds = %7
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 7
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = load i8*, i8** %15
%22 = tail call i32 @strcpy(i8* %20, i8* %21)
%23 = tail call i32 @strcat(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @mkstemp(i8* %20)
%25 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.7, i64 0, i64 0))
br label %45
29: ; preds = %14
%30 = load i8*, i8** %8
%31 = tail call i32 @assemble(i32* %10, i32* %25, i8* %30)
%32 = tail call i32 @fclose(i32* %10)
%33 = tail call i32 @fclose(i32* %25)
%34 = icmp eq i32 %31, 0
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = load i8*, i8** %15
%37 = tail call i64 @rename(i8* %20, i8* %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %45
41: ; preds = %29
%42 = tail call i32 @remove(i8* %20)
br label %43
43: ; preds = %35, %41
%44 = tail call i32 @free(i8* %20)
br label %45
45: ; preds = %12, %39, %43, %27, %4
%46 = phi i32 [ 1, %4 ], [ 1, %12 ], [ 1, %27 ], [ 1, %39 ], [ %31, %43 ]
ret i32 %46
}