#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_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
void *sys_st(void *arg) {
  char *buf = arg;
  int fd;
  int rc;
  fd = open("/etc/passwd", O_RDONLY);
  if (fd == -1)
    err(1, "open()");
  rc = read(fd, buf, 1023);
  if (rc <= 0)
    err(1, "read()");
  buf[rc] = 0;
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<sys_st>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1187 <sys_st+0x47>
mov    $0x1,%edi
lea    0xe8a(%rip),%rsi
call   1050 <err@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11b6 <sys_st+0x76>
mov    $0x1,%edi
lea    0xe62(%rip),%rsi
call   1050 <err@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_st>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
lea    0xea9(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1174 <sys_st+0x34>
lea    0xea2(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     119a <sys_st+0x5a>
lea    0xe83(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"read()\00"
define i8* @sys_st(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
store i8* %6, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = load i32, i32* %4
%15 = load i8*, i8** %3
%16 = call i32 @read(i32 %14, i8* %15, i32 1023)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = load i8*, i8** %3
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
ret i8* null
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"read()\00"
define noalias i8* @sys_st(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %1
%8 = tail call i32 @read(i32 %3, i8* %0, i32 1023)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = sext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 0, i8* %14
%15 = tail call i32 @close(i32 %3)
ret i8* null
}