Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:00:34+00:00 2026-06-14T22:00:34+00:00

While Learning compiler Optimisation, I write codes in C under Linux with GCC version

  • 0

While Learning compiler Optimisation, I write codes in C under Linux with GCC version gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5.1)
To understant not a statement (nop) in C. I written two codes first y.c second x.c and generate their compiled assembly code using gcc -S option.

Fist code y.c

desktop:~$ cat y.c
main()
{
int i=0;
}
desktop:~$ gcc -S y.c
desktop:~$ 

Second code x.c

desktop:~$ cat x.c
main()
{
int i=0;

/* Loops and if*/
while(0);
for(;0;);
if(0);

/* Arithmetic Operations */
i * i;
i / i;
i - i;
i + i;
i % i;
 +i;
 -i;

/* Comparison operators */
i == i;
i != i;
i < i;
i > i;
i >= i;
i <= i;

/* Bitwise Operations*/
i ^ i;
i | i;
i & i;
 ~i;
i << i;
i >> i;

/* Address Operatins*/ 
 &i;
 *(&i);
 (&i)[i];

/* Ternary conditional operation*/
 i? i : i;

/* Other Operations*/ 
 sizeof(i);
 (char)i;

/* Not-Logical Operation*/ 
 !i; 

/* Logical AND , OR Operators */ 
// i && i;  // Commented && operation 
// i || i;  // Commented || operation
}
desktop:~$ gcc -S x.c

NOTICE: This time Last two lines in x.c are commented.
And as I was expecting. There is no difference in their generated assembly codes. I compared x.s and y.s using diff command.

desktop:~$ diff x.s y.s
1c1
<   .file   "x.c"
---
>   .file   "y.c"
desktop:~$

But When I uncomment last(or say add) two lines in x.c.

i && i;  
i || i; 

Again compile x.c with -S option and compared with y.s.

desktop:~$ tail  x.c  
 sizeof(i);
 (char)i;

/* Not-Logical Operation*/ 
 !i; 

/* Logical AND , OR Operators */ 
i && i;  // unCommented && operation 
i || i;  // unCommented || operation
}
desktop:~$ gcc -S x.c
desktop:~$ diff x.s y.s
1c1
<     .file    "x.c"
---
>     .file    "y.c"
10,21d9
<     movl    -4(%ebp), %eax
<     testl    %eax, %eax
<     je    .L3
<     movl    -4(%ebp), %eax
<     testl    %eax, %eax
< .L3:
<     movl    -4(%ebp), %eax
<     testl    %eax, %eax
<     jne    .L8
<     movl    -4(%ebp), %eax
<     testl    %eax, %eax
< .L8:
desktop:~$ 

Question:
I just can not understand why expressions i || i and i && i are not equivalent to 'not a statement'?

Why compiler transfer this two statements into executable( we can disassemble with objdump will get same code). What is special in this two expression. they do not includes = operation.

Does they change (set/reset) CPU flag-registers ? I guess no!

Even / division operation discarded that may cause an divided by zero fault.

EDIT : Added answer

There is nothing special in i || i and i && i expressions. Both are equivalent to
NOT A STATEMENT. And can be removed by GCC compiler with some extra effort.

To remove this: -o2 and -o3 flags are useful:
Here is my Try!!

desktop:~$ gcc -o2 -S y.c 
desktop:~$ gcc -o2 -S x.c 
desktop:~$ diff x.s y.s -y
    .file   "x.c"                         |     .file   "y.c"
    .text                               .text
    .p2align 4,,15                        <
.globl main                         .globl main
    .type   main, @function                     .type   main, @function
main:                               main:
    pushl   %ebp                            pushl   %ebp
    movl    %esp, %ebp                      movl    %esp, %ebp
    popl    %ebp                          |     subl    $16, %esp
                                  >     movl    $0, -4(%ebp)
                                  >     leave

The extra lines in at RHS are because of misalignment between files.

I also like to add information that JAVA and C# compilers discard this expressions without any flags.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:00:35+00:00Added an answer on June 14, 2026 at 10:00 pm

    Enable optimizations using -O2, and you should see the extra code disappear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While learning Scalaz 6 , I'm trying to write type-safe readers returning validations. Here
Possible Duplicate: Learning to write a compiler I looked around trying to find out
While learning Rails, I found this example in the Sam Ruby's book: app/controllers/line_items_controller.rb def
While learning an example from learn-you-a-haskell which right triangle that has integers for all
I am developing a small app while learning Android. The app is basically making
i just got some more questions while learning PHP, does php implement any built
MOV is probably the first instruction everyone learns while learning ASM. Just now I
I've slightly modified the iPhone SDK's GLSprite example while learning OpenGL ES and it
So I found some similarities between arrays and set notation while learning about sets
Some background info: I am currently writing a bootloader in protected mode while learning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.