#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*/ write_buffer;
typedef int /*<<< orphan*/ file_name;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int my_file_desc;
  char file_name[4096];
  char write_buffer[4096];
  int write_length;
  char *string;
  int i;
  int w;
  if (argc < 2) {
    printf("need to specify a file name as the first argument\n");
    exit(1);
  }
  for (i = 0; i < sizeof(file_name); i++) {
    file_name[i] = 0;
  }
  strncpy(file_name, argv[1], sizeof(file_name));
  file_name[sizeof(file_name) - 1] = 0;
  my_file_desc = open(file_name, O_CREAT | O_WRONLY, 0600);
  if (my_file_desc < 0) {
    printf("failed to open %s for creation\n", file_name);
    exit(1);
  }
  string =
      "This program brought to you by the council for better C programming";
  for (i = 0; i < sizeof(write_buffer); i++) {
    write_buffer[i] = 0;
  }
  strncpy(write_buffer, string, sizeof(write_buffer));
  write_length = strlen(write_buffer);
  w = write(my_file_desc, write_buffer, write_length);
  if (w != write_length) {
    printf("write didn't complete, w: %d, length: %d\n", w, write_length);
    close(my_file_desc);
    exit(1);
  }
  close(my_file_desc);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ab <main+0x3b>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x2034(%rbp)
movslq -0x2034(%rbp),%rax
cmp    $0x1000,%rax
jae    11eb <main+0x7b>
movslq -0x2034(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x2034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2034(%rbp)
jmp    11b5 <main+0x45>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x1020(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1257 <main+0xe7>
lea    -0x1020(%rbp),%rsi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdf5(%rip),%rax
mov    %rax,-0x2030(%rbp)
movl   $0x0,-0x2034(%rbp)
movslq -0x2034(%rbp),%rax
cmp    $0x1000,%rax
jae    12a5 <main+0x135>
movslq -0x2034(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
mov    -0x2034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2034(%rbp)
jmp    126f <main+0xff>
lea    -0x2020(%rbp),%rdi
mov    -0x2030(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
lea    -0x2020(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2024(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x2024(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x2038(%rbp)
mov    -0x2038(%rbp),%eax
cmp    -0x2024(%rbp),%eax
je     1328 <main+0x1b8>
mov    -0x2038(%rbp),%esi
mov    -0x2024(%rbp),%edx
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2000,%rsp
cmp    $0x2,%edi
jl     1254 <main+0xc4>
mov    %rsi,%rbx
mov    %rsp,%rbp
mov    $0x1000,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    0x8(%rbx),%rsi
mov    %rbp,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xfff(%rsp)
mov    0x2e1f(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x180,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     126a <main+0xda>
mov    %eax,%r14d
lea    0x1000(%rsp),%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xe0a(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    %ebp,%eax
jne    1285 <main+0xf5>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe33(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd8f(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdd8(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14d,%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [51 x i8] c"need to specify a file name as the first argument\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [32 x i8] c"failed to open %s for creation\0A\00"
@.str.2 = [68 x i8] c"This program brought to you by the council for better C programming\00"
@.str.3 = [42 x i8] c"write didn't complete, w: %d, length: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca [4096 x i8]
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
store i32 0, i32* %11
br label %19
19: ; preds = %27, %18
%20 = load i32, i32* %11
%21 = sext i32 %20 to i64
%22 = icmp ult i64 %21, 4096
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = load i32, i32* %11
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %23
%28 = load i32, i32* %11
%29 = add nsw i32 %28, 1
store i32 %29, i32* %11
br label %19
30: ; preds = %19
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @strncpy(i8* %31, i8* %34, i32 4096)
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 4095
store i8 0, i8* %36
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%38 = load i32, i32* @O_CREAT
%39 = load i32, i32* @O_WRONLY
%40 = or i32 %38, %39
%41 = call i32 @open(i8* %37, i32 %40, i32 384)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %30
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %30
store i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i8** %10
store i32 0, i32* %11
br label %49
49: ; preds = %57, %48
%50 = load i32, i32* %11
%51 = sext i32 %50 to i64
%52 = icmp ult i64 %51, 4096
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i32, i32* %11
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %55
store i8 0, i8* %56
br label %57
57: ; preds = %53
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 1
store i32 %59, i32* %11
br label %49
60: ; preds = %49
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%62 = load i8*, i8** %10
%63 = call i32 @strncpy(i8* %61, i8* %62, i32 4096)
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%65 = call i32 @strlen(i8* %64)
store i32 %65, i32* %9
%66 = load i32, i32* %6
%67 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%68 = load i32, i32* %9
%69 = call i32 @write(i32 %66, i8* %67, i32 %68)
store i32 %69, i32* %12
%70 = load i32, i32* %12
%71 = load i32, i32* %9
%72 = icmp ne i32 %70, %71
br i1 %72, label %73, label %80
73: ; preds = %60
%74 = load i32, i32* %12
%75 = load i32, i32* %9
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %74, i32 %75)
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %60
%81 = load i32, i32* %6
%82 = call i32 @close(i32 %81)
ret i32 0
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [32 x i8] c"failed to open %s for creation\0A\00"
@.str.2 = [68 x i8] c"This program brought to you by the council for better C programming\00"
@.str.3 = [42 x i8] c"write didn't complete, w: %d, length: %d\0A\00"
@str = [50 x i8] c"need to specify a file name as the first argument\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = icmp slt i32 %0, 2
br i1 %8, label %20, label %9
9: ; preds = %2
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @strncpy(i8* %6, i8* %12, i32 4096)
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 4095
store i8 0, i8* %14
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %16, %15
%18 = call i32 @open(i8* %6, i32 %17, i32 384)
%19 = icmp slt i32 %18, 0
br i1 %19, label %28, label %23
20: ; preds = %2
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %9
%24 = call i32 @strncpy(i8* %7, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i32 4096)
%25 = call i32 @strlen(i8* %7)
%26 = call i32 @write(i32 %18, i8* %7, i32 %25)
%27 = icmp eq i32 %26, %25
br i1 %27, label %35, label %31
28: ; preds = %9
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %25)
%33 = call i32 @close(i32 %18)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %23
%36 = call i32 @close(i32 %18)
ret i32 0
}