#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_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  char buf[1000];
  int fp, size;
  unsigned int *address;
  address = (unsigned int *)buf;
  *address = 0x804b054;
  scanf("%s", buf + 4);
  size = strlen(buf + 4) + 4;
  printf("The string length is %d\n", size);
  fp = open("mystring", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (fp != -1) {
    write(fp, buf, size);
    close(fp);
  } else {
    printf("Open failed!\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x3f0(%rbp),%rax
mov    %rax,-0x400(%rbp)
mov    -0x400(%rbp),%rax
movl   $0x804b054,(%rax)
lea    -0x3f0(%rbp),%rsi
add    $0x4,%rsi
lea    0xe61(%rip),%rdi
call   1070 <scanf@plt>
lea    -0x3f0(%rbp),%rdi
add    $0x4,%rdi
call   1040 <strlen@plt>
add    $0x4,%eax
mov    %eax,-0x3f8(%rbp)
mov    -0x3f8(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
mov    0x2dd4(%rip),%rax
or     (%rax),%edx
lea    0xe17(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x3f4(%rbp)
cmpl   $0xffffffff,-0x3f4(%rbp)
je     1245 <main+0xe5>
mov    -0x3f4(%rbp),%edi
lea    -0x3f0(%rbp),%rsi
mov    -0x3f8(%rbp),%edx
call   1030 <write@plt>
mov    -0x3f4(%rbp),%edi
call   1060 <close@plt>
jmp    1253 <main+0xf3>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x3e8,%rsp
movl   $0x804b054,(%rsp)
lea    0x4(%rsp),%rbx
lea    0xe74(%rip),%rdi
mov    %rbx,%rsi
call   1080 <scanf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
add    $0x4,%ebx
lea    0xe5b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e20(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
mov    0x2ded(%rip),%rax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe37(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1206 <main+0x96>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1212 <main+0xa2>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [25 x i8] c"The string length is %d\0A\00"
@.str.2 = [9 x i8] c"mystring\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [14 x i8] c"Open failed!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%7 = bitcast i8* %6 to i32*
store i32* %7, i32** %5
%8 = load i32*, i32** %5
store i32 134525012, i32* %8
%9 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds i8, i8* %9, i64 4
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%13 = getelementptr inbounds i8, i8* %12, i64 4
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 4
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %23, %24
%26 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %25)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %0
%30 = load i32, i32* %3
%31 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%32 = load i32, i32* %4
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
%34 = load i32, i32* %3
%35 = call i32 @close(i32 %34)
br label %38
36: ; preds = %0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %29
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [25 x i8] c"The string length is %d\0A\00"
@.str.2 = [9 x i8] c"mystring\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@str = [13 x i8] c"Open failed!\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%3 = bitcast [1000 x i8]* %1 to i32*
store i32 134525012, i32* %3
%4 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 4
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = call i32 @strlen(i8* %4)
%7 = add nsw i32 %6, 4
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %0
%20 = call i32 @write(i32 %17, i8* %2, i32 %7)
%21 = call i32 @close(i32 %17)
br label %24
22: ; preds = %0
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
ret i32 0
}