#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 /*<<< orphan*/ O_RDWR;
int creat(char *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ perror(char *);
int pwrite(int, char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strlen(char *);
int main() {
  int fd = creat("a.txt", O_RDWR);
  if (fd == -1) {
    perror("create");
    return errno;
  }
  char *str = "hello world";
  int ret = pwrite(fd, str, strlen(str), 5);
  if (ret == -1) {
    perror("pwrite");
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1060 <creat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    118e <main+0x4e>
lea    0xe8e(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f4 <main+0xb4>
lea    0xe78(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
mov    $0x5,%ecx
call   1040 <pwrite@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ed <main+0xad>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f4 <main+0xb4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
lea    0xeac(%rip),%rdi
call   1060 <creat@plt>
cmp    $0xffffffff,%eax
je     1191 <main+0x51>
mov    %eax,%ebx
lea    0xea6(%rip),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
mov    $0x5,%ecx
call   1040 <pwrite@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     119a <main+0x5a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6e(%rip),%rdi
jmp    11a1 <main+0x61>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
jmp    1189 <main+0x49>

LLVM IR (-O0):
@.str = [6 x i8] c"a.txt\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"create\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"hello world\00"
@.str.3 = [7 x i8] c"pwrite\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @creat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @errno
store i32 %11, i32* %1
br label %24
12: ; preds = %0
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8** %3
%13 = load i32, i32* %2
%14 = load i8*, i8** %3
%15 = load i8*, i8** %3
%16 = call i32 @strlen(i8* %15)
%17 = call i32 @pwrite(i32 %13, i8* %14, i32 %16, i32 5)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @errno
store i32 %22, i32* %1
br label %24
23: ; preds = %12
store i32 0, i32* %1
br label %24
24: ; preds = %23, %20, %9
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [6 x i8] c"a.txt\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"create\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"hello world\00"
@.str.3 = [7 x i8] c"pwrite\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @creat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %8, label %4
4: ; preds = %0
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i32 @pwrite(i32 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %5, i32 5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %4, %0
%9 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %4 ]
%10 = tail call i32 @perror(i8* %9)
%11 = load i32, i32* @errno
br label %12
12: ; preds = %8, %4
%13 = phi i32 [ 0, %4 ], [ %11, %8 ]
ret i32 %13
}