#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*/ assert(int);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int strlen(char *);
int main() {
  FILE *fp;
  int err;
  char str[] = "test string";
  fp = fopen("file.txt", "w");
  err = fwrite(str, 1, strlen(str) * sizeof(char), fp);
  assert(err == 0);
  err = fwrite(str, 1, strlen(str) * sizeof(char), fp);
  assert(err == 0);
  fclose(fp);
  return errno;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe98(%rip),%eax
mov    %eax,-0x18(%rbp)
lea    0xe92(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
shl    $0x0,%rax
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
cltq
shl    $0x0,%rax
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movabs $0x7274732074736574,%rax
73 74 72
mov    %rax,0x8(%rsp)
movl   $0x676e69,0x10(%rsp)
lea    0xe97(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.str = [12 x i8] c"test string\00"
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [12 x i8]
store i32 0, i32* %1
%5 = bitcast [12 x i8]* %4 to i8*
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = mul i64 %10, 1
%12 = trunc i64 %11 to i32
%13 = load i32*, i32** %2
%14 = call i32 @fwrite(i8* %7, i32 1, i32 %12, i32* %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%20 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 1
%24 = trunc i64 %23 to i32
%25 = load i32*, i32** %2
%26 = call i32 @fwrite(i8* %19, i32 1, i32 %24, i32* %25)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i32*, i32** %2
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* @errno
ret i32 %33
}

LLVM IR (-O2):
@__const.main.str = [12 x i8] c"test string\00"
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca [12 x i8]
%2 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = call i32 @strlen(i8* %2)
%5 = call i32 @fwrite(i8* %2, i32 1, i32 %4, i32* %3)
%6 = icmp eq i32 %5, 0
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = call i32 @strlen(i8* %2)
%10 = call i32 @fwrite(i8* %2, i32 1, i32 %9, i32* %3)
%11 = icmp eq i32 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = call i32 @fclose(i32* %3)
%15 = load i32, i32* @errno
ret i32 %15
}