#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*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  FILE *tmpfd;
  static char *tmpfile, buf[16];
  if (argc <= 1) {
    fprintf(stderr, "Usage: %s <garbage>\n", argv[0]);
    exit(-1);
  }
  tmpfile = "./hello.tmp";
  printf("before: tmpfile = %s\n", tmpfile);
  printf("Enter one line of data to put in %s: ", tmpfile);
  printf("\ndiff:%d\n", (int)buf - (int)&tmpfile);
  gets(buf);
  printf("\nafter: tmpfile = %s\n", tmpfile);
  tmpfd = fopen(tmpfile, "w");
  if (tmpfd == NULL) {
    fprintf(stderr, "error opening %s: %s\n", tmpfile, strerror(errno));
    exit(-1);
  }
  fputs(buf, tmpfd);
  fclose(tmpfd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jg     11c1 <main+0x41>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe4d(%rip),%rax
mov    %rax,0x2ea1(%rip)
mov    0x2e9a(%rip),%rsi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e85(%rip),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe5d(%rip),%rdi
lea    0x2e79(%rip),%rax
mov    %eax,%esi
lea    0x2e60(%rip),%rax
sub    %eax,%esi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0x2e60(%rip),%rdi
call   1070 <gets@plt>
mov    0x2e44(%rip),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e2f(%rip),%rdi
lea    0xe35(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12a3 <main+0x123>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e01(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0x2dd2(%rip),%rdi
call   1050 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    1238 <main+0xb8>
lea    0xe81(%rip),%rsi
mov    %rsi,0x2ed5(%rip)
lea    0x2ece(%rip),%r14
lea    0xe78(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2eb9(%rip),%rsi
lea    0xe79(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x2eb4(%rip),%rbx
mov    %ebx,%esi
sub    %r14d,%esi
lea    0xe85(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <gets@plt>
mov    0x2e82(%rip),%rsi
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e6d(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     125c <main+0xdc>
mov    %rax,%rbx
lea    0x2e62(%rip),%rdi
mov    %rax,%rsi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdb5(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e04(%rip),%r14
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdfe(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@main.tmpfile = internal global i8* null
@main.buf = internal global [16 x i8] zeroinitializer
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s <garbage>\0A\00"
@.str.1 = [12 x i8] c"./hello.tmp\00"
@.str.2 = [22 x i8] c"before: tmpfile = %s\0A\00"
@.str.3 = [38 x i8] c"Enter one line of data to put in %s: \00"
@.str.4 = [10 x i8] c"\0Adiff:%d\0A\00"
@.str.5 = [22 x i8] c"\0Aafter: tmpfile = %s\0A\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [22 x i8] c"error opening %s: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%6 = load i32, i32* %3
%7 = icmp sle i32 %6, 1
br i1 %7, label %8, label %15
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8**, i8*** %4
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** @main.tmpfile
%16 = load i8*, i8** @main.tmpfile
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** @main.tmpfile
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 sub nsw (i32 ptrtoint ([16 x i8]* @main.buf to i32), i32 ptrtoint (i8** @main.tmpfile to i32)))
%21 = call i32 @gets(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0))
%22 = load i8*, i8** @main.tmpfile
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** @main.tmpfile
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %25, i32** %5
%26 = load i32*, i32** %5
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %35
28: ; preds = %15
%29 = load i32, i32* @stderr
%30 = load i8*, i8** @main.tmpfile
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %30, i8* %32)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %15
%36 = load i32*, i32** %5
%37 = call i32 @fputs(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0), i32* %36)
%38 = load i32*, i32** %5
%39 = call i32 @fclose(i32* %38)
ret i32 0
}

LLVM IR (-O2):
@main.tmpfile = internal global i8* null
@main.buf = internal global [16 x i8] zeroinitializer
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s <garbage>\0A\00"
@.str.1 = [12 x i8] c"./hello.tmp\00"
@.str.2 = [22 x i8] c"before: tmpfile = %s\0A\00"
@.str.3 = [38 x i8] c"Enter one line of data to put in %s: \00"
@.str.4 = [10 x i8] c"\0Adiff:%d\0A\00"
@.str.5 = [22 x i8] c"\0Aafter: tmpfile = %s\0A\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [22 x i8] c"error opening %s: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** @main.tmpfile
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%11 = load i8*, i8** @main.tmpfile
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 sub (i32 ptrtoint ([16 x i8]* @main.buf to i32), i32 ptrtoint (i8** @main.tmpfile to i32)))
%14 = tail call i32 @gets(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0))
%15 = load i8*, i8** @main.tmpfile
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** @main.tmpfile
%18 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %27
20: ; preds = %9
%21 = load i32, i32* @stderr
%22 = load i8*, i8** @main.tmpfile
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %22, i8* %24)
%26 = tail call i32 @exit(i32 -1)
unreachable
27: ; preds = %9
%28 = tail call i32 @fputs(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0), i32* %18)
%29 = tail call i32 @fclose(i32* %18)
ret i32 0
}